paperFormats = new PaperFormats(); $this->labelSheets = new LabelSheets(); $this->profiles = new Profiles(); } public function save($post = null) { $success = true; $error = array(); $id = 0; try { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } if (empty($post)) { $post = Request::getData(); } $data = array(); foreach (array('id', 'name', 'width', 'height', 'units') as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); if ($data[$key] === 'true') { $data[$key] = 1; } elseif ($data[$key] === 'false') { $data[$key] = 0; } } } $validationOptions = array( 'id' => 'numeric', 'name' => 'required', 'width' => 'required|numeric', 'height' => 'required|numeric', 'units' => 'required', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { throw new LabelPrintingError(implode(', ', $validator->getErrors())); } $paperFoundByName = $this->paperFormats->getByName($data['name']); if (!isset($data['id']) || $data['id'] == 0) { if (!empty($paperFoundByName)) { throw new LabelPrintingError(__('Paper with same name already exists.', 'a4-barcode-generator')); } $result = $this->paperFormats->add($data); if (false === $result) { throw new LabelPrintingError(__('Data was not saved.', 'a4-barcode-generator')); } $id = $result; $defaultWidthMargin = LabelSheets::calculateDefaultMarginBySize($data['width'], $data['units']); $defaultHeightMargin = LabelSheets::calculateDefaultMarginBySize($data['height'], $data['units']); $saveLabelSheetResult = $this->labelSheets->add(array( 'width' => LabelSheets::calculateSizeByPaperSizeAndMargin($data['width'], $defaultWidthMargin, $data['units']), 'height' => LabelSheets::calculateSizeByPaperSizeAndMargin($data['height'], $defaultHeightMargin, $data['units']), 'marginLeft' => $defaultWidthMargin, 'marginRight' => $defaultWidthMargin, 'marginTop' => $defaultHeightMargin, 'marginBottom' => $defaultHeightMargin, 'paperId' => $id, 'userId' => get_current_user_id(), )); if (false === $saveLabelSheetResult) { throw new LabelPrintingError(__('Paper was saved, but default label sheet was not saved.', 'a4-barcode-generator')); } } else { $id = $data['id']; if (!empty($paperFoundByName) && $paperFoundByName->id !== $data['id']) { throw new LabelPrintingError(__('Paper with same name already exists.', 'a4-barcode-generator')); } $result = $this->paperFormats->update($data); if (false === $result) { throw new LabelPrintingError(__('Data was not saved.', 'a4-barcode-generator')); } } } catch (LabelPrintingError $e) { $success = false; $error = array($e->getMessage()); } $listFormats = $this->paperFormats->getAll(); $labelSheets = $this->labelSheets->getAll(); return compact('success', 'error', 'id', 'listFormats', 'labelSheets'); } public function remove() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $post = Request::getData(); $data = array(); if (isset($post['id'])) { $data['id'] = sanitize_key($post['id']); } $validationOptions = array('id' => 'required|numeric'); $data = Validator::create($data, $validationOptions, true)->validate(); $paperFormat = $this->paperFormats->get($data['id']); if (empty($paperFormat)) { return array( 'success' => false, 'error' => array(__('Paper format not found.', 'a4-barcode-generator')), ); } $isRemoved = $this->paperFormats->update(array('id' => $data['id'], 'is_removed' => 1)); if ($isRemoved) { $success = true; $error = array(); } else { $success = false; $error = array(__('Data was not removed.', 'a4-barcode-generator')); } $listFormats = $this->paperFormats->getAll(); return compact('success', 'error', 'listFormats'); } public function getAll() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $listFormats = $this->paperFormats->getAll(); $listSheets = $this->labelSheets->getAll(); $this->profiles->createUserDefaultProfileIfNotExists(); $listProfiles = $this->profiles->getProfilesList(); $dimensions = new Dimensions(); return array( 'dimension' => $dimensions->getActive(), 'listFormats' => $listFormats, 'listSheets' => $listSheets, 'listProfiles' => $listProfiles, 'error' => empty($listFormats) ? array(__('No data found on request.', 'a4-barcode-generator')) : array(), 'success' => true, ); } }