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