| @@ -71,8 +71,32 @@ | ||
| 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 | + /** | |
| 75 | 99 | * Create block object. |
| 76 | 100 | * |
| 77 | 101 | * @param integer Block id. |
| 78 | 102 | * @param array Block data array or null to use default data. |
| @@ -77,24 +101,31 @@ | ||
| 77 | 101 | * @param integer Block id. |
| 78 | 102 | * @param array Block data array or null to use default data. |
| 79 | 103 | * @return void |
| 80 | 104 | */ |
| 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 | - ); | |
| 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 | + | |
| 95 | 126 | // Set block properties. |
| 96 | - $this->setValues($values); | |
| 127 | + $this->setValues( $values ); | |
| 97 | 128 | } |
| 98 | 129 | |
| 99 | 130 | /** |
| 100 | 131 | * Get block property value. |
| @@ -101,20 +132,18 @@ | ||
| 101 | 132 | * |
| 102 | 133 | * @param string Property name. |
| 103 | 134 | * @return mixed Property value. |
| 104 | 135 | */ |
| 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 | - } | |
| 136 | + public function __get( $property ) | |
| 137 | + { | |
| 138 | + | |
| 139 | + // Return default values | |
| 140 | + $value = ( ( $this->properties[ $property ] === null ) && | |
| 141 | + ( isset( $this->propertiesMeta[ $property ][ 'default' ] ) ) ) ? | |
| 142 | + | |
| 143 | + $this->propertiesMeta[ $property ][ 'default' ] : | |
| 144 | + $this->properties[ $property ]; | |
| 145 | + | |
| 117 | 146 | return $value; |
| 118 | 147 | } |
| 119 | 148 | |
| 120 | 149 | /** |
| @@ -123,54 +152,153 @@ | ||
| 123 | 152 | * @param string Property name. |
| 124 | 153 | * @param mixed Property value. |
| 125 | 154 | * @return void |
| 126 | 155 | */ |
| 127 | - public function __set($property, $value) { | |
| 128 | - switch ($property) { | |
| 156 | + public function __set($property, $value) | |
| 157 | + { | |
| 158 | + switch ( $property ) | |
| 159 | + { | |
| 129 | 160 | case 'code': |
| 161 | + | |
| 130 | 162 | case 'links': |
| 163 | + | |
| 131 | 164 | case 'expressions': |
| 165 | + | |
| 132 | 166 | // New lines submitted to server as CRLF but displayed in browser as LF. |
| 133 | 167 | // PHP script and JS work on two different versions of texts. |
| 134 | 168 | // Replace CRLF with LF just as displayed in browsers. |
| 135 | - $value = preg_replace("/\x0D\x0A/", "\x0A", $value); | |
| 169 | + $value = preg_replace( "/\x0D\x0A/", "\x0A", $value ); | |
| 170 | + | |
| 136 | 171 | break; |
| 137 | 172 | } |
| 138 | - $this->properties[$property] = $value; | |
| 173 | + | |
| 174 | + $this->properties[ $property ] = $value; | |
| 139 | 175 | } |
| 140 | 176 | |
| 141 | 177 | /** |
| 142 | 178 | * put your comment there... |
| 143 | 179 | * |
| 180 | + * @param mixed $blockData | |
| 181 | + */ | |
| 182 | + public static function arrangePins( & $blockData ) | |
| 183 | + { | |
| 184 | + | |
| 185 | + $pinsGroupNames = array_flip( array_merge( array_keys( self::getCustomPins() ), array( 'pinPoint' ) ) ); | |
| 186 | + $dbDriver = cssJSToolbox::getInstance()->getDBDriver(); | |
| 187 | + $mdlBlock = new CJTBlocksModel(); | |
| 188 | + $block = $mdlBlock->getBlock( $blockData->id, array(), array( 'id', 'pinPoint' ) ); | |
| 189 | + $submittedPins = array_intersect_key( ( ( array ) $blockData ), $pinsGroupNames ); | |
| 190 | + $assignedPins = array_intersect_key( ( ( array ) $block ), $pinsGroupNames ); | |
| 191 | + | |
| 192 | + // Transfer assigned PinPoint from "FLAGGED INTEGER" TO "ARRAY" like | |
| 193 | + // the other pins. | |
| 194 | + $assignedPins[ 'pinPoint' ] = array_keys( CJT_Models_Block_Assignmentpanel_Helpers_Auxiliary | |
| 195 | + ::getInstance() | |
| 196 | + ->getPinsArray( $assignedPins[ 'pinPoint' ] ) ); | |
| 197 | + | |
| 198 | + // Walk through all assigned pins. | |
| 199 | + // Unassigned any item with 'value=false'. | |
| 200 | + // Whenever an item is found on the submitted | |
| 201 | + // pins it should be removed from the submitted list | |
| 202 | + foreach ( $submittedPins as $groupName => $submittedGroup ) | |
| 203 | + { | |
| 204 | + // Get assigned pins group if found | |
| 205 | + if ( ! isset( $assignedPins[ $groupName ] ) ) | |
| 206 | + { | |
| 207 | + // Initialize new assigned group array. | |
| 208 | + $assignedPins[ $groupName ] = array(); | |
| 209 | + } | |
| 210 | + | |
| 211 | + $assignedGroup =& $assignedPins[ $groupName ]; | |
| 212 | + | |
| 213 | + // For every submitted item there is three types. | |
| 214 | + // 1. Already assigned :WHEN: (sync == true and value == true) | |
| 215 | + // 2. Unassigned :WHEN: (value == false) | |
| 216 | + // 3. Newly assigned :WHEN: (sync = false). | |
| 217 | + foreach ( $submittedGroup as $submittedPinId => $submittedPin ) | |
| 218 | + { | |
| 219 | + // Unassigned pin | |
| 220 | + if ( ! $submittedPin[ 'value' ] ) | |
| 221 | + { | |
| 222 | + // Find the submittedPinId AssignedPins index. | |
| 223 | + $assignedIndex = array_search( $submittedPinId, $assignedGroup ); | |
| 224 | + // Unassigned it :REMOVE FROM ARRAY: | |
| 225 | + unset( $assignedGroup[ $assignedIndex ] ); | |
| 226 | + } | |
| 227 | + else if ( ! $submittedPin[ 'sync' ] ) | |
| 228 | + { | |
| 229 | + // Add newly assigned item. | |
| 230 | + $assignedGroup[] = $submittedPinId; | |
| 231 | + } | |
| 232 | + | |
| 233 | + } | |
| 234 | + | |
| 235 | + } | |
| 236 | + | |
| 237 | + // Copy all assigned pins back to the block object. | |
| 238 | + foreach ( $assignedPins as $groupName => $finalGroupAssigns ) | |
| 239 | + { | |
| 240 | + $blockData->{$groupName} = $finalGroupAssigns; | |
| 241 | + } | |
| 242 | + | |
| 243 | + // Important for caller short-circle condition. | |
| 244 | + return true; | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * put your comment there... | |
| 249 | + * | |
| 144 | 250 | * @deprecated Use calculatePinpoint |
| 145 | 251 | */ |
| 146 | - public static function calculateBlockPinPoint(&$block) { | |
| 252 | + public static function calculateBlockPinPoint( & $block ) | |
| 253 | + { | |
| 254 | + | |
| 147 | 255 | // Generate PinPoint Value. |
| 148 | - if (is_array($block->pinPoint)) { | |
| 256 | + if ( isset( $block->pinPoint ) && is_array( $block->pinPoint ) ) | |
| 257 | + { | |
| 149 | 258 | $pinPoint = 0; |
| 259 | + | |
| 150 | 260 | // Each item is a bit flag. |
| 151 | - foreach ($block->pinPoint as $pin) { | |
| 261 | + foreach ( $block->pinPoint as $pin ) | |
| 262 | + { | |
| 152 | 263 | $pinPoint |= hexdec($pin); |
| 153 | 264 | } |
| 265 | + | |
| 154 | 266 | } |
| 155 | - else { | |
| 156 | - // If provided as integer. | |
| 267 | + else | |
| 268 | + { | |
| 269 | + // Provided as integer or not even provided! | |
| 270 | + if ( ! isset( $block->pinPoint ) ) | |
| 271 | + { | |
| 272 | + $block->pinPoint = 0; | |
| 273 | + } | |
| 274 | + | |
| 157 | 275 | $pinPoint = (int) $block->pinPoint; |
| 158 | 276 | } |
| 277 | + | |
| 159 | 278 | // Pin should be set only for not empty properties. |
| 160 | - // This help us retreiving only needed blocks when outputing blocks code. | |
| 161 | - $pins = array( | |
| 162 | - self::PINS_PAGES_CUSTOM_PAGE => 'pages', | |
| 163 | - self::PINS_POSTS_CUSTOM_POST => 'posts', | |
| 164 | - self::PINS_CATEGORIES_CUSTOM_CATEGORY => 'categories', | |
| 279 | + // This help us retreiving only needed blocks when querying blocks code. | |
| 280 | + $pins = array | |
| 281 | + ( | |
| 165 | 282 | self::PINS_LINKS => 'links', |
| 166 | 283 | self::PINS_EXPRESSIONS => 'expressions', |
| 167 | 284 | ); |
| 168 | - foreach ($pins as $flag => $pin) { | |
| 169 | - $pinPoint |= abs((int) (!empty($block->{$pin}))) * $flag; | |
| 285 | + | |
| 286 | + $customPins = self::getCustomPins(); | |
| 287 | + | |
| 288 | + // Add Custom Pins to pins to be calculated below | |
| 289 | + foreach ( $customPins as $customPinName => $customPin ) | |
| 290 | + { | |
| 291 | + $pins[ $customPin[ 'pinValue' ] ] = $customPinName; | |
| 170 | 292 | } |
| 171 | - // Set new pin point. | |
| 172 | - $block->pinPoint = $pinPoint; | |
| 293 | + | |
| 294 | + foreach ( $pins as $flag => $pin ) | |
| 295 | + { | |
| 296 | + $pinPoint |= abs( ( int ) ( ! empty( $block->{$pin} ) ) ) * $flag; | |
| 297 | + } | |
| 298 | + | |
| 299 | + $block->pinPoint = $pinPoint; | |
| 300 | + | |
| 173 | 301 | return $block; |
| 174 | 302 | } |
| 175 | 303 | |
| 176 | 304 | /** |
| @@ -178,22 +306,57 @@ | ||
| 178 | 306 | * |
| 179 | 307 | * @param mixed $block |
| 180 | 308 | * @param mixed $pins |
| 181 | 309 | */ |
| 182 | - public static function calculatePinPoint($block, $pins) { | |
| 310 | + public static function calculatePinPoint( $block, $pins ) | |
| 311 | + { | |
| 183 | 312 | // Add pins to Block object so that calculateBlockPinPoint can calculate PinPoint value! |
| 184 | - $block = (object) array_merge($block, $pins); | |
| 185 | - $pinPoint = self::calculateBlockPinPoint($block)->pinPoint; | |
| 186 | - // Return only pinPoint. | |
| 313 | + $block = ( object ) array_merge( $block, $pins ); | |
| 314 | + | |
| 315 | + $pinPoint = self::calculateBlockPinPoint( $block )->pinPoint; | |
| 316 | + | |
| 187 | 317 | return $pinPoint; |
| 188 | 318 | } |
| 189 | - | |
| 319 | + | |
| 190 | 320 | /** |
| 191 | 321 | * put your comment there... |
| 192 | 322 | * |
| 193 | - * @param mixed $values | |
| 194 | 323 | */ |
| 195 | - public static function getInstance($values) { | |
| 196 | - return new CJTBlockModel($values); | |
| 324 | + public static function getCustomPins() | |
| 325 | + { | |
| 326 | + static $pins = array | |
| 327 | + ( | |
| 328 | + 'pages' => array | |
| 329 | + ( | |
| 330 | + | |
| 331 | + 'default' => array(), | |
| 332 | + 'pinValue' => self::PINS_PAGES_CUSTOM_PAGE | |
| 333 | + | |
| 334 | + ), | |
| 335 | + | |
| 336 | + 'posts' => array | |
| 337 | + ( | |
| 338 | + | |
| 339 | + 'default' => array(), | |
| 340 | + 'pinValue' => self::PINS_POSTS_CUSTOM_POST | |
| 341 | + | |
| 342 | + ), | |
| 343 | + | |
| 344 | + 'categories' => array | |
| 345 | + ( | |
| 346 | + | |
| 347 | + 'default' => array(), | |
| 348 | + 'pinValue' => self::PINS_CATEGORIES_CUSTOM_CATEGORY | |
| 349 | + | |
| 350 | + ), | |
| 351 | + ); | |
| 352 | + | |
| 353 | + // Cache custom pins | |
| 354 | + if ( ! self::$customPins ) | |
| 355 | + { | |
| 356 | + self::$customPins = apply_filters( CJTPluggableHelper::FILTER_BLOCK_MODEL_CUSTOM_PINS, $pins ); | |
| 357 | + } | |
| 358 | + | |
| 359 | + return self::$customPins; | |
| 197 | 360 | } |
| 198 | 361 | |
| 199 | 362 | } // End class. |