| @@ -54,13 +54,24 @@ | ||
| 54 | 54 | // Make sure it array and not stdClass as it has been used from various areas! |
| 55 | 55 | $block = (array) $block; |
| 56 | 56 | // Create Tables objects. |
| 57 | 57 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 58 | + $codeFile = new CJTBlockFilesTable($this->dbDriver); | |
| 58 | 59 | // Get new id if not specified. |
| 59 | - if (!$block['id']) { | |
| 60 | + if (!isset($block['id']) || !$block['id']) { | |
| 60 | 61 | $block['id'] = $blocks->getNextId(); |
| 61 | 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. | |
| 62 | 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. | |
| 63 | 74 | return $block['id']; |
| 64 | 75 | } |
| 65 | 76 | |
| 66 | 77 | /** |
| @@ -65,17 +76,21 @@ | ||
| 65 | 76 | |
| 66 | 77 | /** |
| 67 | 78 | * put your comment there... |
| 68 | 79 | * |
| 69 | - * @param mixed $id | |
| 80 | + * @param mixed $blockId | |
| 81 | + * @param mixed $activeFileId | |
| 70 | 82 | */ |
| 71 | - public function addRevision($blockId) { | |
| 83 | + public function addRevision($blockId, $activeFileId) { | |
| 72 | 84 | // Create Tables objects. |
| 73 | 85 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 74 | 86 | $pins = new CJTBlockPinsTable($this->dbDriver); |
| 75 | - // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per block. | |
| 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) | |
| 76 | 91 | $revisions['fields'] = array('id'); |
| 77 | - $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId); | |
| 92 | + $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId); | |
| 78 | 93 | $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']); |
| 79 | 94 | // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one. |
| 80 | 95 | if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) { |
| 81 | 96 | $this->delete(array_shift($revisions)->id); |
| @@ -80,11 +95,12 @@ | ||
| 80 | 95 | if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) { |
| 81 | 96 | $this->delete(array_shift($revisions)->id); |
| 82 | 97 | } |
| 83 | 98 | // Get block data. |
| 84 | - $block['fields'] = array('lastModified', 'pinPoint', 'code', 'links', 'expressions'); | |
| 99 | + $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions'); | |
| 85 | 100 | // get() developed to return multiple blocks, fetch the first. |
| 86 | - $block = array_shift($blocks->get($blockId, $block['fields'])); | |
| 101 | + $result = $blocks->get($blockId, $block['fields']); | |
| 102 | + $block = reset($result); | |
| 87 | 103 | // Set other fields. |
| 88 | 104 | $block->location = $block->state = ''; |
| 89 | 105 | $block->parent = $blockId; |
| 90 | 106 | $block->type = 'revision'; |
| @@ -89,8 +105,10 @@ | ||
| 89 | 105 | $block->parent = $blockId; |
| 90 | 106 | $block->type = 'revision'; |
| 91 | 107 | $block->created = current_time('mysql'); |
| 92 | 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! | |
| 93 | 111 | $block->id = $blocks->getNextId(); // Get new id for revision rrecord. |
| 94 | 112 | // Add block data. |
| 95 | 113 | $blocks->insert($block); |
| 96 | 114 | // Get block pins and insert pins for the revision block. |
| @@ -97,8 +115,16 @@ | ||
| 97 | 115 | $blockPins = $pins->get($blockId); |
| 98 | 116 | if (!empty($blockPins)) { |
| 99 | 117 | $pins->insertRaw($block->id, $blockPins); |
| 100 | 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); | |
| 101 | 127 | } |
| 102 | 128 | |
| 103 | 129 | /** |
| 104 | 130 | * Put your comments here... |
| @@ -115,11 +141,16 @@ | ||
| 115 | 141 | * |
| 116 | 142 | * @param mixed $ids |
| 117 | 143 | */ |
| 118 | 144 | public function delete($ids) { |
| 145 | + // Allow single or multiple Ids to be passed. | |
| 146 | + if (!is_array($ids)) { | |
| 147 | + $ids = array($ids); | |
| 148 | + } | |
| 119 | 149 | // Create Tables objects. |
| 120 | 150 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 121 | 151 | $pins = new CJTBlockPinsTable($this->dbDriver); |
| 152 | + $codeFile = new CJTBlockFilesTable($this->dbDriver); | |
| 122 | 153 | // Get blocks revisions. |
| 123 | 154 | $revisions['fields'] = array('id'); |
| 124 | 155 | $revisions['filters']['parent'] = $ids; |
| 125 | 156 | $revisions['type'] = 'revision'; |
| @@ -130,12 +161,25 @@ | ||
| 130 | 161 | // only if there is at least one revision. |
| 131 | 162 | if (!empty($revisions)) { |
| 132 | 163 | $blocks->delete($revisions); |
| 133 | 164 | $pins->delete($revisions); |
| 165 | + // Delete code files. | |
| 166 | + $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions))); | |
| 134 | 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); | |
| 135 | 177 | // Delete blocks. |
| 136 | 178 | $blocks->delete($ids); |
| 137 | 179 | $pins->delete($ids); |
| 180 | + // Delete code files. | |
| 181 | + $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids))); | |
| 138 | 182 | // Chaining! |
| 139 | 183 | return $this; |
| 140 | 184 | } |
| 141 | 185 | |
| @@ -144,10 +188,10 @@ | ||
| 144 | 188 | * |
| 145 | 189 | * @param mixed $id |
| 146 | 190 | * @param mixed $fields |
| 147 | 191 | */ |
| 148 | - public function getBlock($id, $filters = array(), $fields = array('*')) { | |
| 149 | - $blocks = $this->getBlocks($id, $filters, $fields); | |
| 192 | + public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) { | |
| 193 | + $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr); | |
| 150 | 194 | $block = !empty($blocks) ? reset($blocks) : null; |
| 151 | 195 | return $block; |
| 152 | 196 | } |
| 153 | 197 | |
| @@ -155,15 +199,19 @@ | ||
| 155 | 199 | * put your comment there... |
| 156 | 200 | * |
| 157 | 201 | * @param mixed $ids |
| 158 | 202 | */ |
| 159 | - public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array()) { | |
| 203 | + public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) { | |
| 204 | + // initialize. | |
| 160 | 205 | $blocks = array(); |
| 206 | + $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true); | |
| 207 | + unset($filters['returnCodeFile']); | |
| 161 | 208 | // Create Tables objects. |
| 162 | 209 | $blocksTable = new CJTBlocksTable($this->dbDriver); |
| 163 | 210 | $pinsTable = new CJTBlockPinsTable($this->dbDriver); |
| 211 | + $blockFiles = new CJTBlockFilesTable($this->dbDriver); | |
| 164 | 212 | // Read blocks. |
| 165 | - $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy); | |
| 213 | + $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr); | |
| 166 | 214 | // Get only pins for retrieved blocks. |
| 167 | 215 | $ids = array_keys($blocks); |
| 168 | 216 | $pins = empty($ids) ? array() : $pinsTable->get($ids); |
| 169 | 217 | // Push pins into blocks. |
| @@ -172,8 +220,28 @@ | ||
| 172 | 220 | // Each pin is an array. |
| 173 | 221 | // e.g blocks[ID]->pages[] = PAGE-ID. |
| 174 | 222 | $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value; |
| 175 | 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 | + } | |
| 176 | 244 | return $blocks; |
| 177 | 245 | } |
| 178 | 246 | |
| 179 | 247 | /** |
| @@ -178,8 +246,31 @@ | ||
| 178 | 246 | |
| 179 | 247 | /** |
| 180 | 248 | * put your comment there... |
| 181 | 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 | + * | |
| 182 | 273 | * @param mixed $id |
| 183 | 274 | */ |
| 184 | 275 | public function getInfo($id) { |
| 185 | 276 | // Info fields. |
| @@ -190,9 +281,9 @@ | ||
| 190 | 281 | // Get user object from id. |
| 191 | 282 | $info = reset($info); |
| 192 | 283 | $info->owner = get_userdata($info->owner); |
| 193 | 284 | // Set shortcode name. |
| 194 | - $info->shortcode = "[cjtoolbox id='{$info->id}']"; | |
| 285 | + $info->shortcode = "[cjtoolbox name='{$info->name}']"; | |
| 195 | 286 | return $info; |
| 196 | 287 | } |
| 197 | 288 | |
| 198 | 289 | /** |
| @@ -229,9 +320,10 @@ | ||
| 229 | 320 | * |
| 230 | 321 | * @param mixed $block |
| 231 | 322 | */ |
| 232 | 323 | public function update($block, $updatePins) { |
| 233 | - $block = (array) $block; // To be used by array_intersect_key. | |
| 324 | + // To be used by array_intersect_key. | |
| 325 | + $block = (array) $block; | |
| 234 | 326 | // Create Tables objects. |
| 235 | 327 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 236 | 328 | $pins = new CJTBlockPinsTable($this->dbDriver); |
| 237 | 329 | // Update block pins if requested. |
| @@ -239,10 +331,18 @@ | ||
| 239 | 331 | // Isolate block pins freom native block data. |
| 240 | 332 | $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories'))); |
| 241 | 333 | $pins->update($block['id'], $pinsData); |
| 242 | 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 | + } | |
| 243 | 343 | // Isolate block fields. |
| 244 | - $block = array_intersect_key($block, $blocks->getFields()); | |
| 245 | - $blocks->update($block); | |
| 344 | + $blockData = array_intersect_key($block, $blocks->getFields()); | |
| 345 | + $blocks->update($blockData); | |
| 246 | 346 | } |
| 247 | 347 | |
| 248 | 348 | } // End class. |