wpdb = $wpdb; $this->tbl = $wpdb->prefix . Database::$tableGeneratedLabelsData; } public function save(&$labelsData, &$errorMsg = '') { $success = true; $placeholders = array(); $values = array(); if (!empty($labelsData)) { $lastOrderNumber = $this->getLastLabelOrderNumber(get_current_user_id()); foreach ($labelsData as &$labelData) { $labelData = $this->transformToDb($labelData); $labelData['order_number'] = ++$lastOrderNumber; if (empty($labelData)) { continue; } $placeholders[] = "(" . implode(',', array_fill(0, count($this->labelDataColumns), '%s')) . ")"; foreach ($this->labelDataColumns as $columnName) { $values[] = $labelData[$columnName]; } } $columnsListSqlPart = implode('`,`', $this->labelDataColumns); $insertResult = $this->wpdb->query($this->wpdb->prepare( "INSERT INTO {$this->tbl} (`$columnsListSqlPart`) VALUES " . implode(', ', $placeholders), $values )); if ($insertResult === false) { $success = false; $errorMsg .= esc_html__("Insert labels data error.", 'a4-barcode-generator'); } } return $success; } public function update($id, $labelData) { $updateResult = $this->wpdb->update($this->tbl, $this->transformToDb($labelData), array('id' => $id)); return $updateResult; } public function get($userId = null, $ids = null) { $dbRows = array(); if (empty($userId)) { $userId = get_current_user_id(); } $args = array($userId); if (is_array($ids) && !empty($ids)) { $idsCondition = " AND `id` IN(" . implode(', ', array_fill(0, count($ids), '%s')) . ")"; $args = array_merge($args, $ids); } else { $idsCondition = ''; } $limit = 100; $lastId = 0; do { $rows = $this->wpdb->get_results($this->wpdb->prepare( " SELECT * FROM {$this->tbl} WHERE `user_id` = %s $idsCondition AND `id` > %d ORDER BY `order_number` LIMIT %d", array_merge($args, array($lastId, $limit)) ) ); foreach ($rows as $row) { $dbRows[] = $this->transformToGeneratorResult($row); $lastId = max($lastId, $row->id); } } while (!empty($rows)); unset($rows); return $dbRows; } public function delete($params) { return $this->wpdb->delete($this->tbl, $params); } public function deleteByIdsAndUserId($params) { if (!empty($params['ids']) && !empty($params['user_id'])) { $placeholders = implode(', ', array_fill(0, count($params['ids']), '%d')); return $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$this->tbl} WHERE `id` IN ({$placeholders}) AND `user_id` = %d", array_merge($params['ids'], array($params['user_id'])) ) ); } else { return false; } } public function copy($id, $userId = null, $position = self::COPY_POSITION_EMPTY) { if (empty($userId)) { $userId = get_current_user_id(); } $row = $this->wpdb->get_row($this->wpdb->prepare( "SELECT * FROM {$this->tbl} WHERE `id` = %s AND `user_id` = %s", array($id, $userId) ), ARRAY_A); if ($row) { unset($row['id']); if ($position === self::COPY_POSITION_EMPTY) { $row['order_number'] = $this->getLastLabelOrderNumber($userId) + 1; } $this->wpdb->insert($this->tbl, $row); if ($this->wpdb->insert_id) { return $this->wpdb->insert_id; } else { return false; } } else { return false; } } public function getLastLabelOrderNumber($userId) { $dbRow = $this->wpdb->get_row($this->wpdb->prepare( "SELECT * FROM {$this->tbl} WHERE `user_id` = %s ORDER BY `order_number` DESC", $userId )); return !empty($dbRow) ? $dbRow->order_number : 0; } protected function transformToDb($labelData) { if (!empty($labelData['replacements']) && !is_string($labelData['replacements'])) { $labelData['replacements'] = wp_json_encode($labelData['replacements']); } return $labelData; } protected function transformToGeneratorResultBatch($dbRows) { foreach ($dbRows as &$dbRow) { $dbRow = $this->transformToGeneratorResult($dbRow); } return $dbRows; } public function transformToGeneratorResult($dbRow) { $dbRow->replacements = json_decode($dbRow->replacements, true); return $dbRow; } }