| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version $ Id; block-ajax.php 21-03-2012 03:22:10 Ahmed Said $ |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* No direct access. |
| 8 |
*/ |
| 9 |
defined('ABSPATH') or die("Access denied"); |
| 10 |
|
| 11 |
/** |
| 12 |
* Model base class. |
| 13 |
*/ |
| 14 |
require_once CJTOOLBOX_MVC_FRAMEWOK . '/model.inc.php'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Represent single block object. |
| 18 |
* |
| 19 |
* Provide simple access read or write to block properties. |
| 20 |
* |
| 21 |
* @author Ahmed Said |
| 22 |
* @version 6 |
| 23 |
*/ |
| 24 |
class CJTBlockModel extends CJTModel { |
| 25 |
|
| 26 |
/** |
| 27 |
* Global scope pins |
| 28 |
*/ |
| 29 |
const PINS_FRONTEND = 0x01; |
| 30 |
const PINS_BACKEND = 0x02; |
| 31 |
|
| 32 |
/** |
| 33 |
* Pages Pins. |
| 34 |
*/ |
| 35 |
const PINS_PAGES_ALL_PAGES = 0x10; |
| 36 |
const PINS_PAGES_CUSTOM_PAGE = 0x20; |
| 37 |
const PINS_PAGES_FRONT_PAGE = 0x40; |
| 38 |
|
| 39 |
/** |
| 40 |
* Posts Pins. |
| 41 |
*/ |
| 42 |
const PINS_POSTS_ALL_POSTS = 0x100; |
| 43 |
const PINS_POSTS_CUSTOM_POST = 0x200; |
| 44 |
const PINS_POSTS_RECENT = 0x400; |
| 45 |
const PINS_POSTS_BLOG_INDEX = 0x800; |
| 46 |
|
| 47 |
/** |
| 48 |
* Categories Pins. |
| 49 |
*/ |
| 50 |
const PINS_CATEGORIES_ALL_CATEGORIES = 0x1000; |
| 51 |
const PINS_CATEGORIES_CUSTOM_CATEGORY = 0x2000; |
| 52 |
|
| 53 |
/** |
| 54 |
* Other general pages pins. |
| 55 |
*/ |
| 56 |
const PINS_SEARCH = 0x10000; |
| 57 |
const PINS_ARCHIVE = 0x20000; |
| 58 |
const PINS_TAG = 0x40000; |
| 59 |
const PINS_AUTHOR = 0x80000; |
| 60 |
const PINS_ATTACHMENT = 0x100000; |
| 61 |
const PINS_404_ERROR = 0x200000; |
| 62 |
|
| 63 |
/** |
| 64 |
* |
| 65 |
*/ |
| 66 |
const PINS_LINKS = 0x1000000; |
| 67 |
const PINS_EXPRESSIONS = 0x2000000; |
| 68 |
|
| 69 |
/** |
| 70 |
* |
| 71 |
*/ |
| 72 |
const PINS_LINK_EXPRESSION = 0x3000000; |
| 73 |
|
| 74 |
/** |
| 75 |
* Create block object. |
| 76 |
* |
| 77 |
* @param integer Block id. |
| 78 |
* @param array Block data array or null to use default data. |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function __construct($values = array()) { |
| 82 |
$this->properties = array( |
| 83 |
'name' => null, |
| 84 |
'pinPoint' => null, |
| 85 |
'state' => null, |
| 86 |
'location' => null, |
| 87 |
'code' => null, |
| 88 |
'pages' => null, |
| 89 |
'posts' => null, |
| 90 |
'categories' => null, |
| 91 |
'links' => null, |
| 92 |
'expressions' => null, |
| 93 |
'id' => null, |
| 94 |
); |
| 95 |
// Set block properties. |
| 96 |
$this->setValues($values); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get block property value. |
| 101 |
* |
| 102 |
* @param string Property name. |
| 103 |
* @return mixed Property value. |
| 104 |
*/ |
| 105 |
public function __get($property) { |
| 106 |
switch ($property) { |
| 107 |
case 'pages': |
| 108 |
case 'posts': |
| 109 |
case 'categories': |
| 110 |
$value = ($this->properties[$property] !== null) ? $this->properties[$property] : array(); |
| 111 |
break; |
| 112 |
|
| 113 |
default: |
| 114 |
$value = $this->properties[$property]; |
| 115 |
break; |
| 116 |
} |
| 117 |
return $value; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Set block proprty value. |
| 122 |
* |
| 123 |
* @param string Property name. |
| 124 |
* @param mixed Property value. |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function __set($property, $value) { |
| 128 |
switch ($property) { |
| 129 |
case 'code': |
| 130 |
case 'links': |
| 131 |
case 'expressions': |
| 132 |
// New lines submitted to server as CRLF but displayed in browser as LF. |
| 133 |
// PHP script and JS work on two different versions of texts. |
| 134 |
// Replace CRLF with LF just as displayed in browsers. |
| 135 |
$value = preg_replace("/\x0D\x0A/", "\x0A", $value); |
| 136 |
break; |
| 137 |
} |
| 138 |
$this->properties[$property] = $value; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* put your comment there... |
| 143 |
* |
| 144 |
* @param mixed $blockData |
| 145 |
*/ |
| 146 |
public static function arrangePins(& $blockData) { |
| 147 |
// Initialize |
| 148 |
$pinsGroupNames = array_flip(array('pages', 'posts', 'categories', 'pinPoint')); |
| 149 |
$dbDriver = cssJSToolbox::getInstance()->getDBDriver(); |
| 150 |
$mdlBlock = new CJTBlocksModel(); |
| 151 |
$block = $mdlBlock->getBlock($blockData->id, array(), array('id', 'pinPoint')); |
| 152 |
$submittedPins = array_intersect_key(((array) $blockData), $pinsGroupNames); |
| 153 |
$assignedPins = array_intersect_key(((array) $block), $pinsGroupNames); |
| 154 |
// Transfer assigned PinPoint from "FLAGGED INTEGER" TO "ARRAY" like |
| 155 |
// the other pins. |
| 156 |
$assignedPins['pinPoint'] = array_keys(CJT_Models_Block_Assignmentpanel_Helpers_Auxiliary |
| 157 |
::getInstance() |
| 158 |
->getPinsArray($assignedPins['pinPoint'])); |
| 159 |
// Walk through all assigned pins. |
| 160 |
// Unassigned any item with 'value=false'. |
| 161 |
// Whenever an item is found on the submitted |
| 162 |
// pins it should be removed from the submitted list |
| 163 |
foreach ($submittedPins as $groupName => $submittedGroup) { |
| 164 |
// Get assigned pins group if found |
| 165 |
if (!isset($assignedPins[$groupName])) { |
| 166 |
// Initialize new assigned group array. |
| 167 |
$assignedPins[$groupName] = array(); |
| 168 |
} |
| 169 |
// Get assigned group array reference. |
| 170 |
$assignedGroup =& $assignedPins[$groupName]; |
| 171 |
// For every submitted item there is three types. |
| 172 |
// 1. Already assigned :WHEN: (sync == true and value == true) |
| 173 |
// 2. Unassigned :WHEN: (value == false) |
| 174 |
// 3. Newly assigned :WHEN: (sync = false). |
| 175 |
foreach ($submittedGroup as $submittedPinId => $submittedPin) { |
| 176 |
// Unassigned pin |
| 177 |
if (!$submittedPin['value']) { |
| 178 |
// Find the submittedPinId AssignedPins index. |
| 179 |
$assignedIndex = array_search($submittedPinId, $assignedGroup); |
| 180 |
// Unassigned it :REMOVE FROM ARRAY: |
| 181 |
unset($assignedGroup[$assignedIndex]); |
| 182 |
} |
| 183 |
else if (!$submittedPin['sync']) { |
| 184 |
// Add newly assigned item. |
| 185 |
$assignedGroup[] = $submittedPinId; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
// Copy all assigned pins back to the block object. |
| 190 |
foreach ($assignedPins as $groupName => $finalGroupAssigns) { |
| 191 |
// Copy the group back to the block object. |
| 192 |
$blockData->{$groupName} = $finalGroupAssigns; |
| 193 |
} |
| 194 |
// Important for caller short-circle condition. |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* put your comment there... |
| 200 |
* |
| 201 |
* @deprecated Use calculatePinpoint |
| 202 |
*/ |
| 203 |
public static function calculateBlockPinPoint(& $block) { |
| 204 |
// Generate PinPoint Value. |
| 205 |
if (isset($block->pinPoint) && is_array($block->pinPoint)) { |
| 206 |
$pinPoint = 0; |
| 207 |
// Each item is a bit flag. |
| 208 |
foreach ($block->pinPoint as $pin) { |
| 209 |
$pinPoint |= hexdec($pin); |
| 210 |
} |
| 211 |
} |
| 212 |
else { |
| 213 |
// Provided as integer or not even provided! |
| 214 |
if (!isset($block->pinPoint)) { |
| 215 |
$block->pinPoint = 0; |
| 216 |
} |
| 217 |
$pinPoint = (int) $block->pinPoint; |
| 218 |
} |
| 219 |
// Pin should be set only for not empty properties. |
| 220 |
// This help us retreiving only needed blocks when outputing blocks code. |
| 221 |
$pins = array( |
| 222 |
self::PINS_PAGES_CUSTOM_PAGE => 'pages', |
| 223 |
self::PINS_POSTS_CUSTOM_POST => 'posts', |
| 224 |
self::PINS_CATEGORIES_CUSTOM_CATEGORY => 'categories', |
| 225 |
self::PINS_LINKS => 'links', |
| 226 |
self::PINS_EXPRESSIONS => 'expressions', |
| 227 |
); |
| 228 |
foreach ($pins as $flag => $pin) { |
| 229 |
$pinPoint |= abs((int) (!empty($block->{$pin}))) * $flag; |
| 230 |
} |
| 231 |
// Set new pin point. |
| 232 |
$block->pinPoint = $pinPoint; |
| 233 |
return $block; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* put your comment there... |
| 238 |
* |
| 239 |
* @param mixed $block |
| 240 |
* @param mixed $pins |
| 241 |
*/ |
| 242 |
public static function calculatePinPoint($block, $pins) { |
| 243 |
// Add pins to Block object so that calculateBlockPinPoint can calculate PinPoint value! |
| 244 |
$block = (object) array_merge($block, $pins); |
| 245 |
$pinPoint = self::calculateBlockPinPoint($block)->pinPoint; |
| 246 |
// Return only pinPoint. |
| 247 |
return $pinPoint; |
| 248 |
} |
| 249 |
|
| 250 |
} // End class. |