PluginProbe
CSS & JavaScript Toolbox / trunk
CSS & JavaScript Toolbox vtrunk
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
← All changes | models/blocks.php +303 -62 6.0trunk View file →
@@ -12,79 +12,99 @@
12 12 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
13 13 require_once CJTOOLBOX_TABLES_PATH . '/block-pins.php';
14 14 // MYSQL Queue Driver.
15 15 require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/queue-driver.inc.php';
16 -
16 +
17 17 /**
18 18 * Provide simple access (read or write) to all Blocks data.
19 -*
19 +*
20 20 * @author Ahmed Said
21 21 * @version 6
22 22 */
23 23 class CJTBlocksModel {
24 -
24 +
25 25 /**
26 - *
26 + *
27 27 */
28 - const MAX_REVISIONS_PER_BLOCK = 10;
29 -
28 + const MAX_REVISIONS_PER_BLOCK = 99;
29 +
30 30 /**
31 31 * put your comment there...
32 - *
32 + *
33 33 * @var mixed
34 34 */
35 35 protected $dbDriver = null;
36 -
36 +
37 37 /**
38 38 * put your comment there...
39 - *
39 + *
40 40 */
41 41 public function __construct() {
42 42 // Initialize CTTTable MYSQL Driver.
43 43 $this->dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']);
44 44 }
45 -
45 +
46 46 /**
47 47 * put your comment there...
48 - *
49 - * @param mixed $data
50 - * @param mixed $pins
51 - * @param mixed $customPins
48 + *
49 + * @param mixed $block
50 + * @param mixed $forceAddCodeBlock
52 51 */
53 - public function add($block) {
52 + public function add($block, $forceAddCodeBlock = false) {
54 53 // Make sure it array and not stdClass as it has been used from various areas!
55 54 $block = (array) $block;
56 55 // Create Tables objects.
57 56 $blocks = new CJTBlocksTable($this->dbDriver);
57 + $codeFile = new CJTBlockFilesTable($this->dbDriver);
58 58 // Get new id if not specified.
59 - if (!$block['id']) {
60 - $block['id'] = $blocks->getNextId();
59 + if (!isset($block['id']) || !$block['id']) {
60 + $block['id'] = $blocks->getNextIdNEW();
61 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.
62 76 $blocks->insert($block);
77 + // Return Newly added block id.
63 78 return $block['id'];
64 79 }
65 -
80 +
66 81 /**
67 82 * put your comment there...
68 - *
69 - * @param mixed $id
83 + *
84 + * @param mixed $blockId
85 + * @param mixed $activeFileId
70 86 */
71 - public function addRevision($blockId) {
87 + public function addRevision($blockId, $activeFileId) {
72 88 // Create Tables objects.
73 89 $blocks = new CJTBlocksTable($this->dbDriver);
74 90 $pins = new CJTBlockPinsTable($this->dbDriver);
75 - // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per block.
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)
76 95 $revisions['fields'] = array('id');
77 - $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId);
96 + $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId);
78 97 $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']);
79 98 // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one.
80 99 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
81 100 $this->delete(array_shift($revisions)->id);
82 101 }
83 - // Get block data.
84 - $block['fields'] = array('lastModified', 'pinPoint', 'code', 'links', 'expressions');
102 + // Get block data.
103 + $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions');
85 104 // get() developed to return multiple blocks, fetch the first.
86 - $block = array_shift($blocks->get($blockId, $block['fields']));
105 + $result = $blocks->get($blockId, $block['fields']);
106 + $block = reset($result);
87 107 // Set other fields.
88 108 $block->location = $block->state = '';
89 109 $block->parent = $blockId;
90 110 $block->type = 'revision';
@@ -89,9 +109,12 @@
89 109 $block->parent = $blockId;
90 110 $block->type = 'revision';
91 111 $block->created = current_time('mysql');
92 112 $block->owner = get_current_user_id();
93 - $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
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.
94 117 // Add block data.
95 118 $blocks->insert($block);
96 119 // Get block pins and insert pins for the revision block.
97 120 $blockPins = $pins->get($blockId);
@@ -97,29 +120,42 @@
97 120 $blockPins = $pins->get($blockId);
98 121 if (!empty($blockPins)) {
99 122 $pins->insertRaw($block->id, $blockPins);
100 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);
101 132 }
102 -
133 +
103 134 /**
104 135 * Put your comments here...
105 136 *
106 137 *
107 - * @return
108 - */
138 + * @return
139 + */
109 140 public function dbDriver() {
110 141 return $this->dbDriver;
111 142 }
112 -
143 +
113 144 /**
114 145 * put your comment there...
115 - *
146 + *
116 147 * @param mixed $ids
117 148 */
118 149 public function delete($ids) {
150 + // Allow single or multiple Ids to be passed.
151 + if (!is_array($ids)) {
152 + $ids = array($ids);
153 + }
119 154 // Create Tables objects.
120 155 $blocks = new CJTBlocksTable($this->dbDriver);
121 156 $pins = new CJTBlockPinsTable($this->dbDriver);
157 + $codeFile = new CJTBlockFilesTable($this->dbDriver);
122 158 // Get blocks revisions.
123 159 $revisions['fields'] = array('id');
124 160 $revisions['filters']['parent'] = $ids;
125 161 $revisions['type'] = 'revision';
@@ -130,40 +166,104 @@
130 166 // only if there is at least one revision.
131 167 if (!empty($revisions)) {
132 168 $blocks->delete($revisions);
133 169 $pins->delete($revisions);
170 + // Delete code files.
171 + $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions)));
134 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);
135 182 // Delete blocks.
136 183 $blocks->delete($ids);
137 184 $pins->delete($ids);
185 + // Delete code files.
186 + $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
138 187 // Chaining!
139 188 return $this;
140 189 }
141 -
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 +
142 238 /**
143 239 * put your comment there...
144 - *
240 + *
145 241 * @param mixed $id
146 242 * @param mixed $fields
147 243 */
148 - public function getBlock($id, $filters = array(), $fields = array('*')) {
149 - $blocks = $this->getBlocks($id, $filters, $fields);
244 + public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
245 + $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
150 246 $block = !empty($blocks) ? reset($blocks) : null;
151 247 return $block;
152 248 }
153 -
249 +
154 250 /**
155 251 * put your comment there...
156 - *
252 + *
157 253 * @param mixed $ids
158 254 */
159 - public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array()) {
255 + public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
256 + // initialize.
160 257 $blocks = array();
258 + $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true);
259 + unset($filters['returnCodeFile']);
161 260 // Create Tables objects.
162 261 $blocksTable = new CJTBlocksTable($this->dbDriver);
163 262 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
263 + $blockFiles = new CJTBlockFilesTable($this->dbDriver);
164 264 // Read blocks.
165 - $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy);
265 + $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr);
166 266 // Get only pins for retrieved blocks.
167 267 $ids = array_keys($blocks);
168 268 $pins = empty($ids) ? array() : $pinsTable->get($ids);
169 269 // Push pins into blocks.
@@ -172,14 +272,130 @@
172 272 // Each pin is an array.
173 273 // e.g blocks[ID]->pages[] = PAGE-ID.
174 274 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
175 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 + }
176 296 return $blocks;
177 297 }
178 -
298 +
179 299 /**
180 300 * put your comment there...
181 - *
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 + *
182 398 * @param mixed $id
183 399 */
184 400 public function getInfo($id) {
185 401 // Info fields.
@@ -190,31 +406,31 @@
190 406 // Get user object from id.
191 407 $info = reset($info);
192 408 $info->owner = get_userdata($info->owner);
193 409 // Set shortcode name.
194 - $info->shortcode = "[cjtoolbox id='{$info->id}']";
410 + $info->shortcode = "[cjtoolbox name='{$info->name}']";
195 411 return $info;
196 412 }
197 -
413 +
198 414 /**
199 415 * put your comment there...
200 - *
416 + *
201 417 */
202 418 public function getOrder() {
203 419 return get_option('meta-box-order_cjtoolbox');
204 420 }
205 -
421 +
206 422 /**
207 423 * put your comment there...
208 - *
424 + *
209 425 */
210 426 public function save() {
211 427 $this->dbDriver->processQueue();
212 428 }
213 -
429 +
214 430 /**
215 431 * put your comment there...
216 - *
432 + *
217 433 * @param mixed $order
218 434 */
219 435 public function setOrder($order) {
220 436 $orderOptionName = 'meta-box-order_cjtoolbox';
@@ -222,27 +438,52 @@
222 438 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
223 439 // Update CENTRALIZED order in the options table!
224 440 update_option($orderOptionName, $order);
225 441 }
226 -
442 +
227 443 /**
228 444 * put your comment there...
229 - *
445 + *
230 446 * @param mixed $block
231 447 */
232 - public function update($block, $updatePins) {
233 - $block = (array) $block; // To be used by array_intersect_key.
234 - // Create Tables objects.
235 - $blocks = new CJTBlocksTable($this->dbDriver);
236 - $pins = new CJTBlockPinsTable($this->dbDriver);
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 +
237 456 // Update block pins if requested.
238 - if ($updatePins) {
457 + if ( $updatePins )
458 + {
239 459 // Isolate block pins freom native block data.
240 - $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
241 - $pins->update($block['id'], $pinsData);
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 +
242 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 +
243 480 // Isolate block fields.
244 - $block = array_intersect_key($block, $blocks->getFields());
245 - $blocks->update($block);
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 +
246 487 }
247 -
488 +
248 489 } // End class.