| @@ -14,11 +14,11 @@ | ||
| 14 | 14 | require_once CJTOOLBOX_MVC_FRAMEWOK . '/model.inc.php'; |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * Represent single block object. |
| 18 | -* | |
| 18 | +* | |
| 19 | 19 | * Provide simple access read or write to block properties. |
| 20 | -* | |
| 20 | +* | |
| 21 | 21 | * @author Ahmed Said |
| 22 | 22 | * @version 6 |
| 23 | 23 | */ |
| 24 | 24 | class CJTBlockModel extends CJTModel { |
| @@ -27,9 +27,9 @@ | ||
| 27 | 27 | * Global scope pins |
| 28 | 28 | */ |
| 29 | 29 | const PINS_FRONTEND = 0x01; |
| 30 | 30 | const PINS_BACKEND = 0x02; |
| 31 | - | |
| 31 | + | |
| 32 | 32 | /** |
| 33 | 33 | * Pages Pins. |
| 34 | 34 | */ |
| 35 | 35 | const PINS_PAGES_ALL_PAGES = 0x10; |
| @@ -34,23 +34,23 @@ | ||
| 34 | 34 | */ |
| 35 | 35 | const PINS_PAGES_ALL_PAGES = 0x10; |
| 36 | 36 | const PINS_PAGES_CUSTOM_PAGE = 0x20; |
| 37 | 37 | const PINS_PAGES_FRONT_PAGE = 0x40; |
| 38 | - | |
| 38 | + | |
| 39 | 39 | /** |
| 40 | 40 | * Posts Pins. |
| 41 | - */ | |
| 41 | + */ | |
| 42 | 42 | const PINS_POSTS_ALL_POSTS = 0x100; |
| 43 | 43 | const PINS_POSTS_CUSTOM_POST = 0x200; |
| 44 | 44 | const PINS_POSTS_RECENT = 0x400; |
| 45 | 45 | const PINS_POSTS_BLOG_INDEX = 0x800; |
| 46 | - | |
| 46 | + | |
| 47 | 47 | /** |
| 48 | 48 | * Categories Pins. |
| 49 | 49 | */ |
| 50 | 50 | const PINS_CATEGORIES_ALL_CATEGORIES = 0x1000; |
| 51 | 51 | const PINS_CATEGORIES_CUSTOM_CATEGORY = 0x2000; |
| 52 | - | |
| 52 | + | |
| 53 | 53 | /** |
| 54 | 54 | * Other general pages pins. |
| 55 | 55 | */ |
| 56 | 56 | const PINS_SEARCH = 0x10000; |
| @@ -58,188 +58,140 @@ | ||
| 58 | 58 | const PINS_TAG = 0x40000; |
| 59 | 59 | const PINS_AUTHOR = 0x80000; |
| 60 | 60 | const PINS_ATTACHMENT = 0x100000; |
| 61 | 61 | const PINS_404_ERROR = 0x200000; |
| 62 | - | |
| 62 | + | |
| 63 | 63 | /** |
| 64 | - * | |
| 64 | + * | |
| 65 | 65 | */ |
| 66 | 66 | const PINS_LINKS = 0x1000000; |
| 67 | 67 | const PINS_EXPRESSIONS = 0x2000000; |
| 68 | - | |
| 68 | + | |
| 69 | 69 | /** |
| 70 | - * | |
| 70 | + * | |
| 71 | 71 | */ |
| 72 | 72 | const PINS_LINK_EXPRESSION = 0x3000000; |
| 73 | - | |
| 73 | + | |
| 74 | 74 | /** |
| 75 | - * put your comment there... | |
| 76 | - * | |
| 77 | - * @var mixed | |
| 78 | - */ | |
| 79 | - private static $customPins; | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * put your comment there... | |
| 83 | - * | |
| 84 | - * @var mixed | |
| 85 | - */ | |
| 86 | - private $propertiesMeta = array | |
| 87 | - ( | |
| 88 | - 'name' => array(), | |
| 89 | - 'pinPoint' => array(), | |
| 90 | - 'state' => array(), | |
| 91 | - 'location' => array(), | |
| 92 | - 'code' => array(), | |
| 93 | - 'links' => array(), | |
| 94 | - 'expressions' => array(), | |
| 95 | - 'id' => array(), | |
| 96 | - ); | |
| 97 | - | |
| 98 | - /** | |
| 99 | 75 | * Create block object. |
| 100 | - * | |
| 76 | + * | |
| 101 | 77 | * @param integer Block id. |
| 102 | 78 | * @param array Block data array or null to use default data. |
| 103 | 79 | * @return void |
| 104 | 80 | */ |
| 105 | - public function __construct( $values = array() ) | |
| 106 | - { | |
| 107 | - // Allow pluggable custom pins | |
| 108 | - $customPins = self::getCustomPins(); | |
| 109 | - | |
| 110 | - // Allow pluggable fields | |
| 111 | - $this->propertiesMeta = apply_filters( CJTPluggableHelper::FILTER_BLOCK_MODEL_PROPERTIES_META, $this->propertiesMeta ); | |
| 112 | - | |
| 113 | - // Merge prpperties meta wioth custom pins to create one full propertiesMeta objetc | |
| 114 | - // that hold both fields and custom pins fields | |
| 115 | - $this->propertiesMeta = array_merge( $this->propertiesMeta, $customPins ); | |
| 116 | - | |
| 117 | - // Properties backward compatibolity to hold all available properties | |
| 118 | - $this->properties = array_combine( | |
| 119 | - | |
| 120 | - array_keys( $this->propertiesMeta ), | |
| 121 | - | |
| 122 | - array_fill( 0, count( $this->propertiesMeta ), null ) | |
| 123 | - | |
| 124 | - ); | |
| 125 | - | |
| 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 | + ); | |
| 126 | 95 | // Set block properties. |
| 127 | - $this->setValues( $values ); | |
| 96 | + $this->setValues($values); | |
| 128 | 97 | } |
| 129 | - | |
| 98 | + | |
| 130 | 99 | /** |
| 131 | 100 | * Get block property value. |
| 132 | - * | |
| 101 | + * | |
| 133 | 102 | * @param string Property name. |
| 134 | 103 | * @return mixed Property value. |
| 135 | 104 | */ |
| 136 | - public function __get( $property ) | |
| 137 | - { | |
| 138 | - // Return default values | |
| 139 | - $value = ( isset( $this->properties[ $property ] ) && ( $this->properties[ $property ] === null ) && | |
| 140 | - ( isset( $this->propertiesMeta[ $property ][ 'default' ] ) ) ) ? | |
| 141 | - | |
| 142 | - $this->propertiesMeta[ $property ][ 'default' ] : | |
| 143 | - ( isset( $this->properties[ $property ] ) ? $this->properties[ $property ] : null ); | |
| 144 | - | |
| 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 | + } | |
| 145 | 117 | return $value; |
| 146 | 118 | } |
| 147 | - | |
| 119 | + | |
| 148 | 120 | /** |
| 149 | 121 | * Set block proprty value. |
| 150 | - * | |
| 122 | + * | |
| 151 | 123 | * @param string Property name. |
| 152 | 124 | * @param mixed Property value. |
| 153 | 125 | * @return void |
| 154 | 126 | */ |
| 155 | - public function __set($property, $value) | |
| 156 | - { | |
| 157 | - switch ( $property ) | |
| 158 | - { | |
| 127 | + public function __set($property, $value) { | |
| 128 | + switch ($property) { | |
| 159 | 129 | case 'code': |
| 160 | - | |
| 161 | 130 | case 'links': |
| 162 | - | |
| 163 | 131 | case 'expressions': |
| 164 | - | |
| 165 | 132 | // New lines submitted to server as CRLF but displayed in browser as LF. |
| 166 | 133 | // PHP script and JS work on two different versions of texts. |
| 167 | 134 | // Replace CRLF with LF just as displayed in browsers. |
| 168 | - $value = preg_replace( "/\x0D\x0A/", "\x0A", $value ); | |
| 169 | - | |
| 135 | + $value = preg_replace("/\x0D\x0A/", "\x0A", $value); | |
| 170 | 136 | break; |
| 171 | 137 | } |
| 172 | - | |
| 173 | - $this->properties[ $property ] = $value; | |
| 138 | + $this->properties[$property] = $value; | |
| 174 | 139 | } |
| 175 | - | |
| 140 | + | |
| 176 | 141 | /** |
| 177 | 142 | * put your comment there... |
| 178 | - * | |
| 143 | + * | |
| 179 | 144 | * @param mixed $blockData |
| 180 | 145 | */ |
| 181 | - public static function arrangePins( & $blockData ) | |
| 182 | - { | |
| 183 | - | |
| 184 | - $pinsGroupNames = array_flip( array_merge( array_keys( self::getCustomPins() ), array( 'pinPoint' ) ) ); | |
| 146 | + public static function arrangePins(& $blockData) { | |
| 147 | + // Initialize | |
| 148 | + $pinsGroupNames = array_flip(array('pages', 'posts', 'categories', 'pinPoint')); | |
| 185 | 149 | $dbDriver = cssJSToolbox::getInstance()->getDBDriver(); |
| 186 | 150 | $mdlBlock = new CJTBlocksModel(); |
| 187 | - $block = $mdlBlock->getBlock( $blockData->id, array(), array( 'id', 'pinPoint' ) ); | |
| 188 | - $submittedPins = array_intersect_key( ( ( array ) $blockData ), $pinsGroupNames ); | |
| 189 | - $assignedPins = array_intersect_key( ( ( array ) $block ), $pinsGroupNames ); | |
| 190 | - | |
| 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); | |
| 191 | 154 | // Transfer assigned PinPoint from "FLAGGED INTEGER" TO "ARRAY" like |
| 192 | 155 | // the other pins. |
| 193 | - $assignedPins[ 'pinPoint' ] = array_keys( CJT_Models_Block_Assignmentpanel_Helpers_Auxiliary | |
| 156 | + $assignedPins['pinPoint'] = array_keys(CJT_Models_Block_Assignmentpanel_Helpers_Auxiliary | |
| 194 | 157 | ::getInstance() |
| 195 | - ->getPinsArray( $assignedPins[ 'pinPoint' ] ) ); | |
| 196 | - | |
| 158 | + ->getPinsArray($assignedPins['pinPoint'])); | |
| 197 | 159 | // Walk through all assigned pins. |
| 198 | 160 | // Unassigned any item with 'value=false'. |
| 199 | 161 | // Whenever an item is found on the submitted |
| 200 | 162 | // pins it should be removed from the submitted list |
| 201 | - foreach ( $submittedPins as $groupName => $submittedGroup ) | |
| 202 | - { | |
| 203 | - // Get assigned pins group if found | |
| 204 | - if ( ! isset( $assignedPins[ $groupName ] ) ) | |
| 205 | - { | |
| 163 | + foreach ($submittedPins as $groupName => $submittedGroup) { | |
| 164 | + // Get assigned pins group if found | |
| 165 | + if (!isset($assignedPins[$groupName])) { | |
| 206 | 166 | // Initialize new assigned group array. |
| 207 | - $assignedPins[ $groupName ] = array(); | |
| 167 | + $assignedPins[$groupName] = array(); | |
| 208 | 168 | } |
| 209 | - | |
| 210 | - $assignedGroup =& $assignedPins[ $groupName ]; | |
| 211 | - | |
| 169 | + // Get assigned group array reference. | |
| 170 | + $assignedGroup =& $assignedPins[$groupName]; | |
| 212 | 171 | // For every submitted item there is three types. |
| 213 | 172 | // 1. Already assigned :WHEN: (sync == true and value == true) |
| 214 | 173 | // 2. Unassigned :WHEN: (value == false) |
| 215 | 174 | // 3. Newly assigned :WHEN: (sync = false). |
| 216 | - foreach ( $submittedGroup as $submittedPinId => $submittedPin ) | |
| 217 | - { | |
| 175 | + foreach ($submittedGroup as $submittedPinId => $submittedPin) { | |
| 218 | 176 | // Unassigned pin |
| 219 | - if ( ! $submittedPin[ 'value' ] ) | |
| 220 | - { | |
| 177 | + if (!$submittedPin['value']) { | |
| 221 | 178 | // Find the submittedPinId AssignedPins index. |
| 222 | - $assignedIndex = array_search( $submittedPinId, $assignedGroup ); | |
| 179 | + $assignedIndex = array_search($submittedPinId, $assignedGroup); | |
| 223 | 180 | // Unassigned it :REMOVE FROM ARRAY: |
| 224 | - unset( $assignedGroup[ $assignedIndex ] ); | |
| 181 | + unset($assignedGroup[$assignedIndex]); | |
| 225 | 182 | } |
| 226 | - else if ( ! $submittedPin[ 'sync' ] ) | |
| 227 | - { | |
| 183 | + else if (!$submittedPin['sync']) { | |
| 228 | 184 | // Add newly assigned item. |
| 229 | 185 | $assignedGroup[] = $submittedPinId; |
| 230 | 186 | } |
| 231 | - | |
| 232 | 187 | } |
| 233 | - | |
| 234 | 188 | } |
| 235 | - | |
| 236 | 189 | // Copy all assigned pins back to the block object. |
| 237 | - foreach ( $assignedPins as $groupName => $finalGroupAssigns ) | |
| 238 | - { | |
| 190 | + foreach ($assignedPins as $groupName => $finalGroupAssigns) { | |
| 191 | + // Copy the group back to the block object. | |
| 239 | 192 | $blockData->{$groupName} = $finalGroupAssigns; |
| 240 | 193 | } |
| 241 | - | |
| 242 | 194 | // Important for caller short-circle condition. |
| 243 | 195 | return true; |
| 244 | 196 | } |
| 245 | 197 | |
| @@ -244,118 +196,55 @@ | ||
| 244 | 196 | } |
| 245 | 197 | |
| 246 | 198 | /** |
| 247 | 199 | * put your comment there... |
| 248 | - * | |
| 200 | + * | |
| 249 | 201 | * @deprecated Use calculatePinpoint |
| 250 | 202 | */ |
| 251 | - public static function calculateBlockPinPoint( & $block ) | |
| 252 | - { | |
| 253 | - | |
| 203 | + public static function calculateBlockPinPoint(& $block) { | |
| 254 | 204 | // Generate PinPoint Value. |
| 255 | - if ( isset( $block->pinPoint ) && is_array( $block->pinPoint ) ) | |
| 256 | - { | |
| 205 | + if (isset($block->pinPoint) && is_array($block->pinPoint)) { | |
| 257 | 206 | $pinPoint = 0; |
| 258 | - | |
| 259 | 207 | // Each item is a bit flag. |
| 260 | - foreach ( $block->pinPoint as $pin ) | |
| 261 | - { | |
| 208 | + foreach ($block->pinPoint as $pin) { | |
| 262 | 209 | $pinPoint |= hexdec($pin); |
| 263 | 210 | } |
| 264 | - | |
| 265 | 211 | } |
| 266 | - else | |
| 267 | - { | |
| 212 | + else { | |
| 268 | 213 | // Provided as integer or not even provided! |
| 269 | - if ( ! isset( $block->pinPoint ) ) | |
| 270 | - { | |
| 214 | + if (!isset($block->pinPoint)) { | |
| 271 | 215 | $block->pinPoint = 0; |
| 272 | 216 | } |
| 273 | - | |
| 274 | 217 | $pinPoint = (int) $block->pinPoint; |
| 275 | 218 | } |
| 276 | - | |
| 277 | 219 | // Pin should be set only for not empty properties. |
| 278 | - // This help us retreiving only needed blocks when querying blocks code. | |
| 279 | - $pins = array | |
| 280 | - ( | |
| 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', | |
| 281 | 225 | self::PINS_LINKS => 'links', |
| 282 | 226 | self::PINS_EXPRESSIONS => 'expressions', |
| 283 | 227 | ); |
| 284 | - | |
| 285 | - $customPins = self::getCustomPins(); | |
| 286 | - | |
| 287 | - // Add Custom Pins to pins to be calculated below | |
| 288 | - foreach ( $customPins as $customPinName => $customPin ) | |
| 289 | - { | |
| 290 | - $pins[ $customPin[ 'pinValue' ] ] = $customPinName; | |
| 228 | + foreach ($pins as $flag => $pin) { | |
| 229 | + $pinPoint |= abs((int) (!empty($block->{$pin}))) * $flag; | |
| 291 | 230 | } |
| 292 | - | |
| 293 | - foreach ( $pins as $flag => $pin ) | |
| 294 | - { | |
| 295 | - $pinPoint |= abs( ( int ) ( ! empty( $block->{$pin} ) ) ) * $flag; | |
| 296 | - } | |
| 297 | - | |
| 298 | - $block->pinPoint = $pinPoint; | |
| 299 | - | |
| 231 | + // Set new pin point. | |
| 232 | + $block->pinPoint = $pinPoint; | |
| 300 | 233 | return $block; |
| 301 | 234 | } |
| 302 | - | |
| 235 | + | |
| 303 | 236 | /** |
| 304 | 237 | * put your comment there... |
| 305 | - * | |
| 238 | + * | |
| 306 | 239 | * @param mixed $block |
| 307 | 240 | * @param mixed $pins |
| 308 | 241 | */ |
| 309 | - public static function calculatePinPoint( $block, $pins ) | |
| 310 | - { | |
| 242 | + public static function calculatePinPoint($block, $pins) { | |
| 311 | 243 | // Add pins to Block object so that calculateBlockPinPoint can calculate PinPoint value! |
| 312 | - $block = ( object ) array_merge( $block, $pins ); | |
| 313 | - | |
| 314 | - $pinPoint = self::calculateBlockPinPoint( $block )->pinPoint; | |
| 315 | - | |
| 244 | + $block = (object) array_merge($block, $pins); | |
| 245 | + $pinPoint = self::calculateBlockPinPoint($block)->pinPoint; | |
| 246 | + // Return only pinPoint. | |
| 316 | 247 | return $pinPoint; |
| 317 | - } | |
| 318 | - | |
| 319 | - /** | |
| 320 | - * put your comment there... | |
| 321 | - * | |
| 322 | - */ | |
| 323 | - public static function getCustomPins() | |
| 324 | - { | |
| 325 | - static $pins = array | |
| 326 | - ( | |
| 327 | - 'pages' => array | |
| 328 | - ( | |
| 329 | - | |
| 330 | - 'default' => array(), | |
| 331 | - 'pinValue' => self::PINS_PAGES_CUSTOM_PAGE | |
| 332 | - | |
| 333 | - ), | |
| 334 | - | |
| 335 | - 'posts' => array | |
| 336 | - ( | |
| 337 | - | |
| 338 | - 'default' => array(), | |
| 339 | - 'pinValue' => self::PINS_POSTS_CUSTOM_POST | |
| 340 | - | |
| 341 | - ), | |
| 342 | - | |
| 343 | - 'categories' => array | |
| 344 | - ( | |
| 345 | - | |
| 346 | - 'default' => array(), | |
| 347 | - 'pinValue' => self::PINS_CATEGORIES_CUSTOM_CATEGORY | |
| 348 | - | |
| 349 | - ), | |
| 350 | - ); | |
| 351 | - | |
| 352 | - // Cache custom pins | |
| 353 | - if ( ! self::$customPins ) | |
| 354 | - { | |
| 355 | - self::$customPins = apply_filters( CJTPluggableHelper::FILTER_BLOCK_MODEL_CUSTOM_PINS, $pins ); | |
| 356 | - } | |
| 357 | - | |
| 358 | - return self::$customPins; | |
| 359 | 248 | } |
| 360 | 249 | |
| 361 | 250 | } // End class. |