| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disllow direct access. |
| 7 |
defined('ABSPATH') or die('Access denied'); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
class CJTTemplatesManagerModel { |
| 13 |
|
| 14 |
/** |
| 15 |
* put your comment there... |
| 16 |
* |
| 17 |
* @var mixed |
| 18 |
*/ |
| 19 |
public $inputs; |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
*/ |
| 25 |
public function changeState() { |
| 26 |
$ids = implode(',', $this->inputs['ids']); |
| 27 |
// initialize vars. |
| 28 |
cssJSToolbox::getInstance()->getDBDriver() |
| 29 |
->update("UPDATE #__cjtoolbox_templates |
| 30 |
SET `state` = '{$this->inputs['state']}' |
| 31 |
WHERE id IN ({$ids})") |
| 32 |
->processQueue(); |
| 33 |
return $this; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* put your comment there... |
| 38 |
* |
| 39 |
*/ |
| 40 |
public function delete() { |
| 41 |
// Initialize. |
| 42 |
WP_Filesystem(); |
| 43 |
// initialize vars. |
| 44 |
$wpFileSystem =& $GLOBALS['wp_filesystem']; |
| 45 |
$ids = array(); |
| 46 |
$dbDriver = cssJSToolbox::getInstance()->getDBDriver(); |
| 47 |
$fsConfig = cssJSToolbox::$config->fileSystem; |
| 48 |
// Import dependencies. |
| 49 |
cssJSToolbox::import('framework:db:mysql:xtable.inc.php'); |
| 50 |
CJTxTable::import('template'); |
| 51 |
// Delete only templates in "trash" state! |
| 52 |
// For more security don't even delete templates with SYSTEM Attribute flag |
| 53 |
// is turned ON! |
| 54 |
$sysFlag = CJTTemplateTable::ATTRIBUTES_SYSTEM_FLAG; |
| 55 |
$idsQueryList = implode(',', $this->inputs['ids']); |
| 56 |
$templates = $dbDriver->select("SELECT id, queueName `directory` |
| 57 |
FROM #__cjtoolbox_templates |
| 58 |
WHERE ID IN ({$idsQueryList}) AND ((attributes & {$sysFlag}) = 0) AND (`state` = 'trash')"); |
| 59 |
if (!empty($templates)) { |
| 60 |
// Deleing template directory files. |
| 61 |
foreach ($templates as $template) { |
| 62 |
// Absolute path to template directory! |
| 63 |
$templateDirectoryAbsPath = WP_CONTENT_DIR . "/{$fsConfig->contentDir}/{$fsConfig->templatesDir}/{$template->directory}"; |
| 64 |
// Delete template directory RECUSIVLY! |
| 65 |
$wpFileSystem->rmdir($templateDirectoryAbsPath, true); |
| 66 |
} |
| 67 |
// Get templates IDs to delete. |
| 68 |
$ids = implode(', ', array_keys($templates)); |
| 69 |
// Permenantly delete all templates data from |
| 70 |
// templates table and all refernced tables. |
| 71 |
$dbDriver->startTransaction() |
| 72 |
->delete("DELETE FROM #__cjtoolbox_block_templates WHERE templateId IN ({$ids})") |
| 73 |
->delete("DELETE FROM #__cjtoolbox_template_revisions WHERE templateId IN ({$ids})") |
| 74 |
->delete("DELETE FROM #__cjtoolbox_templates WHERE id IN ({$ids})") |
| 75 |
->commit() |
| 76 |
->processQueue(); |
| 77 |
} |
| 78 |
return $ids; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* put your comment there... |
| 83 |
* |
| 84 |
*/ |
| 85 |
public function getItems() { |
| 86 |
// Build query. |
| 87 |
$select = 'SELECT t.id, |
| 88 |
t.name, |
| 89 |
t.type, |
| 90 |
t.description, |
| 91 |
t.creationDate, |
| 92 |
t.state, |
| 93 |
a.name author, |
| 94 |
r.dateCreated lastModified, |
| 95 |
r.version, |
| 96 |
r.state developmentState'; |
| 97 |
$queryBase = $this->getItemsQuery(); |
| 98 |
// Paging. |
| 99 |
$itemsPerPage = $this->getItemsPerPage(); |
| 100 |
// Get page no#. |
| 101 |
$page = !isset($this->inputs['paged']) ? 1 : $this->inputs['paged']; |
| 102 |
// Calculate start offset. |
| 103 |
$start = ($page - 1) * $itemsPerPage; |
| 104 |
$limit = " LIMIT {$start},{$itemsPerPage}"; |
| 105 |
// Order. |
| 106 |
$orderBy = isset($this->inputs['orderby']) ? " ORDER BY {$this->inputs['orderby']} {$this->inputs['order']}" : ''; |
| 107 |
// final query. |
| 108 |
$query = "{$select}{$queryBase['from']}{$queryBase['where']}{$orderBy}{$limit}"; |
| 109 |
// Execute our query using MYSQL queue driver. |
| 110 |
$result = cssJSToolbox::getInstance()->getDBDriver()->select($query); |
| 111 |
return $result; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* put your comment there... |
| 116 |
* |
| 117 |
*/ |
| 118 |
public function getItemsPerPage() { |
| 119 |
return 20; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* put your comment there... |
| 124 |
* |
| 125 |
*/ |
| 126 |
public function getItemsQuery() { |
| 127 |
// Import dependencies. |
| 128 |
cssJSToolbox::import('framework:db:mysql:xtable.inc.php'); |
| 129 |
CJTxTable::import('template-revision'); |
| 130 |
CJTxTable::import('author'); |
| 131 |
// From clause. |
| 132 |
$query['from'] = ' FROM #__cjtoolbox_templates t |
| 133 |
LEFT JOIN #__cjtoolbox_template_revisions r ON t.id = r.templateId |
| 134 |
LEFT JOIN #__cjtoolbox_authors a ON t.authorId = a.id'; |
| 135 |
// Always get only the last revision. |
| 136 |
$where[] = '(r.attributes & ' . CJTTemplateRevisionTable::FLAG_LAST_REVISION . ')'; |
| 137 |
// For version 6 don't display Internal/System (e.g Wordpress, ) Authors templates |
| 138 |
$where[] = '((a.attributes & ' . CJTAuthorTable::FLAG_SYS_AUTHOR . ') = 0)'; |
| 139 |
// Build where clause based on the given filters! |
| 140 |
$filters = array( |
| 141 |
'types' => array('table' => 't', 'name' =>'type'), |
| 142 |
'authors' => array('table' => 't', 'name' => 'authorId'), |
| 143 |
'version' => array('table' => 'r', 'name' => 'version'), |
| 144 |
'creation-dates' => array( 'name' => 'DATE(creationDate)'), |
| 145 |
'last-modified-dates' => array('name' => 'DATE(dateCreated)'), |
| 146 |
'states' => array('table' => 't', 'name' => 'state'), |
| 147 |
'development-state' => array('table' => 'r', 'name' => 'state'), |
| 148 |
); |
| 149 |
foreach ($filters as $name => $field) { |
| 150 |
$filterName = "filter_{$name}"; |
| 151 |
// Add filter only if there is a value specified. |
| 152 |
if (!empty($this->inputs[$filterName])) { |
| 153 |
$value = $this->inputs[$filterName]; |
| 154 |
if (!is_numeric($value)) { |
| 155 |
$value = "'{$value}'"; |
| 156 |
} |
| 157 |
$field['table'] = isset($field['table']) ? "{$field['table']}." : ''; |
| 158 |
$where[] = "{$field['table']}{$field['name']} = {$value} "; |
| 159 |
} |
| 160 |
} |
| 161 |
$query['where'] = ' WHERE ' . implode(' AND ', $where); |
| 162 |
return $query; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* put your comment there... |
| 167 |
* |
| 168 |
*/ |
| 169 |
public function getItemsTotal() { |
| 170 |
$queryBase = $this->getItemsQuery(); |
| 171 |
$select = 'SELECT count(*) Total'; |
| 172 |
$query = "{$select}{$queryBase['from']}{$queryBase['where']}"; |
| 173 |
// Get items total. |
| 174 |
$dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']); |
| 175 |
$result = $dbDriver->select($query); |
| 176 |
return array_shift($result)->Total; |
| 177 |
} |
| 178 |
|
| 179 |
} // End class. |