| 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 |
* @deprecated Use calculatePinpoint |
| 145 |
*/ |
| 146 |
public static function calculateBlockPinPoint(&$block) { |
| 147 |
// Generate PinPoint Value. |
| 148 |
if (is_array($block->pinPoint)) { |
| 149 |
$pinPoint = 0; |
| 150 |
// Each item is a bit flag. |
| 151 |
foreach ($block->pinPoint as $pin) { |
| 152 |
$pinPoint |= hexdec($pin); |
| 153 |
} |
| 154 |
} |
| 155 |
else { |
| 156 |
// If provided as integer. |
| 157 |
$pinPoint = (int) $block->pinPoint; |
| 158 |
} |
| 159 |
// 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', |
| 165 |
self::PINS_LINKS => 'links', |
| 166 |
self::PINS_EXPRESSIONS => 'expressions', |
| 167 |
); |
| 168 |
foreach ($pins as $flag => $pin) { |
| 169 |
$pinPoint |= abs((int) (!empty($block->{$pin}))) * $flag; |
| 170 |
} |
| 171 |
// Set new pin point. |
| 172 |
$block->pinPoint = $pinPoint; |
| 173 |
return $block; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* put your comment there... |
| 178 |
* |
| 179 |
* @param mixed $block |
| 180 |
* @param mixed $pins |
| 181 |
*/ |
| 182 |
public static function calculatePinPoint($block, $pins) { |
| 183 |
// 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. |
| 187 |
return $pinPoint; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* put your comment there... |
| 192 |
* |
| 193 |
* @param mixed $values |
| 194 |
*/ |
| 195 |
public static function getInstance($values) { |
| 196 |
return new CJTBlockModel($values); |
| 197 |
} |
| 198 |
|
| 199 |
} // End class. |