| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disallow direct access. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
abstract class CJTInstallerBlock extends ArrayIterator { |
| 13 |
|
| 14 |
/** |
| 15 |
* put your comment there... |
| 16 |
* |
| 17 |
* @var CJTBlocksModel |
| 18 |
*/ |
| 19 |
public $model; |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @param mixed $blocks |
| 25 |
* @return CJTInstallerBlock |
| 26 |
*/ |
| 27 |
public function __construct($blocks) { |
| 28 |
// Initialize! |
| 29 |
$this->model = CJTModel::getInstance('blocks'); |
| 30 |
// Initialize Array Iterator class! |
| 31 |
parent::__construct(is_array($blocks) ? $blocks : array()); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* put your comment there... |
| 36 |
* |
| 37 |
*/ |
| 38 |
public function id() { |
| 39 |
return parent::key() + 1; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
*/ |
| 46 |
public function upgrade() { |
| 47 |
// Read block! |
| 48 |
$srcBlock =& $this[$this->key()]; |
| 49 |
// Build block in new structure! |
| 50 |
$block = array_diff_key($srcBlock, array_flip(array('page', 'category'))); |
| 51 |
$pins = array(); |
| 52 |
// Set interna data! |
| 53 |
$block['id'] = $this->id(); |
| 54 |
$block['created'] = $block['lastModified'] = current_time('mysql'); |
| 55 |
$block['owner'] = get_current_user_id(); |
| 56 |
// Translate old assignment panel to use the new structure! |
| 57 |
if (isset($srcBlock['category'])) { |
| 58 |
$pins['categories'] = $srcBlock['category']; |
| 59 |
} |
| 60 |
// Translate named map from last versions to the value used in the new versions! |
| 61 |
CJTModel::import('block'); // Import CJTBlockModel |
| 62 |
$namedPins = array( |
| 63 |
'allpages' => CJTBlockModel::PINS_PAGES_ALL_PAGES, |
| 64 |
'allposts' => CJTBlockModel::PINS_POSTS_ALL_POSTS, |
| 65 |
'frontpage' => CJTBlockModel::PINS_PAGES_FRONT_PAGE, |
| 66 |
); |
| 67 |
foreach ((isset($srcBlock['page']) ? $srcBlock['page'] : array()) as $assignedObject) { |
| 68 |
// Translate named pin to flag! |
| 69 |
if (isset($namedPins[$assignedObject])) { |
| 70 |
// Set pinPoint flags! |
| 71 |
$block['pinPoint'][] = dechex($namedPins[$assignedObject]); |
| 72 |
} |
| 73 |
else { // Previous versions support only pages but not posts! |
| 74 |
$pins['pages'][] = $assignedObject; |
| 75 |
} |
| 76 |
} |
| 77 |
// Calculate Pin Points! |
| 78 |
$block['pinPoint'] = CJTBlockModel::calculatePinPoint($block, $pins); |
| 79 |
// Create new Block! |
| 80 |
$this->model->add($block); |
| 81 |
// Save Block pins/assigned objects as it doesnt saved when created! |
| 82 |
$pins['id'] = $block['id']; |
| 83 |
$this->model->update($pins, true); |
| 84 |
// Chaining |
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
} // End class. |
| 89 |
|