PluginProbe
CSS & JavaScript Toolbox / 8.0.3
CSS & JavaScript Toolbox v8.0.3
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 +121 -18 6.0.118.0.3 View file →
@@ -45,22 +45,37 @@
45 45
46 46 /**
47 47 * put your comment there...
48 48 *
49 - * @param mixed $data
50 - * @param mixed $pins
51 - * @param mixed $customPins
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']) {
59 + if (!isset($block['id']) || !$block['id']) {
60 60 $block['id'] = $blocks->getNextId();
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 /**
@@ -65,17 +80,21 @@
65 80
66 81 /**
67 82 * put your comment there...
68 83 *
69 - * @param mixed $id
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);
@@ -80,9 +99,9 @@
80 99 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
81 100 $this->delete(array_shift($revisions)->id);
82 101 }
83 102 // Get block data.
84 - $block['fields'] = array('id', 'lastModified', 'pinPoint', 'code', 'links', 'expressions');
103 + $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions');
85 104 // get() developed to return multiple blocks, fetch the first.
86 105 $result = $blocks->get($blockId, $block['fields']);
87 106 $block = reset($result);
88 107 // Set other fields.
@@ -90,8 +109,10 @@
90 109 $block->parent = $blockId;
91 110 $block->type = 'revision';
92 111 $block->created = current_time('mysql');
93 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!
94 115 $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
95 116 // Add block data.
96 117 $blocks->insert($block);
97 118 // Get block pins and insert pins for the revision block.
@@ -98,8 +119,16 @@
98 119 $blockPins = $pins->get($blockId);
99 120 if (!empty($blockPins)) {
100 121 $pins->insertRaw($block->id, $blockPins);
101 122 }
123 + // Revision current ActiveFileId code record.
124 + // Simply, get a copy of it from the target block
125 + // and assign the copy to the new created block revision.
126 + $codeFile->set('id', $activeFileId)
127 + ->set('blockId', $blockId)
128 + ->load()
129 + ->set('blockId', $block->id)
130 + ->save(true, true);
102 131 }
103 132
104 133 /**
105 134 * Put your comments here...
@@ -116,11 +145,16 @@
116 145 *
117 146 * @param mixed $ids
118 147 */
119 148 public function delete($ids) {
149 + // Allow single or multiple Ids to be passed.
150 + if (!is_array($ids)) {
151 + $ids = array($ids);
152 + }
120 153 // Create Tables objects.
121 154 $blocks = new CJTBlocksTable($this->dbDriver);
122 155 $pins = new CJTBlockPinsTable($this->dbDriver);
156 + $codeFile = new CJTBlockFilesTable($this->dbDriver);
123 157 // Get blocks revisions.
124 158 $revisions['fields'] = array('id');
125 159 $revisions['filters']['parent'] = $ids;
126 160 $revisions['type'] = 'revision';
@@ -131,12 +165,25 @@
131 165 // only if there is at least one revision.
132 166 if (!empty($revisions)) {
133 167 $blocks->delete($revisions);
134 168 $pins->delete($revisions);
169 + // Delete code files.
170 + $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions)));
135 171 }
172 + // Delete linked templates.
173 + $linkedOnlyTemplatesQuery = 'DELETE FROM #__cjtoolbox_block_templates WHERE blockId IN(%s)';
174 + $this->dbDriver->delete(sprintf($linkedOnlyTemplatesQuery, implode(',', $ids)));
175 + // Delete associated parameters.
176 + $mdlParams = new CJT_Models_Parameters();
177 + $mdlParams->delete($ids);
178 + // Delete form.
179 + $mdlForm = new CJT_Models_Forms();
180 + $mdlForm->delete($ids);
136 181 // Delete blocks.
137 182 $blocks->delete($ids);
138 183 $pins->delete($ids);
184 + // Delete code files.
185 + $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
139 186 // Chaining!
140 187 return $this;
141 188 }
142 189
@@ -145,10 +192,10 @@
145 192 *
146 193 * @param mixed $id
147 194 * @param mixed $fields
148 195 */
149 - public function getBlock($id, $filters = array(), $fields = array('*')) {
150 - $blocks = $this->getBlocks($id, $filters, $fields);
196 + public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
197 + $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
151 198 $block = !empty($blocks) ? reset($blocks) : null;
152 199 return $block;
153 200 }
154 201
@@ -156,15 +203,19 @@
156 203 * put your comment there...
157 204 *
158 205 * @param mixed $ids
159 206 */
160 - public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array()) {
207 + public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
208 + // initialize.
161 209 $blocks = array();
210 + $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true);
211 + unset($filters['returnCodeFile']);
162 212 // Create Tables objects.
163 213 $blocksTable = new CJTBlocksTable($this->dbDriver);
164 214 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
215 + $blockFiles = new CJTBlockFilesTable($this->dbDriver);
165 216 // Read blocks.
166 - $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy);
217 + $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr);
167 218 // Get only pins for retrieved blocks.
168 219 $ids = array_keys($blocks);
169 220 $pins = empty($ids) ? array() : $pinsTable->get($ids);
170 221 // Push pins into blocks.
@@ -173,8 +224,28 @@
173 224 // Each pin is an array.
174 225 // e.g blocks[ID]->pages[] = PAGE-ID.
175 226 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
176 227 }
228 + // Get active file code.
229 + // There always should be active file unless that the
230 + // requested block is never switched by current author before.
231 + // However we'll do that only if filters['returnCodeFile'] set to TRUE.
232 + if ($returnCodeFile) {
233 + foreach ($ids as $id) {
234 + if(!$activeFileId = $this->getCurrentAuthorBlockActiveFileId($id)) {
235 + // If first time use masterFile ID as he default.
236 + $activeFileId = $blocks[$id]->masterFile;
237 + }
238 + // Retreive code for block code file.
239 + $codeFile = (array) $blockFiles->setData(array('blockId' => $id, 'id' => $activeFileId))
240 + ->load()
241 + ->getData();
242 + unset($codeFile['description']);
243 + $blocks[$id]->file = (object) $codeFile;
244 + // Also return the active file id withing the set.
245 + $blocks[$id]->activeFileId = $activeFileId;
246 + }
247 + }
177 248 return $blocks;
178 249 }
179 250
180 251 /**
@@ -179,8 +250,31 @@
179 250
180 251 /**
181 252 * put your comment there...
182 253 *
254 + * @param mixed $blockId
255 + * @param mixed $authorId
256 + */
257 + public function getAuthorBlockActiveFileId($blockId, $authorId = null) {
258 + // Get author active file for the requested block.
259 + $activeFileId = (int) get_user_meta($authorId, "cjt_block_active_file_{$blockId}", true);
260 + // Query block active file.
261 + return $activeFileId;
262 + }
263 +
264 + /**
265 + * put your comment there...
266 + *
267 + * @param mixed $blockId
268 + * @param mixed $authorId
269 + */
270 + public function getCurrentAuthorBlockActiveFileId($blockId) {
271 + return $this->getAuthorBlockActiveFileId($blockId, get_current_user_id());
272 + }
273 +
274 + /**
275 + * put your comment there...
276 + *
183 277 * @param mixed $id
184 278 */
185 279 public function getInfo($id) {
186 280 // Info fields.
@@ -191,9 +285,9 @@
191 285 // Get user object from id.
192 286 $info = reset($info);
193 287 $info->owner = get_userdata($info->owner);
194 288 // Set shortcode name.
195 - $info->shortcode = "[cjtoolbox id='{$info->id}']";
289 + $info->shortcode = "[cjtoolbox name='{$info->name}']";
196 290 return $info;
197 291 }
198 292
199 293 /**
@@ -230,9 +324,10 @@
230 324 *
231 325 * @param mixed $block
232 326 */
233 327 public function update($block, $updatePins) {
234 - $block = (array) $block; // To be used by array_intersect_key.
328 + // To be used by array_intersect_key.
329 + $block = (array) $block;
235 330 // Create Tables objects.
236 331 $blocks = new CJTBlocksTable($this->dbDriver);
237 332 $pins = new CJTBlockPinsTable($this->dbDriver);
238 333 // Update block pins if requested.
@@ -240,10 +335,18 @@
240 335 // Isolate block pins freom native block data.
241 336 $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
242 337 $pins->update($block['id'], $pinsData);
243 338 }
339 + // Update code file
340 + if (isset($block['activeFileId'])) {
341 + $codeFile = new CJTBlockFilesTable($this->dbDriver);
342 + $codeFile->set('blockId', $block['id'])
343 + ->set('id', $block['activeFileId'])
344 + ->set('code', $block['code'])
345 + ->save();
346 + }
244 347 // Isolate block fields.
245 - $block = array_intersect_key($block, $blocks->getFields());
246 - $blocks->update($block);
348 + $blockData = array_intersect_key($block, $blocks->getFields());
349 + $blocks->update($blockData);
247 350 }
248 351
249 352 } // End class.