'required', 'template' => 'required|xml', 'height' => 'numeric', 'width' => 'numeric', ); /** * CustomBarcodeTemplates constructor. */ public function __construct() { global $wpdb; $this->wpdb = $wpdb; $this->tbl = $wpdb->prefix.'a4barcode_custom_templates'; } /** * Create page. */ public function create() { // Get data from db $templates = $this->wpdb->get_results("SELECT * FROM `{$this->tbl}`"); // Get old post data $old = isset($_COOKIE['old_post']) ? json_decode(base64_decode($_COOKIE['old_post']), true) : array(); // Show template include_once A4B_PLUGIN_BASE_PATH.'templates/barcode-templates.php'; } /** * Store and redirect ot edit page. */ public function store() { ValidatorPostData::validate($this->templateValidationRules, true); // Get data from request $name = isset($_POST['name']) ? $_POST['name'] : ''; $template = isset($_POST['template']) ? stripslashes(trim($_POST['template'])) : ''; $height = isset($_POST['height']) ? $_POST['height'] : ''; $width = isset($_POST['width']) ? $_POST['width'] : ''; $uol = isset($_POST['uol']) ? $_POST['uol'] : ''; // Store new template data and make it active. $result = $this->wpdb->insert($this->tbl, compact('name', 'template', 'height', 'width', 'uol')); // If update ok, show success message. if (false !== $result) { a4bFlashMessage(__('Template saved successfully!', 'wpbcu-barcode-generator'), 'success'); } // Redirect to edit page $this->redirectToEditPage($this->wpdb->insert_id); } /** * Edit page. */ public function edit() { // Get id from request $id = isset($_GET['id']) ? intval($_GET['id']) : null; // Get data from db if ($id) { // Get template by id $chosenTemplateRow = $this->wpdb->get_row( $this->wpdb->prepare("SELECT * FROM `{$this->tbl}` WHERE `id` = %s", $id) ); } else { // Get active template $chosenTemplateRow = $this->wpdb->get_row("SELECT * FROM `{$this->tbl}` WHERE `is_active` = 1"); } // Get data from db $chosenTemplate = new BarcodeTemplate($chosenTemplateRow); $templates = $this->wpdb->get_results("SELECT * FROM `{$this->tbl}`"); // Get old post data $old = isset($_COOKIE['old_post']) ? json_decode(base64_decode($_COOKIE['old_post']), true) : array(); // Show template include_once A4B_PLUGIN_BASE_PATH.'templates/barcode-templates.php'; } /** * Update and redirect to edit page. */ public function update() { ValidatorPostData::validate($this->templateValidationRules, true); // Get data from request $id = isset($_POST['id']) ? $_POST['id'] : ''; $name = isset($_POST['name']) ? $_POST['name'] : ''; $template = isset($_POST['template']) ? stripslashes(trim($_POST['template'])) : ''; $height = isset($_POST['height']) ? $_POST['height'] : ''; $width = isset($_POST['width']) ? $_POST['width'] : ''; $uol = isset($_POST['uol']) ? $_POST['uol'] : ''; // Update data in db $result = $this->wpdb->update($this->tbl, compact('name', 'template', 'height', 'width', 'uol'), array('id' => $id)); // If update ok, show success message. if (false !== $result) { a4bFlashMessage(__('Template updated successfully!', 'wpbcu-barcode-generator'), 'success'); } // Redirect to edit page $this->redirectToEditPage($id); } /** * Delete and redirect to edit page. */ public function delete() { // Get data from request $id = isset($_POST['id']) ? $_POST['id'] : ''; // Delete from db $this->wpdb->delete($this->tbl, array('id' => $id, 'is_default' => 0)); // Redirect to edit page $this->redirectToEditPage(); } /** * Set template as active and show it edit page. */ public function setactive() { // Get template id from request $id = isset($_POST['id']) ? intval($_POST['id']) : null; // Get template id from request if ($id) { // Set chosen template as active $this->setActiveTemplate($id); } // Redirect to edit page $this->redirectToEditPage($id); } /** * Set active template by id. * * @param $id */ protected function setActiveTemplate($id) { // Unset old active template $this->wpdb->update($this->tbl, array('is_active' => 0), array('is_active' => 1)); // Set template with given id as active $this->wpdb->update($this->tbl, array('is_active' => 1), array('id' => $id)); } /** * Redirect ot edit page. * * @param null $id */ protected function redirectToEditPage($id = null) { wp_redirect(admin_url('/admin.php?page=wpbcu-barcode-templates-edit&id='.$id)); exit; } /** * Redirect to create page. */ protected function redirectToCreatePage() { wp_redirect(admin_url('/admin.php?page=wpbcu-barcode-templates-create')); exit; } /** * Get active template. * * @return mixed */ public function getActiveTemplate() { // Get active template $chosenTemplateRow = $this->wpdb->get_row("SELECT * FROM `{$this->tbl}` WHERE `is_active` = 1"); return $chosenTemplateRow; } }