PluginProbe
CSS & JavaScript Toolbox / 12.0.1
CSS & JavaScript Toolbox v12.0.1
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / models / template.php

template.php in CSS & JavaScript Toolbox 12.0.1, at models/template.php

237 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6 // Disllow direct access.
7 defined('ABSPATH') or die('Access denied');
8
9 /**
10 *
11 */
12 class CJTTemplateModel extends CJTHookableClass {
13
14 /**
15 * put your comment there...
16 *
17 * @var mixed
18 */
19 public $inputs;
20
21 /**
22 * put your comment there...
23 *
24 * @var mixed
25 */
26 protected $onloadcodefile = array('parameters' => array('code', 'item'));
27
28 /**
29 * put your comment there...
30 *
31 * @var mixed
32 */
33 protected $onqueryitem = array('parameters' => array('query'));
34
35 /**
36 * put your comment there...
37 *
38 * @param mixed $code
39 */
40 public function decryptCode($encodedCode) {
41 /// base 64 code!
42 $code64 = str_replace(array('<?php defined("ABS_PATH") or die("Access Denied"); \'', '\' ?>'), '', $encodedCode);
43 // Decode!
44 $code = base64_decode($code64);
45 return $code;
46 }
47
48 /**
49 * put your comment there...
50 *
51 * @param mixed $code
52 */
53 public function encryptCode($code) {
54 // Base 64 Encode!
55 $code64 = base64_encode($code);
56 // Hide inside PHP tags!
57 $encodedCode = "<?php defined(\"ABS_PATH\") or die(\"Access Denied\"); '{$code64}'; ?>";
58 return $encodedCode;
59 }
60
61 /**
62 * put your comment there...
63 *
64 * @param mixed $name
65 */
66 public function exists($name) {
67 // Find by name.
68 $this->inputs['filter']['field'] = 'name';
69 $this->inputs['filter']['value'] = $name;
70 // Template object if exists with E_ALL complain!
71 return (($existsItem = $this->getTemplateBy()) && property_exists($existsItem, 'id') && $existsItem->id) ?
72 $existsItem : FALSE;
73 }
74
75 /**
76 * put your comment there...
77 *
78 */
79 public function getItem() {
80 $tManager = CJTModel::create('templates-manager');
81 $query = $tManager->getItemsQuery();
82 $query['select'] = 'SELECT t.id,
83 t.queueName,
84 t.name,
85 t.type,
86 t.description,
87 t.creationDate,
88 t.keywords,
89 r.dateCreated lastModified,
90 t.state,
91 a.name author,
92 r.changeLog,
93 r.version,
94 r.file,
95 r.state developmentState';
96 $query['where'] .= " AND t.id = {$this->inputs['id']}";
97 // Filering!
98 $query = $this->onqueryitem($query);
99 // Build and query!
100 $query = "{$query['select']} {$query['from']} {$query['where']}";
101 $item = cssJSToolbox::getInstance()->getDBDriver()->getRow($query);
102 // Get code.
103 $code = file_get_contents(ABSPATH . "/{$item->file}");
104 // Decode if its a PHP template!
105 if ($item->type == 'php') {
106 $code = $this->decryptCode($code);
107 }
108 $item->code = $this->onloadcodefile($code, $item);
109 // Return PHP StdClass object.
110 return $item;
111 }
112
113 /**
114 * Query Block based on the passed paramaters.
115 *
116 */
117 public function getTemplateBy() {
118 // import dependencies.
119 cssJSToolbox::import('framework:db:mysql:xtable.inc.php');
120 return CJTxTable::getInstance('template')
121 ->set($this->inputs['filter']['field'], $this->inputs['filter']['value'])
122 ->load(array($this->inputs['filter']['field']))
123 ->getData();
124 }
125
126 /**
127 * put your comment there...
128 *
129 */
130 public function save() {
131 // import libraries.
132 cssJSToolbox::import('framework:db:mysql:xtable.inc.php');
133 // Initialize vars.
134 $fSConfig = cssJSToolbox::$config->fileSystem;
135 $currentUser = get_userdata(get_current_user_id());
136 $dbDriver = cssJSToolbox::getInstance()->getDBDriver();
137 $saveId = isset($this->inputs['item']['template']['id']) ? $this->inputs['item']['template']['id'] : null;
138 // Load template data is exists (load), change values (setData).
139 $template = CJTxTable::getInstance('template');
140 if ($saveId) {
141 $template->set('id', $saveId)->load();
142 }
143 $template->setData($this->inputs['item']['template'])
144 ->setQueueName();
145 $templateDirName = $template->get('queueName');
146 $contentDir = end( explode( DIRECTORY_SEPARATOR, dirname( dirname( plugin_dir_path( CJTOOLBOX_PLUGIN_FILE ) ) ) ) );
147 $templateDir = "$contentDir/{$fSConfig->contentDir}/{$fSConfig->templatesDir}/{$templateDirName}";
148 if (!$template->get('id')) { // Add new Template
149 // Search for author for the current local Wordpress user.
150 // If not created in the Authors table create one! If created get the ID!
151 $author = CJTxTable::getInstance('author', null, "SELECT * FROM #__cjtoolbox_authors
152 WHERE name = '{$currentUser->user_login}'
153 AND (attributes & 2);");
154 // Create Wordpress user in Authors table.
155 if (!$author->get('id')) {
156 $author->setData(array(
157 'name' => $currentUser->user_login,
158 'email' => $currentUser->user_email,
159 'url' => $currentUser->user_url,
160 'attributes' => 2 // 2 For LOCAL AUTHORS!!
161 ))->save();
162 }
163 // Set template data
164 $template->set('ownerId', $currentUser->ID)
165 ->set('creationDate', current_time('mysql'))
166 ->set('authorId', $author->get('id'));
167 }
168 // Make sure directory created even if updating templats.
169 // This case is needed when revisioned built-in templates (e.g jquery)!!
170 if (!file_exists(ABSPATH . "/{$templateDir}")) {
171 mkdir(ABSPATH . "/{$templateDir}", 0755);
172 }
173 // Save template.
174 if (!$template->save()->get('id')) {
175 throw new Exception('Error saving template into database!!!');
176 }
177 /// Always create new revision. ///
178 // Get last used Revision Number!
179 $lastRevisionNo = ((int) ($dbDriver->getRow("SELECT max(revisionNo) revisionNo
180 FROM #__cjtoolbox_template_revisions
181 WHERE templateId = {$template->get('id')}")->revisionNo));
182 // Checki if there is a previous revision and if there is changes!!
183 $lastRevision = CJTxTable::getInstance('template-revision', null,
184 "SELECT *
185 FROM #__cjtoolbox_template_revisions
186 WHERE templateId = {$template->get('id')} AND revisionNo = {$lastRevisionNo}"
187 );
188 // Only add Revision if it has any field changed!
189 $revisionData = $this->inputs['item']['revision'];
190 $lastRevisionCode = $lastRevision->get('id') ? file_get_contents(ABSPATH . "/{$lastRevision->get('file')}") : null;
191 $revisionHasChanges = $lastRevisionCode !== $revisionData['code']
192 || $lastRevision->get('state') !== $revisionData['state']
193 || $lastRevision->get('version') !== $revisionData['version']
194 || $lastRevision->get('changeLog') !== $revisionData['changeLog'];
195 if ($revisionHasChanges) {
196 $templateType = $template->get('type');
197 // New added revision number!
198 $revisionNo = $lastRevisionNo + 1;
199 // Get template revision extension.
200 $extension = cssJSToolbox::$config->templates->types[$templateType]->extension;
201 // Remove fields that not part of the revision table!
202 $code = $this->inputs['item']['revision']['code'];
203 unset($this->inputs['item']['revision']['code']);
204 // Creae revision object.
205 $revision = CJTxTable::getInstance('template-revision')
206 ->setData($this->inputs['item']['revision'])
207 ->set('templateId', $template->get('id'))
208 ->set('revisionNo', $revisionNo)
209 ->set('dateCreated', current_time('mysql'))
210 ->set('attributes', CJTTemplateRevisionTable::FLAG_LAST_REVISION) // Mark as last revision.
211 ->set('file', "{$templateDir}/{$revisionNo}.{$extension}")
212 ->save();
213 // Encrypt PHP codes!
214 if ($templateType == 'php') {
215 $code = $this->encryptCode($code);
216 }
217 // Write revision content into Disk File!
218 file_put_contents(ABSPATH . "/{$revision->get('file')}", $code);
219 // Remove FLAG_LAST_REVISION flag from last revision so
220 // our new revision will be the last one!!
221 if ($lastRevision->get('id')) {
222 $flagsOff = $lastRevision->get('attributes') & (~CJTTemplateRevisionTable::FLAG_LAST_REVISION);
223 $lastRevision->set('attributes', $flagsOff)->save();
224 }
225 }
226 else {
227 // Return last Revision!
228 $revision = $lastRevision;
229 }
230 // Return revision object.
231 return $revision->getData();
232 }
233
234 } // End class.
235
236 // Hookable!
237 CJTTemplateModel::define('CJTTemplateModel', array('hookType' => CJTWordpressEvents::HOOK_FILTER));