PluginProbe
CSS & JavaScript Toolbox / 7.2
CSS & JavaScript Toolbox v7.2
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 7.2, at models/template.php

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