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