| @@ -45,37 +45,22 @@ | ||
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * put your comment there... |
| 48 | 48 | * |
| 49 | - * @param mixed $block | |
| 50 | - * @param mixed $forceAddCodeBlock | |
| 49 | + * @param mixed $data | |
| 50 | + * @param mixed $pins | |
| 51 | + * @param mixed $customPins | |
| 51 | 52 | */ |
| 52 | - public function add($block, $forceAddCodeBlock = false) { | |
| 53 | + public function add($block) { | |
| 53 | 54 | // Make sure it array and not stdClass as it has been used from various areas! |
| 54 | 55 | $block = (array) $block; |
| 55 | 56 | // Create Tables objects. |
| 56 | 57 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 57 | - $codeFile = new CJTBlockFilesTable($this->dbDriver); | |
| 58 | 58 | // Get new id if not specified. |
| 59 | - if (!isset($block['id']) || !$block['id']) { | |
| 59 | + if (!$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. | |
| 76 | 62 | $blocks->insert($block); |
| 77 | - // Return Newly added block id. | |
| 78 | 63 | return $block['id']; |
| 79 | 64 | } |
| 80 | 65 | |
| 81 | 66 | /** |
| @@ -80,21 +65,17 @@ | ||
| 80 | 65 | |
| 81 | 66 | /** |
| 82 | 67 | * put your comment there... |
| 83 | 68 | * |
| 84 | - * @param mixed $blockId | |
| 85 | - * @param mixed $activeFileId | |
| 69 | + * @param mixed $id | |
| 86 | 70 | */ |
| 87 | - public function addRevision($blockId, $activeFileId) { | |
| 71 | + public function addRevision($blockId) { | |
| 88 | 72 | // Create Tables objects. |
| 89 | 73 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 90 | 74 | $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) | |
| 75 | + // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per block. | |
| 95 | 76 | $revisions['fields'] = array('id'); |
| 96 | - $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId); | |
| 77 | + $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId); | |
| 97 | 78 | $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']); |
| 98 | 79 | // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one. |
| 99 | 80 | if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) { |
| 100 | 81 | $this->delete(array_shift($revisions)->id); |
| @@ -99,9 +80,9 @@ | ||
| 99 | 80 | if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) { |
| 100 | 81 | $this->delete(array_shift($revisions)->id); |
| 101 | 82 | } |
| 102 | 83 | // Get block data. |
| 103 | - $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions'); | |
| 84 | + $block['fields'] = array('id', 'lastModified', 'pinPoint', 'code', 'links', 'expressions'); | |
| 104 | 85 | // get() developed to return multiple blocks, fetch the first. |
| 105 | 86 | $result = $blocks->get($blockId, $block['fields']); |
| 106 | 87 | $block = reset($result); |
| 107 | 88 | // Set other fields. |
| @@ -109,10 +90,8 @@ | ||
| 109 | 90 | $block->parent = $blockId; |
| 110 | 91 | $block->type = 'revision'; |
| 111 | 92 | $block->created = current_time('mysql'); |
| 112 | 93 | $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 | 94 | $block->id = $blocks->getNextId(); // Get new id for revision rrecord. |
| 116 | 95 | // Add block data. |
| 117 | 96 | $blocks->insert($block); |
| 118 | 97 | // Get block pins and insert pins for the revision block. |
| @@ -119,16 +98,8 @@ | ||
| 119 | 98 | $blockPins = $pins->get($blockId); |
| 120 | 99 | if (!empty($blockPins)) { |
| 121 | 100 | $pins->insertRaw($block->id, $blockPins); |
| 122 | 101 | } |
| 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); | |
| 131 | 102 | } |
| 132 | 103 | |
| 133 | 104 | /** |
| 134 | 105 | * Put your comments here... |
| @@ -145,16 +116,11 @@ | ||
| 145 | 116 | * |
| 146 | 117 | * @param mixed $ids |
| 147 | 118 | */ |
| 148 | 119 | public function delete($ids) { |
| 149 | - // Allow single or multiple Ids to be passed. | |
| 150 | - if (!is_array($ids)) { | |
| 151 | - $ids = array($ids); | |
| 152 | - } | |
| 153 | 120 | // Create Tables objects. |
| 154 | 121 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 155 | 122 | $pins = new CJTBlockPinsTable($this->dbDriver); |
| 156 | - $codeFile = new CJTBlockFilesTable($this->dbDriver); | |
| 157 | 123 | // Get blocks revisions. |
| 158 | 124 | $revisions['fields'] = array('id'); |
| 159 | 125 | $revisions['filters']['parent'] = $ids; |
| 160 | 126 | $revisions['type'] = 'revision'; |
| @@ -165,25 +131,12 @@ | ||
| 165 | 131 | // only if there is at least one revision. |
| 166 | 132 | if (!empty($revisions)) { |
| 167 | 133 | $blocks->delete($revisions); |
| 168 | 134 | $pins->delete($revisions); |
| 169 | - // Delete code files. | |
| 170 | - $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions))); | |
| 171 | 135 | } |
| 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); | |
| 181 | 136 | // Delete blocks. |
| 182 | 137 | $blocks->delete($ids); |
| 183 | 138 | $pins->delete($ids); |
| 184 | - // Delete code files. | |
| 185 | - $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids))); | |
| 186 | 139 | // Chaining! |
| 187 | 140 | return $this; |
| 188 | 141 | } |
| 189 | 142 | |
| @@ -204,16 +157,12 @@ | ||
| 204 | 157 | * |
| 205 | 158 | * @param mixed $ids |
| 206 | 159 | */ |
| 207 | 160 | public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) { |
| 208 | - // initialize. | |
| 209 | 161 | $blocks = array(); |
| 210 | - $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true); | |
| 211 | - unset($filters['returnCodeFile']); | |
| 212 | 162 | // Create Tables objects. |
| 213 | 163 | $blocksTable = new CJTBlocksTable($this->dbDriver); |
| 214 | 164 | $pinsTable = new CJTBlockPinsTable($this->dbDriver); |
| 215 | - $blockFiles = new CJTBlockFilesTable($this->dbDriver); | |
| 216 | 165 | // Read blocks. |
| 217 | 166 | $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr); |
| 218 | 167 | // Get only pins for retrieved blocks. |
| 219 | 168 | $ids = array_keys($blocks); |
| @@ -224,28 +173,8 @@ | ||
| 224 | 173 | // Each pin is an array. |
| 225 | 174 | // e.g blocks[ID]->pages[] = PAGE-ID. |
| 226 | 175 | $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value; |
| 227 | 176 | } |
| 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 | - } | |
| 248 | 177 | return $blocks; |
| 249 | 178 | } |
| 250 | 179 | |
| 251 | 180 | /** |
| @@ -250,84 +179,8 @@ | ||
| 250 | 179 | |
| 251 | 180 | /** |
| 252 | 181 | * put your comment there... |
| 253 | 182 | * |
| 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 | - * | |
| 277 | - */ | |
| 278 | - public static function getCustomPostTypes() | |
| 279 | - { | |
| 280 | - | |
| 281 | - static $postTypes = null; | |
| 282 | - | |
| 283 | - if ( $postTypes !== null ) | |
| 284 | - { | |
| 285 | - return $postTypes; | |
| 286 | - } | |
| 287 | - | |
| 288 | - | |
| 289 | - $postTypes = array(); | |
| 290 | - | |
| 291 | - // Create tabs for every custom post under the custom posts tab. | |
| 292 | - // Get all registered custom posts. | |
| 293 | - $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' ); | |
| 294 | - | |
| 295 | - // Add tab for every custom post | |
| 296 | - // Exclude 'Empty' Custom Post Types. | |
| 297 | - foreach ( $customPosts as $typeName => $customPost ) | |
| 298 | - { | |
| 299 | - // Check if has posts. | |
| 300 | - $hasPosts = count( get_posts( array( 'post_type' => $typeName, 'offset' => 0, 'numberposts' => 1 ) ) ); | |
| 301 | - | |
| 302 | - // Add only types with at least one post exists. | |
| 303 | - if ( $hasPosts ) | |
| 304 | - { | |
| 305 | - $postTypes[ $typeName ] = array | |
| 306 | - ( | |
| 307 | - 'title' => $customPost->labels->name, | |
| 308 | - 'renderer' => 'objects-list', | |
| 309 | - 'type' => array | |
| 310 | - ( | |
| 311 | - 'type' => $typeName, | |
| 312 | - 'group' => 'posts', | |
| 313 | - 'targetType' => 'post' | |
| 314 | - ) | |
| 315 | - | |
| 316 | - ); | |
| 317 | - | |
| 318 | - } | |
| 319 | - | |
| 320 | - } | |
| 321 | - | |
| 322 | - do_action( CJTPluggableHelper::ACTION_BLOCK_CUSTOM_POST_TYPES, $postTypes ); | |
| 323 | - | |
| 324 | - return $postTypes; | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** | |
| 328 | - * put your comment there... | |
| 329 | - * | |
| 330 | 183 | * @param mixed $id |
| 331 | 184 | */ |
| 332 | 185 | public function getInfo($id) { |
| 333 | 186 | // Info fields. |
| @@ -376,46 +229,21 @@ | ||
| 376 | 229 | * put your comment there... |
| 377 | 230 | * |
| 378 | 231 | * @param mixed $block |
| 379 | 232 | */ |
| 380 | - public function update( $block, $updatePins ) | |
| 381 | - { | |
| 382 | - | |
| 383 | - $block = ( array ) $block; | |
| 384 | - | |
| 385 | - $blocks = new CJTBlocksTable( $this->dbDriver ); | |
| 386 | - $pins = new CJTBlockPinsTable( $this->dbDriver ); | |
| 387 | - | |
| 233 | + public function update($block, $updatePins) { | |
| 234 | + $block = (array) $block; // To be used by array_intersect_key. | |
| 235 | + // Create Tables objects. | |
| 236 | + $blocks = new CJTBlocksTable($this->dbDriver); | |
| 237 | + $pins = new CJTBlockPinsTable($this->dbDriver); | |
| 388 | 238 | // Update block pins if requested. |
| 389 | - if ( $updatePins ) | |
| 390 | - { | |
| 239 | + if ($updatePins) { | |
| 391 | 240 | // Isolate block pins freom native block data. |
| 392 | - $pinsData = array_intersect_key( $block, array_flip( array_keys( CJTBlockModel::getCustomPins() ) ) ); | |
| 393 | - | |
| 394 | - do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK_PINS, $block, $pinsData ); | |
| 395 | - | |
| 396 | - $pins->update( $block[ 'id' ], $pinsData ); | |
| 397 | - | |
| 241 | + $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories'))); | |
| 242 | + $pins->update($block['id'], $pinsData); | |
| 398 | 243 | } |
| 399 | - | |
| 400 | - // Update code file | |
| 401 | - if ( isset( $block[ 'activeFileId' ] ) ) | |
| 402 | - { | |
| 403 | - | |
| 404 | - $codeFile = new CJTBlockFilesTable( $this->dbDriver ); | |
| 405 | - | |
| 406 | - $codeFile->set( 'blockId', $block[ 'id' ] ) | |
| 407 | - ->set( 'id', $block[ 'activeFileId'] ) | |
| 408 | - ->set( 'code', $block[ 'code'] ) | |
| 409 | - ->save(); | |
| 410 | - } | |
| 411 | - | |
| 412 | 244 | // Isolate block fields. |
| 413 | - $blockData = array_intersect_key($block, $blocks->getFields()); | |
| 414 | - | |
| 415 | - do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK, $updatePins, $blockData ); | |
| 416 | - | |
| 417 | - $blocks->update( $blockData ); | |
| 418 | - | |
| 245 | + $block = array_intersect_key($block, $blocks->getFields()); | |
| 246 | + $blocks->update($block); | |
| 419 | 247 | } |
| 420 | 248 | |
| 421 | 249 | } // End class. |