PluginProbe
CSS & JavaScript Toolbox / 7.1.2
CSS & JavaScript Toolbox v7.1.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 / blocks.php

blocks.php in CSS & JavaScript Toolbox 7.1.2, at models/blocks.php

348 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version $ Id; blocks.php 21-03-2012 03:22:10 Ahmed Said $
4 */
5
6 /**
7 * No direct access.
8 */
9 defined('ABSPATH') or die("Access denied");
10
11 // Blocks Database tables.
12 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
13 require_once CJTOOLBOX_TABLES_PATH . '/block-pins.php';
14 // MYSQL Queue Driver.
15 require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/queue-driver.inc.php';
16
17 /**
18 * Provide simple access (read or write) to all Blocks data.
19 *
20 * @author Ahmed Said
21 * @version 6
22 */
23 class CJTBlocksModel {
24
25 /**
26 *
27 */
28 const MAX_REVISIONS_PER_BLOCK = 10;
29
30 /**
31 * put your comment there...
32 *
33 * @var mixed
34 */
35 protected $dbDriver = null;
36
37 /**
38 * put your comment there...
39 *
40 */
41 public function __construct() {
42 // Initialize CTTTable MYSQL Driver.
43 $this->dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']);
44 }
45
46 /**
47 * put your comment there...
48 *
49 * @param mixed $data
50 * @param mixed $pins
51 * @param mixed $customPins
52 */
53 public function add($block) {
54 // Make sure it array and not stdClass as it has been used from various areas!
55 $block = (array) $block;
56 // Create Tables objects.
57 $blocks = new CJTBlocksTable($this->dbDriver);
58 $codeFile = new CJTBlockFilesTable($this->dbDriver);
59 // Get new id if not specified.
60 if (!isset($block['id']) || !$block['id']) {
61 $block['id'] = $blocks->getNextId();
62 }
63 // Cache code for MASTER file.
64 $codeFile->set('code', (isset($block['code']) ? $block['code'] : null));
65 unset($block['code']); // If it passed it mist be removed from blocks data(avoid field not found)
66 // Add new block.
67 $blocks->insert($block);
68 // Add code files.
69 $codeFile->set('blockId', $block['id'])
70 ->set('id', 1)
71 ->set('name', 'Master')
72 ->save(true, true);
73 // Return Newly added block id.
74 return $block['id'];
75 }
76
77 /**
78 * put your comment there...
79 *
80 * @param mixed $blockId
81 * @param mixed $activeFileId
82 */
83 public function addRevision($blockId, $activeFileId) {
84 // Create Tables objects.
85 $blocks = new CJTBlocksTable($this->dbDriver);
86 $pins = new CJTBlockPinsTable($this->dbDriver);
87 $codeFile = new CJTBlockFilesTable($this->dbDriver);
88 // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per
89 // block code files So that a single block may has up to
90 // self::MAX_REVISIONS_PER_BLOCK * count(codeFiles)
91 $revisions['fields'] = array('id');
92 $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId);
93 $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']);
94 // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one.
95 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
96 $this->delete(array_shift($revisions)->id);
97 }
98 // Get block data.
99 $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions');
100 // get() developed to return multiple blocks, fetch the first.
101 $result = $blocks->get($blockId, $block['fields']);
102 $block = reset($result);
103 // Set other fields.
104 $block->location = $block->state = '';
105 $block->parent = $blockId;
106 $block->type = 'revision';
107 $block->created = current_time('mysql');
108 $block->owner = get_current_user_id();
109 $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be
110 // used as the masterFile!
111 $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
112 // Add block data.
113 $blocks->insert($block);
114 // Get block pins and insert pins for the revision block.
115 $blockPins = $pins->get($blockId);
116 if (!empty($blockPins)) {
117 $pins->insertRaw($block->id, $blockPins);
118 }
119 // Revision current ActiveFileId code record.
120 // Simply, get a copy of it from the target block
121 // and assign the copy to the new created block revision.
122 $codeFile->set('id', $activeFileId)
123 ->set('blockId', $blockId)
124 ->load()
125 ->set('blockId', $block->id)
126 ->save(true, true);
127 }
128
129 /**
130 * Put your comments here...
131 *
132 *
133 * @return
134 */
135 public function dbDriver() {
136 return $this->dbDriver;
137 }
138
139 /**
140 * put your comment there...
141 *
142 * @param mixed $ids
143 */
144 public function delete($ids) {
145 // Allow single or multiple Ids to be passed.
146 if (!is_array($ids)) {
147 $ids = array($ids);
148 }
149 // Create Tables objects.
150 $blocks = new CJTBlocksTable($this->dbDriver);
151 $pins = new CJTBlockPinsTable($this->dbDriver);
152 $codeFile = new CJTBlockFilesTable($this->dbDriver);
153 // Get blocks revisions.
154 $revisions['fields'] = array('id');
155 $revisions['filters']['parent'] = $ids;
156 $revisions['type'] = 'revision';
157 $revisions['result'] = $blocks->get(null, $revisions['fields'], $revisions['filters']);
158 // Revisions ids used as revision key.
159 $revisions = array_keys($revisions['result']);
160 // Delete all revisions for all "DELETED" blocks
161 // only if there is at least one revision.
162 if (!empty($revisions)) {
163 $blocks->delete($revisions);
164 $pins->delete($revisions);
165 // Delete code files.
166 $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions)));
167 }
168 // Delete linked templates.
169 $linkedOnlyTemplatesQuery = 'DELETE FROM #__cjtoolbox_block_templates WHERE blockId IN(%s)';
170 $this->dbDriver->delete(sprintf($linkedOnlyTemplatesQuery, implode(',', $ids)));
171 // Delete associated parameters.
172 $mdlParams = new CJT_Models_Parameters();
173 $mdlParams->delete($ids);
174 // Delete form.
175 $mdlForm = new CJT_Models_Forms();
176 $mdlForm->delete($ids);
177 // Delete blocks.
178 $blocks->delete($ids);
179 $pins->delete($ids);
180 // Delete code files.
181 $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
182 // Chaining!
183 return $this;
184 }
185
186 /**
187 * put your comment there...
188 *
189 * @param mixed $id
190 * @param mixed $fields
191 */
192 public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
193 $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
194 $block = !empty($blocks) ? reset($blocks) : null;
195 return $block;
196 }
197
198 /**
199 * put your comment there...
200 *
201 * @param mixed $ids
202 */
203 public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
204 // initialize.
205 $blocks = array();
206 $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true);
207 unset($filters['returnCodeFile']);
208 // Create Tables objects.
209 $blocksTable = new CJTBlocksTable($this->dbDriver);
210 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
211 $blockFiles = new CJTBlockFilesTable($this->dbDriver);
212 // Read blocks.
213 $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr);
214 // Get only pins for retrieved blocks.
215 $ids = array_keys($blocks);
216 $pins = empty($ids) ? array() : $pinsTable->get($ids);
217 // Push pins into blocks.
218 foreach ($pins as $pin) {
219 // Use pin name as object member.
220 // Each pin is an array.
221 // e.g blocks[ID]->pages[] = PAGE-ID.
222 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
223 }
224 // Get active file code.
225 // There always should be active file unless that the
226 // requested block is never switched by current author before.
227 // However we'll do that only if filters['returnCodeFile'] set to TRUE.
228 if ($returnCodeFile) {
229 foreach ($ids as $id) {
230 if(!$activeFileId = $this->getCurrentAuthorBlockActiveFileId($id)) {
231 // If first time use masterFile ID as he default.
232 $activeFileId = $blocks[$id]->masterFile;
233 }
234 // Retreive code for block code file.
235 $codeFile = (array) $blockFiles->setData(array('blockId' => $id, 'id' => $activeFileId))
236 ->load()
237 ->getData();
238 unset($codeFile['description']);
239 $blocks[$id]->file = (object) $codeFile;
240 // Also return the active file id withing the set.
241 $blocks[$id]->activeFileId = $activeFileId;
242 }
243 }
244 return $blocks;
245 }
246
247 /**
248 * put your comment there...
249 *
250 * @param mixed $blockId
251 * @param mixed $authorId
252 */
253 public function getAuthorBlockActiveFileId($blockId, $authorId = null) {
254 // Get author active file for the requested block.
255 $activeFileId = (int) get_user_meta($authorId, "cjt_block_active_file_{$blockId}", true);
256 // Query block active file.
257 return $activeFileId;
258 }
259
260 /**
261 * put your comment there...
262 *
263 * @param mixed $blockId
264 * @param mixed $authorId
265 */
266 public function getCurrentAuthorBlockActiveFileId($blockId) {
267 return $this->getAuthorBlockActiveFileId($blockId, get_current_user_id());
268 }
269
270 /**
271 * put your comment there...
272 *
273 * @param mixed $id
274 */
275 public function getInfo($id) {
276 // Info fields.
277 $infoFields = array('name', 'id', 'owner', 'created', 'lastModified');
278 // Get info fields from db.
279 $blocksTable = new CJTBlocksTable($this->dbDriver);
280 $info = $blocksTable->get($id, $infoFields);
281 // Get user object from id.
282 $info = reset($info);
283 $info->owner = get_userdata($info->owner);
284 // Set shortcode name.
285 $info->shortcode = "[cjtoolbox name='{$info->name}']";
286 return $info;
287 }
288
289 /**
290 * put your comment there...
291 *
292 */
293 public function getOrder() {
294 return get_option('meta-box-order_cjtoolbox');
295 }
296
297 /**
298 * put your comment there...
299 *
300 */
301 public function save() {
302 $this->dbDriver->processQueue();
303 }
304
305 /**
306 * put your comment there...
307 *
308 * @param mixed $order
309 */
310 public function setOrder($order) {
311 $orderOptionName = 'meta-box-order_cjtoolbox';
312 // Update user order so That jQuery sortable Plugin will display them in correct orders!
313 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
314 // Update CENTRALIZED order in the options table!
315 update_option($orderOptionName, $order);
316 }
317
318 /**
319 * put your comment there...
320 *
321 * @param mixed $block
322 */
323 public function update($block, $updatePins) {
324 // To be used by array_intersect_key.
325 $block = (array) $block;
326 // Create Tables objects.
327 $blocks = new CJTBlocksTable($this->dbDriver);
328 $pins = new CJTBlockPinsTable($this->dbDriver);
329 // Update block pins if requested.
330 if ($updatePins) {
331 // Isolate block pins freom native block data.
332 $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
333 $pins->update($block['id'], $pinsData);
334 }
335 // Update code file
336 if (isset($block['activeFileId'])) {
337 $codeFile = new CJTBlockFilesTable($this->dbDriver);
338 $codeFile->set('blockId', $block['id'])
339 ->set('id', $block['activeFileId'])
340 ->set('code', $block['code'])
341 ->save();
342 }
343 // Isolate block fields.
344 $blockData = array_intersect_key($block, $blocks->getFields());
345 $blocks->update($blockData);
346 }
347
348 } // End class.