PluginProbe
CSS & JavaScript Toolbox / 11.5
CSS & JavaScript Toolbox v11.5
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 11.5, at models/blocks.php

489 lines 14.0 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 = 99;
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 $block
50 * @param mixed $forceAddCodeBlock
51 */
52 public function add($block, $forceAddCodeBlock = false) {
53 // Make sure it array and not stdClass as it has been used from various areas!
54 $block = (array) $block;
55 // Create Tables objects.
56 $blocks = new CJTBlocksTable($this->dbDriver);
57 $codeFile = new CJTBlockFilesTable($this->dbDriver);
58 // Get new id if not specified.
59 if (!isset($block['id']) || !$block['id']) {
60 $block['id'] = $blocks->getNextIdNEW();
61 }
62 // Backward compatibility for old block style
63 // that has code field inside blocks table.
64 /// if passed create master code file for the code field.
65 if ($forceAddCodeBlock || isset($block['code'])) {
66 // Cache code for MASTER file.
67 $codeFile->set('code', $block['code']);
68 unset($block['code']); // If it passed it mist be removed from blocks data(avoid field not found)
69 // Add code files.
70 $codeFile->set('blockId', $block['id'])
71 ->set('id', 1)
72 ->set('name', 'Master')
73 ->save(true, true);
74 }
75 // Add new block.
76 $blocks->insert($block);
77 // Return Newly added block id.
78 return $block['id'];
79 }
80
81 /**
82 * put your comment there...
83 *
84 * @param mixed $blockId
85 * @param mixed $activeFileId
86 */
87 public function addRevision($blockId, $activeFileId) {
88 // Create Tables objects.
89 $blocks = new CJTBlocksTable($this->dbDriver);
90 $pins = new CJTBlockPinsTable($this->dbDriver);
91 $codeFile = new CJTBlockFilesTable($this->dbDriver);
92 // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per
93 // block code files So that a single block may has up to
94 // self::MAX_REVISIONS_PER_BLOCK * count(codeFiles)
95 $revisions['fields'] = array('id');
96 $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId);
97 $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']);
98 // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one.
99 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
100 $this->delete(array_shift($revisions)->id);
101 }
102 // Get block data.
103 $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions');
104 // get() developed to return multiple blocks, fetch the first.
105 $result = $blocks->get($blockId, $block['fields']);
106 $block = reset($result);
107 // Set other fields.
108 $block->location = $block->state = '';
109 $block->parent = $blockId;
110 $block->type = 'revision';
111 $block->created = current_time('mysql');
112 $block->owner = get_current_user_id();
113 $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be
114 // used as the masterFile!
115 //$block->id = $blocks->getNextId(); // Get new id for revision rrecord.
116 $block->id = $blocks->getNextIdNEW(); // Get new id for revision rrecord.
117 // Add block data.
118 $blocks->insert($block);
119 // Get block pins and insert pins for the revision block.
120 $blockPins = $pins->get($blockId);
121 if (!empty($blockPins)) {
122 $pins->insertRaw($block->id, $blockPins);
123 }
124 // Revision current ActiveFileId code record.
125 // Simply, get a copy of it from the target block
126 // and assign the copy to the new created block revision.
127 $codeFile->set('id', $activeFileId)
128 ->set('blockId', $blockId)
129 ->load()
130 ->set('blockId', $block->id)
131 ->save(true, true);
132 }
133
134 /**
135 * Put your comments here...
136 *
137 *
138 * @return
139 */
140 public function dbDriver() {
141 return $this->dbDriver;
142 }
143
144 /**
145 * put your comment there...
146 *
147 * @param mixed $ids
148 */
149 public function delete($ids) {
150 // Allow single or multiple Ids to be passed.
151 if (!is_array($ids)) {
152 $ids = array($ids);
153 }
154 // Create Tables objects.
155 $blocks = new CJTBlocksTable($this->dbDriver);
156 $pins = new CJTBlockPinsTable($this->dbDriver);
157 $codeFile = new CJTBlockFilesTable($this->dbDriver);
158 // Get blocks revisions.
159 $revisions['fields'] = array('id');
160 $revisions['filters']['parent'] = $ids;
161 $revisions['type'] = 'revision';
162 $revisions['result'] = $blocks->get(null, $revisions['fields'], $revisions['filters']);
163 // Revisions ids used as revision key.
164 $revisions = array_keys($revisions['result']);
165 // Delete all revisions for all "DELETED" blocks
166 // only if there is at least one revision.
167 if (!empty($revisions)) {
168 $blocks->delete($revisions);
169 $pins->delete($revisions);
170 // Delete code files.
171 $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions)));
172 }
173 // Delete linked templates.
174 $linkedOnlyTemplatesQuery = 'DELETE FROM #__cjtoolbox_block_templates WHERE blockId IN(%s)';
175 $this->dbDriver->delete(sprintf($linkedOnlyTemplatesQuery, implode(',', $ids)));
176 // Delete associated parameters.
177 $mdlParams = new CJT_Models_Parameters();
178 $mdlParams->delete($ids);
179 // Delete form.
180 $mdlForm = new CJT_Models_Forms();
181 $mdlForm->delete($ids);
182 // Delete blocks.
183 $blocks->delete($ids);
184 $pins->delete($ids);
185 // Delete code files.
186 $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
187 // Chaining!
188 return $this;
189 }
190
191 /**
192 * put your comment there...
193 *
194 * @param mixed $block
195 */
196 public static function getAllAssignments($block) {
197
198 $inPages = $block->pages ? $block->pages : [];
199 $inPosts = $block->posts ? $block->posts : [];
200 $inCats = $block->categories ? $block->categories : [];
201 $inTags = $block->post_tags ? $block->post_tags : [];
202 $inLinks = ! empty( $block->links ) ? explode( "\n", $block->links ) : [];
203 $inExps = ! empty( $block->expressions ) ? explode( "\n", $block->expressions ) : [];
204 $inAux = array();
205
206 // Found selected Aux
207 $auxFlags = array(
208 CJTBlockModel::PINS_404_ERROR,
209 CJTBlockModel::PINS_ARCHIVE,
210 CJTBlockModel::PINS_ATTACHMENT,
211 CJTBlockModel::PINS_AUTHOR,
212 CJTBlockModel::PINS_BACKEND,
213 CJTBlockModel::PINS_CATEGORIES_ALL_CATEGORIES,
214 CJTBlockModel::PINS_EXPRESSIONS,
215 CJTBlockModel::PINS_FRONTEND,
216 CJTBlockModel::PINS_PAGES_ALL_PAGES,
217 CJTBlockModel::PINS_POSTS_ALL_POSTS,
218 CJTBlockModel::PINS_POSTS_BLOG_INDEX,
219 CJTBlockModel::PINS_POSTS_RECENT,
220 CJTBlockModel::PINS_SEARCH,
221 CJTBlockModel::PINS_TAG
222 );
223
224 foreach ($auxFlags as $auxFlag) {
225
226 if ($auxFlag & $block->pinPoint) {
227
228 $inAux[] = $auxFlag;
229 }
230
231 }
232
233 $allAssignment = array_merge( $inPages, $inPosts, $inCats, $inTags, $inLinks, $inExps, $inAux );
234
235 return $allAssignment;
236 }
237
238 /**
239 * put your comment there...
240 *
241 * @param mixed $id
242 * @param mixed $fields
243 */
244 public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
245 $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
246 $block = !empty($blocks) ? reset($blocks) : null;
247 return $block;
248 }
249
250 /**
251 * put your comment there...
252 *
253 * @param mixed $ids
254 */
255 public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
256 // initialize.
257 $blocks = array();
258 $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true);
259 unset($filters['returnCodeFile']);
260 // Create Tables objects.
261 $blocksTable = new CJTBlocksTable($this->dbDriver);
262 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
263 $blockFiles = new CJTBlockFilesTable($this->dbDriver);
264 // Read blocks.
265 $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr);
266 // Get only pins for retrieved blocks.
267 $ids = array_keys($blocks);
268 $pins = empty($ids) ? array() : $pinsTable->get($ids);
269 // Push pins into blocks.
270 foreach ($pins as $pin) {
271 // Use pin name as object member.
272 // Each pin is an array.
273 // e.g blocks[ID]->pages[] = PAGE-ID.
274 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
275 }
276 // Get active file code.
277 // There always should be active file unless that the
278 // requested block is never switched by current author before.
279 // However we'll do that only if filters['returnCodeFile'] set to TRUE.
280 if ($returnCodeFile) {
281 foreach ($ids as $id) {
282 if(!$activeFileId = $this->getCurrentAuthorBlockActiveFileId($id)) {
283 // If first time use masterFile ID as he default.
284 $activeFileId = $blocks[$id]->masterFile;
285 }
286 // Retreive code for block code file.
287 $codeFile = (array) $blockFiles->setData(array('blockId' => $id, 'id' => $activeFileId))
288 ->load()
289 ->getData();
290 unset($codeFile['description']);
291 $blocks[$id]->file = (object) $codeFile;
292 // Also return the active file id withing the set.
293 $blocks[$id]->activeFileId = $activeFileId;
294 }
295 }
296 return $blocks;
297 }
298
299 /**
300 * put your comment there...
301 *
302 * @param mixed $blockId
303 * @param mixed $authorId
304 */
305 public function getAuthorBlockActiveFileId($blockId, $authorId = null) {
306 // Get author active file for the requested block.
307 $activeFileId = (int) get_user_meta($authorId, "cjt_block_active_file_{$blockId}", true);
308 // Query block active file.
309 return $activeFileId;
310 }
311
312 /**
313 * put your comment there...
314 *
315 * @param mixed $blockId
316 */
317 public static function getCodeFilesCount($blockId) {
318
319 global $wpdb;
320
321 $query = " SELECT count(*)
322 FROM {$wpdb->prefix}cjtoolbox_block_files f
323 WHERE f.blockId = %d;";
324
325 $query = $wpdb->prepare($query, $blockId);
326
327 $codeFilesCount = $wpdb->get_var($query);
328
329 return $codeFilesCount;
330 }
331
332 /**
333 * put your comment there...
334 *
335 * @param mixed $blockId
336 * @param mixed $authorId
337 */
338 public function getCurrentAuthorBlockActiveFileId($blockId) {
339 return $this->getAuthorBlockActiveFileId($blockId, get_current_user_id());
340 }
341
342 /**
343 * put your comment there...
344 *
345 */
346 public static function getCustomPostTypes()
347 {
348
349 static $postTypes = null;
350
351 if ( $postTypes !== null )
352 {
353 return $postTypes;
354 }
355
356
357 $postTypes = array();
358
359 // Create tabs for every custom post under the custom posts tab.
360 // Get all registered custom posts.
361 $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' );
362
363 // Add tab for every custom post
364 // Exclude 'Empty' Custom Post Types.
365 foreach ( $customPosts as $typeName => $customPost )
366 {
367 // Check if has posts.
368 $hasPosts = count( get_posts( array( 'post_type' => $typeName, 'offset' => 0, 'numberposts' => 1 ) ) );
369
370 // Add only types with at least one post exists.
371 if ( $hasPosts )
372 {
373 $postTypes[ $typeName ] = array
374 (
375 'title' => $customPost->labels->name,
376 'renderer' => 'objects-list',
377 'type' => array
378 (
379 'type' => $typeName,
380 'group' => 'posts',
381 'targetType' => 'post'
382 )
383
384 );
385
386 }
387
388 }
389
390 do_action( CJTPluggableHelper::ACTION_BLOCK_CUSTOM_POST_TYPES, $postTypes );
391
392 return $postTypes;
393 }
394
395 /**
396 * put your comment there...
397 *
398 * @param mixed $id
399 */
400 public function getInfo($id) {
401 // Info fields.
402 $infoFields = array('name', 'id', 'owner', 'created', 'lastModified');
403 // Get info fields from db.
404 $blocksTable = new CJTBlocksTable($this->dbDriver);
405 $info = $blocksTable->get($id, $infoFields);
406 // Get user object from id.
407 $info = reset($info);
408 $info->owner = get_userdata($info->owner);
409 // Set shortcode name.
410 $info->shortcode = "[cjtoolbox name='{$info->name}']";
411 return $info;
412 }
413
414 /**
415 * put your comment there...
416 *
417 */
418 public function getOrder() {
419 return get_option('meta-box-order_cjtoolbox');
420 }
421
422 /**
423 * put your comment there...
424 *
425 */
426 public function save() {
427 $this->dbDriver->processQueue();
428 }
429
430 /**
431 * put your comment there...
432 *
433 * @param mixed $order
434 */
435 public function setOrder($order) {
436 $orderOptionName = 'meta-box-order_cjtoolbox';
437 // Update user order so That jQuery sortable Plugin will display them in correct orders!
438 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
439 // Update CENTRALIZED order in the options table!
440 update_option($orderOptionName, $order);
441 }
442
443 /**
444 * put your comment there...
445 *
446 * @param mixed $block
447 */
448 public function update( $block, $updatePins )
449 {
450
451 $block = ( array ) $block;
452
453 $blocks = new CJTBlocksTable( $this->dbDriver );
454 $pins = new CJTBlockPinsTable( $this->dbDriver );
455
456 // Update block pins if requested.
457 if ( $updatePins )
458 {
459 // Isolate block pins freom native block data.
460 $pinsData = array_intersect_key( $block, array_flip( array_keys( CJTBlockModel::getCustomPins() ) ) );
461
462 do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK_PINS, $block, $pinsData );
463
464 $pins->update( $block[ 'id' ], $pinsData );
465
466 }
467
468 // Update code file
469 if ( isset( $block[ 'activeFileId' ] ) )
470 {
471
472 $codeFile = new CJTBlockFilesTable( $this->dbDriver );
473
474 $codeFile->set( 'blockId', $block[ 'id' ] )
475 ->set( 'id', $block[ 'activeFileId'] )
476 ->set( 'code', $block[ 'code'] )
477 ->save();
478 }
479
480 // Isolate block fields.
481 $blockData = array_intersect_key($block, $blocks->getFields());
482
483 do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK, $updatePins, $blockData );
484
485 $blocks->update( $blockData );
486
487 }
488
489 } // End class.