first commit
This commit is contained in:
65
admininistrator/model/tool/backup.php
Normal file
65
admininistrator/model/tool/backup.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace Opencart\Admin\Model\Tool;
|
||||
/**
|
||||
* Class Backup
|
||||
*
|
||||
* @package Opencart\Admin\Model\Tool
|
||||
*/
|
||||
class Backup extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTables(): array {
|
||||
$table_data = [];
|
||||
|
||||
$query = $this->db->query("SHOW TABLES FROM `" . DB_DATABASE . "`");
|
||||
|
||||
foreach ($query->rows as $result) {
|
||||
if (isset($result['Tables_in_' . DB_DATABASE]) && substr($result['Tables_in_' . DB_DATABASE], 0, strlen(DB_PREFIX)) == DB_PREFIX) {
|
||||
$table_data[] = $result['Tables_in_' . DB_DATABASE];
|
||||
}
|
||||
}
|
||||
|
||||
return $table_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param int $start
|
||||
* @param int $limit
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRecords(string $table, int $start = 0, int $limit = 100): array {
|
||||
if ($start < 0) {
|
||||
$start = 0;
|
||||
}
|
||||
|
||||
if ($limit < 1) {
|
||||
$limit = 10;
|
||||
}
|
||||
|
||||
$query = $this->db->query("SELECT * FROM `" . $table . "` LIMIT " . (int)$start . "," . (int)$limit);
|
||||
|
||||
if ($query->num_rows) {
|
||||
return $query->rows;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalRecords(string $table): int {
|
||||
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . $table . "`");
|
||||
|
||||
if ($query->num_rows) {
|
||||
return (int)$query->row['total'];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
61
admininistrator/model/tool/image.php
Normal file
61
admininistrator/model/tool/image.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace Opencart\Admin\Model\Tool;
|
||||
/**
|
||||
* Class Image
|
||||
*
|
||||
* @package Opencart\Admin\Model\Tool
|
||||
*/
|
||||
class Image extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function resize(string $filename, int $width, int $height): string {
|
||||
if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
|
||||
$image_old = $filename;
|
||||
$image_new = 'cache/' . oc_substr($filename, 0, oc_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
|
||||
|
||||
if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) {
|
||||
list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);
|
||||
|
||||
if (!in_array($image_type, [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_WEBP])) {
|
||||
return HTTP_CATALOG . 'image/' . $image_old;
|
||||
}
|
||||
|
||||
$path = '';
|
||||
|
||||
$directories = explode('/', dirname($image_new));
|
||||
|
||||
foreach ($directories as $directory) {
|
||||
if (!$path) {
|
||||
$path = $directory;
|
||||
} else {
|
||||
$path = $path . '/' . $directory;
|
||||
}
|
||||
|
||||
if (!is_dir(DIR_IMAGE . $path)) {
|
||||
@mkdir(DIR_IMAGE . $path, 0777);
|
||||
}
|
||||
}
|
||||
|
||||
if ($width_orig != $width || $height_orig != $height) {
|
||||
$image = new \Opencart\System\Library\Image(DIR_IMAGE . $image_old);
|
||||
$image->resize($width, $height);
|
||||
$image->save(DIR_IMAGE . $image_new);
|
||||
} else {
|
||||
copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
|
||||
}
|
||||
}
|
||||
|
||||
return HTTP_CATALOG . 'image/' . $image_new;
|
||||
}
|
||||
}
|
97
admininistrator/model/tool/notification.php
Normal file
97
admininistrator/model/tool/notification.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
namespace Opencart\Admin\Model\Tool;
|
||||
/**
|
||||
* Class Notification
|
||||
*
|
||||
* @package Opencart\Admin\Model\Tool
|
||||
*/
|
||||
class Notification extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function addNotification(array $data): int {
|
||||
$this->db->query("INSERT INTO `" . DB_PREFIX . "notification` SET `title` = '" . $this->db->escape((string)$data['title']) . "', `text` = '" . $this->db->escape((string)$data['text']) . "', `status` = '" . (bool)$data['status'] . "', `date_added` = NOW()");
|
||||
|
||||
return $this->db->getLastId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $notification_id
|
||||
* @param bool $status
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function editStatus(int $notification_id, bool $status): void {
|
||||
$this->db->query("UPDATE `" . DB_PREFIX . "notification` SET `status` = '" . (bool)$status . "' WHERE `notification_id` = '" . (int)$notification_id . "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $notification_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleteNotification(int $notification_id): void {
|
||||
$this->db->query("DELETE FROM `" . DB_PREFIX . "notification` WHERE `notification_id` = '" . (int)$notification_id . "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $notification_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNotification(int $notification_id): array {
|
||||
$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "notification` WHERE `notification_id` = '" . (int)$notification_id . "'");
|
||||
|
||||
return $query->row;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNotifications(array $data = []): array {
|
||||
$sql = "SELECT * FROM `" . DB_PREFIX . "notification`";
|
||||
|
||||
if (isset($data['filter_status']) && $data['filter_status'] !== '') {
|
||||
$sql .= " WHERE `status` = '" . (bool)$data['filter_status'] . "'";
|
||||
}
|
||||
|
||||
$sql .= " ORDER BY `date_added` DESC";
|
||||
|
||||
if (isset($data['start']) || isset($data['limit'])) {
|
||||
if ($data['start'] < 0) {
|
||||
$data['start'] = 0;
|
||||
}
|
||||
|
||||
if ($data['limit'] < 1) {
|
||||
$data['limit'] = 20;
|
||||
}
|
||||
|
||||
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
||||
}
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
return $query->rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalNotifications(array $data = []): int {
|
||||
$sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "notification`";
|
||||
|
||||
if (isset($data['filter_status']) && $data['filter_status'] !== '') {
|
||||
$sql .= " WHERE `status` = '" . (bool)$data['filter_status'] . "'";
|
||||
}
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
return (int)$query->row['total'];
|
||||
}
|
||||
}
|
153
admininistrator/model/tool/upload.php
Normal file
153
admininistrator/model/tool/upload.php
Normal file
@ -0,0 +1,153 @@
|
||||
<?php
|
||||
namespace Opencart\Admin\Model\Tool;
|
||||
/**
|
||||
* Class Upload
|
||||
*
|
||||
* @package Opencart\Admin\Model\Tool
|
||||
*/
|
||||
class Upload extends \Opencart\System\Engine\Model {
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function addUpload(string $name, string $filename): string {
|
||||
$code = oc_token(32);
|
||||
|
||||
$this->db->query("INSERT INTO `" . DB_PREFIX . "upload` SET `name` = '" . $this->db->escape($name) . "', `filename` = '" . $this->db->escape($filename) . "', `code` = '" . $this->db->escape($code) . "', `date_added` = NOW()");
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $upload_id
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deleteUpload(int $upload_id): void {
|
||||
$this->db->query("DELETE FROM `" . DB_PREFIX . "upload` WHERE `upload_id` = '" . (int)$upload_id . "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $upload_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUpload(int $upload_id): array {
|
||||
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "upload` WHERE `upload_id` = '" . (int)$upload_id . "'");
|
||||
|
||||
return $query->row;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUploadByCode(string $code): array {
|
||||
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "upload` WHERE `code` = '" . $this->db->escape($code) . "'");
|
||||
|
||||
return $query->row;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUploads(array $data = []): array {
|
||||
$sql = "SELECT * FROM `" . DB_PREFIX . "upload`";
|
||||
|
||||
$implode = [];
|
||||
|
||||
if (!empty($data['filter_name'])) {
|
||||
$implode[] = "`name` LIKE '" . $this->db->escape((string)$data['filter_name'] . '%') . "'";
|
||||
}
|
||||
|
||||
if (!empty($data['filter_code'])) {
|
||||
$implode[] = "`code` LIKE '" . $this->db->escape((string)$data['filter_code'] . '%') . "'";
|
||||
}
|
||||
|
||||
if (!empty($data['filter_date_from'])) {
|
||||
$implode[] = "DATE(`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_from']) . "')";
|
||||
}
|
||||
|
||||
if (!empty($data['filter_date_to'])) {
|
||||
$implode[] = "DATE(`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_to']) . "')";
|
||||
}
|
||||
|
||||
if ($implode) {
|
||||
$sql .= " WHERE " . implode(" AND ", $implode);
|
||||
}
|
||||
|
||||
$sort_data = [
|
||||
'name',
|
||||
'code',
|
||||
'date_added'
|
||||
];
|
||||
|
||||
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
||||
$sql .= " ORDER BY " . $data['sort'];
|
||||
} else {
|
||||
$sql .= " ORDER BY `date_added`";
|
||||
}
|
||||
|
||||
if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
||||
$sql .= " DESC";
|
||||
} else {
|
||||
$sql .= " ASC";
|
||||
}
|
||||
|
||||
if (isset($data['start']) || isset($data['limit'])) {
|
||||
if ($data['start'] < 0) {
|
||||
$data['start'] = 0;
|
||||
}
|
||||
|
||||
if ($data['limit'] < 1) {
|
||||
$data['limit'] = 20;
|
||||
}
|
||||
|
||||
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
||||
}
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
return $query->rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalUploads(array $data = []): int {
|
||||
$sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "upload`";
|
||||
|
||||
$implode = [];
|
||||
|
||||
if (!empty($data['filter_name'])) {
|
||||
$implode[] = "`name` LIKE '" . $this->db->escape((string)$data['filter_name'] . '%') . "'";
|
||||
}
|
||||
|
||||
if (!empty($data['filter_code'])) {
|
||||
$implode[] = "`code` LIKE '" . $this->db->escape((string)$data['filter_code'] . '%') . "'";
|
||||
}
|
||||
|
||||
if (!empty($data['filter_date_from'])) {
|
||||
$implode[] = "DATE(`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_from']) . "')";
|
||||
}
|
||||
|
||||
if (!empty($data['filter_date_to'])) {
|
||||
$implode[] = "DATE(`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_to']) . "')";
|
||||
}
|
||||
|
||||
if ($implode) {
|
||||
$sql .= " WHERE " . implode(" AND ", $implode);
|
||||
}
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
return (int)$query->row['total'];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user