PluginProbe
CSS & JavaScript Toolbox / 9.0
CSS & JavaScript Toolbox v9.0
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 9.0, at models/template.php

236 lines 7.8 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 $templateDir = content_url( "{$fSConfig->contentDir}/{$fSConfig->templatesDir}/{$templateDirName}" );
147 if (!$template->get('id')) { // Add new Template
148 // Search for author for the current local Wordpress user.
149 // If not created in the Authors table create one! If created get the ID!
150 $author = CJTxTable::getInstance('author', null, "SELECT * FROM #__cjtoolbox_authors
151 WHERE name = '{$currentUser->user_login}'
152 AND (attributes & 2);");
153 // Create Wordpress user in Authors table.
154 if (!$author->get('id')) {
155 $author->setData(array(
156 'name' => $currentUser->user_login,
157 'email' => $currentUser->user_email,
158 'url' => $currentUser->user_url,
159 'attributes' => 2 // 2 For LOCAL AUTHORS!!
160 ))->save();
161 }
162 // Set template data
163 $template->set('ownerId', $currentUser->ID)
164 ->set('creationDate', current_time('mysql'))
165 ->set('authorId', $author->get('id'));
166 }
167 // Make sure directory created even if updating templats.
168 // This case is needed when revisioned built-in templates (e.g jquery)!!
169 if (!file_exists(ABSPATH . "/{$templateDir}")) {
170 mkdir(ABSPATH . "/{$templateDir}", 0755);
171 }
172 // Save template.
173 if (!$template->save()->get('id')) {
174 throw new Exception('Error saving template into database!!!');
175 }
176 /// Always create new revision. ///
177 // Get last used Revision Number!
178 $lastRevisionNo = ((int) ($dbDriver->getRow("SELECT max(revisionNo) revisionNo
179 FROM #__cjtoolbox_template_revisions
180 WHERE templateId = {$template->get('id')}")->revisionNo));
181 // Checki if there is a previous revision and if there is changes!!
182 $lastRevision = CJTxTable::getInstance('template-revision', null,
183 "SELECT *
184 FROM #__cjtoolbox_template_revisions
185 WHERE templateId = {$template->get('id')} AND revisionNo = {$lastRevisionNo}"
186 );
187 // Only add Revision if it has any field changed!
188 $revisionData = $this->inputs['item']['revision'];
189 $lastRevisionCode = $lastRevision->get('id') ? file_get_contents(ABSPATH . "/{$lastRevision->get('file')}") : null;
190 $revisionHasChanges = $lastRevisionCode !== $revisionData['code']
191 || $lastRevision->get('state') !== $revisionData['state']
192 || $lastRevision->get('version') !== $revisionData['version']
193 || $lastRevision->get('changeLog') !== $revisionData['changeLog'];
194 if ($revisionHasChanges) {
195 $templateType = $template->get('type');
196 // New added revision number!
197 $revisionNo = $lastRevisionNo + 1;
198 // Get template revision extension.
199 $extension = cssJSToolbox::$config->templates->types[$templateType]->extension;
200 // Remove fields that not part of the revision table!
201 $code = $this->inputs['item']['revision']['code'];
202 unset($this->inputs['item']['revision']['code']);
203 // Creae revision object.
204 $revision = CJTxTable::getInstance('template-revision')
205 ->setData($this->inputs['item']['revision'])
206 ->set('templateId', $template->get('id'))
207 ->set('revisionNo', $revisionNo)
208 ->set('dateCreated', current_time('mysql'))
209 ->set('attributes', CJTTemplateRevisionTable::FLAG_LAST_REVISION) // Mark as last revision.
210 ->set('file', "{$templateDir}/{$revisionNo}.{$extension}")
211 ->save();
212 // Encrypt PHP codes!
213 if ($templateType == 'php') {
214 $code = $this->encryptCode($code);
215 }
216 // Write revision content into Disk File!
217 file_put_contents(ABSPATH . "/{$revision->get('file')}", $code);
218 // Remove FLAG_LAST_REVISION flag from last revision so
219 // our new revision will be the last one!!
220 if ($lastRevision->get('id')) {
221 $flagsOff = $lastRevision->get('attributes') & (~CJTTemplateRevisionTable::FLAG_LAST_REVISION);
222 $lastRevision->set('attributes', $flagsOff)->save();
223 }
224 }
225 else {
226 // Return last Revision!
227 $revision = $lastRevision;
228 }
229 // Return revision object.
230 return $revision->getData();
231 }
232
233 } // End class.
234
235 // Hookable!
236 CJTTemplateModel::define('CJTTemplateModel', array('hookType' => CJTWordpressEvents::HOOK_FILTER));