labelSheets = new LabelSheets(); $this->profiles = new Profiles(); $this->templates = new Templates(); } 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', 'around', 'across', 'marginLeft', 'marginRight', 'marginTop', 'marginBottom', 'aroundCount', 'acrossCount', 'paperId', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $validationOptions = array( 'id' => 'numeric', 'name' => 'required', 'width' => 'required|numeric', 'height' => 'required|numeric', 'around' => 'required|numeric', 'across' => 'required|numeric', 'marginLeft' => 'required|numeric', 'marginRight' => 'required|numeric', 'marginTop' => 'required|numeric', 'marginBottom' => 'required|numeric', 'aroundCount' => 'required|numeric', 'acrossCount' => 'required|numeric', 'paperId' => 'required|numeric', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { throw new LabelPrintingError(implode(', ', $validator->getErrors())); } $data['userId'] = get_current_user_id(); $paperLabelSheetFoundByName = $this->labelSheets->getByPaperIdAndName($data['paperId'], $data['name']); if (isset($data['id']) && $data['id'] > 0) { $id = $data['id']; if (!empty($paperLabelSheetFoundByName) && $paperLabelSheetFoundByName->id !== $data['id']) { throw new LabelPrintingError(__('Paper sheet with same name already exists.', 'a4-barcode-generator')); } $result = $this->labelSheets->update($data['id'], $data); } else { if (!empty($paperLabelSheetFoundByName)) { throw new LabelPrintingError(__('Paper sheet with same name already exists.', 'a4-barcode-generator')); } $result = $this->labelSheets->add($data); $id = $result; } if (false === $result) { throw new LabelPrintingError(__('Data was not saved.', 'a4-barcode-generator')); } } catch (LabelPrintingError $e) { $success = false; $error = array($e->getMessage()); } $sheets = $this->labelSheets->getAll(); return compact('success', 'error', 'id', 'sheets'); } public function remove() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $post = Request::getData(); $data = array(); if (isset($post['id'])) { $data['id'] = sanitize_text_field($post['id']); } $validationOptions = array('id' => 'required|numeric'); $data = Validator::create($data, $validationOptions, true)->validate(); $labelSheet = $this->labelSheets->get($data['id']); if (empty($labelSheet)) { return array( 'success' => false, 'error' => array(__('Label sheet not found.', 'a4-barcode-generator')), ); } if ( !current_user_can('manage_options') && $labelSheet['userId'] != get_current_user_id() ) { return array( 'success' => false, 'error' => array(__("No rights to delete.", 'a4-barcode-generator')), ); } $isRemoved = $this->labelSheets->update($data['id'], array('is_removed' => 1)); if ($isRemoved) { $success = true; $error = array(); } else { $success = false; $error = array(__('Data was not removed.', 'a4-barcode-generator')); } $sheets = $this->labelSheets->getAll(); $labelSheets = $this->labelSheets->getByPaperId($labelSheet['paperId']); return compact('success', 'error', 'sheets', 'labelSheets'); } }