'required|numeric', ); // Checking for the correct data from the request. Send error if not valid. ValidatorPostData::validate($validationOptions, true); $id = PostData::$postData['id']; $result = $wpdb->delete("{$wpdb->prefix}a4barcode_custom_formats", array('id' => $id), array('%d')); // If the request was made with an error, then write an error message if (false === $result) { $error[] = __('Data was not deleted.', 'wpbcu-barcode-generator'); // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } } else { $success[] = __('Data successfully deleted.', 'wpbcu-barcode-generator'); } $response = array( 'success' => $success, 'error' => $error, ); a4bJsonResponse($response); } /** * Creates / updates the format data. */ public function saveFormat() { global $wpdb; $error = array(); $success = array(); $validationOptions = array( 'id' => 'numeric', //??? 'name' => 'required', 'width' => 'required|numeric', 'height' => 'required|numeric', 'arround' => 'required|numeric', 'across' => 'required|numeric', 'marginLeft' => 'required|numeric', 'marginRight' => 'required|numeric', 'marginTop' => 'required|numeric', 'marginBottom' => 'required|numeric', 'arroundCount' => 'required|numeric', 'acrossCount' => 'required|numeric', 'paperId' => 'required|numeric', ); // checking for the correct data from the request. Return error if not valid. ValidatorPostData::validate($validationOptions, true); $id = (isset(PostData::$postData['id'])) ? PostData::$postData['id'] : null; $userId = get_current_user_id(); $dataFormat = array( 'userId' => $userId, 'name' => PostData::$postData['name'], 'width' => PostData::$postData['width'], 'height' => PostData::$postData['height'], 'arround' => PostData::$postData['arround'], 'across' => PostData::$postData['across'], 'marginLeft' => PostData::$postData['marginLeft'], 'marginRight' => PostData::$postData['marginRight'], 'marginTop' => PostData::$postData['marginTop'], 'marginBottom' => PostData::$postData['marginBottom'], 'arroundCount' => PostData::$postData['arroundCount'], 'acrossCount' => PostData::$postData['acrossCount'], 'paperId' => PostData::$postData['paperId'], ); $result = false; // if the id parameter is null, create an entry in the table if (is_null($id)) { $result = $wpdb->insert("{$wpdb->prefix}a4barcode_custom_formats", $dataFormat, array('%d', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%d', '%d')); $id = $wpdb->insert_id; } else { // otherwise, update the data in the table $sqlQuery = "select * FROM {$wpdb->prefix}a4barcode_custom_formats where id='%d'"; $preparedSql = $wpdb->prepare($sqlQuery, $id); $itemFormatData = $wpdb->get_row($preparedSql, ARRAY_A); // You can update records in the table if the 'default' parameter is 0 if ($itemFormatData && '0' == $itemFormatData['default']) { $result = $wpdb->update( "{$wpdb->prefix}a4barcode_custom_formats", $dataFormat, array('id' => $id), array('%d', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%d', '%d'), array('%d') ); } } // If the request was made with an error, then write an error message if (false === $result) { $error[] = 'Data was not saved.'; // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } } else { $success[] = 'Data successfully saved.'; } $response = array( 'id' => $id, 'success' => $success, 'error' => $error, ); a4bJsonResponse($response); } /** * Returns a list of all formats. */ public function getAllFormats() { global $wpdb; $error = array(); $sqlQuery = "select * FROM {$wpdb->prefix}a4barcode_custom_formats "; $results = $wpdb->get_results($sqlQuery, ARRAY_A); // get formats data // if the data array is empty, then remember the error if (0 == count($results)) { $error[] = 'No data found on request'; } // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } $response = array( 'listFormats' => $results, 'success' => array(), 'error' => $error, ); a4bJsonResponse($response); } /** * Returns format data by id. */ public function getFormat() { global $wpdb; $error = array(); $response = array(); $validationOptions = array( 'id' => 'required|numeric', ); // Checking for the correct data from the request. Return error if not valid. ValidatorPostData::validate($validationOptions, true); $id = PostData::$postData['id']; $sqlQuery = "select * FROM {$wpdb->prefix}a4barcode_custom_formats where id='%d'"; $preparedSql = $wpdb->prepare($sqlQuery, $id); $results = $wpdb->get_row($preparedSql, ARRAY_A); // get format data // Data is not empty if (!is_null($results)) { $response = array( 'name' => $results['name'], 'width' => $results['width'], 'height' => $results['height'], 'arround' => $results['arround'], 'across' => $results['across'], 'marginLeft' => $results['marginLeft'], 'marginRight' => $results['marginRight'], 'marginTop' => $results['marginTop'], 'marginBottom' => $results['marginBottom'], 'arroundCount' => $results['arroundCount'], 'acrossCount' => $results['acrossCount'], 'paperId' => $results['paperId'], 'default' => $results['default'], ); } else { $error[] = 'No data found on request'; $response['error'] = $error; } // If there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } a4bJsonResponse($response); } /** * Returns a list of all paper formats. */ public function getAllPaperFormats() { global $wpdb; $error = array(); $sqlQuery = "select * FROM {$wpdb->prefix}a4barcode_paper_formats "; $results = $wpdb->get_results($sqlQuery, ARRAY_A); // get paper formats data // if the data array is empty, then remember the error if (0 == count($results)) { $error[] = 'No data found on request'; } // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } $response = array( 'listFormats' => $results, 'success' => array(), 'error' => $error, ); a4bJsonResponse($response); } /** * the method updates the paper format data. */ public function savePaperFormat() { global $wpdb; $validationOptions = array( 'id' => 'numeric', 'name' => 'required', 'width' => 'required|numeric', 'height' => 'required|numeric', ); // Checking for the correct data from the request. Send error if not valid. ValidatorPostData::validate($validationOptions, true); $id = (isset(PostData::$postData['id'])) ? PostData::$postData['id'] : null; $dataFormat = array( 'name' => PostData::$postData['name'], 'width' => PostData::$postData['width'], 'height' => PostData::$postData['height'], ); $error = array(); $success = array(); $result = false; // if there is no data, then remember the error if (is_null($id)) { $result = $wpdb->insert("{$wpdb->prefix}a4barcode_paper_formats", $dataFormat, array('%s', '%f', '%f', '%d')); $id = $wpdb->insert_id; } else { $sqlQuery = "select * FROM {$wpdb->prefix}a4barcode_paper_formats where id='%d'"; $preparedSql = $wpdb->prepare($sqlQuery, $id); $paperFormatData = $wpdb->get_row($preparedSql, ARRAY_A); // You can update records in the table if the 'default' parameter is 0 if ($paperFormatData && '0' == $paperFormatData['default']) { $result = $wpdb->update("{$wpdb->prefix}a4barcode_paper_formats", $dataFormat, array('id' => $id), array('%s', '%f', '%f', '%s'), array('%d')); } } // If the request was made with an error, then write an error message if (false === $result) { $error[] = 'Data was not saved.'; // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } } else { $success[] = 'Data successfully saved.'; } $response = array( 'id' => $id, 'success' => $success, 'error' => $error, ); a4bJsonResponse($response); } /** * Returns formats data by paperId. */ public function getFormatsByPaper() { global $wpdb; $validationOptions = array( 'paperId' => 'required|numeric', ); // checking for the correct data from the request. Send error if not valid. ValidatorPostData::validate($validationOptions, true); $error = array(); $id = PostData::$postData['paperId']; $sqlQuery = "select * FROM {$wpdb->prefix}a4barcode_custom_formats where paperId='%d' "; $preparedSql = $wpdb->prepare($sqlQuery, $id); $results = $wpdb->get_results($preparedSql, ARRAY_A); // get formats data // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } $response = array( 'listFormats' => $results, 'success' => array(), 'error' => $error, ); a4bJsonResponse($response); } /** * Method for removing paper format and label formats which linked to it. */ public function deletePaperFormat() { global $wpdb; $result = null; $error = array(); $success = array(); $validationOptions = array( 'id' => 'required|numeric', ); // Checking for the correct data from the request. Return error if not valid. ValidatorPostData::validate($validationOptions, true); $id = PostData::$postData['id']; $result = $wpdb->delete("{$wpdb->prefix}a4barcode_paper_formats", array('id' => $id), array('%d')); // If the request was made with an error, then write an error message if (false === $result) { $error[] = __('Data was not deleted.', 'wpbcu-barcode-generator'); // if there is an error in the execution of the last request to the database if ('' !== $wpdb->last_error) { $error[] = $wpdb->last_error; } } else { $success[] = __('Data successfully deleted.', 'wpbcu-barcode-generator'); } $response = array( 'success' => $success, 'error' => $error, ); a4bJsonResponse($response); } }