PluginProbe
CSS & JavaScript Toolbox / 6.0.15
CSS & JavaScript Toolbox v6.0.15
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / models / blocks.php

blocks.php in CSS & JavaScript Toolbox 6.0.15, at models/blocks.php

249 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version $ Id; blocks.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 // Blocks Database tables.
12 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
13 require_once CJTOOLBOX_TABLES_PATH . '/block-pins.php';
14 // MYSQL Queue Driver.
15 require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/queue-driver.inc.php';
16
17 /**
18 * Provide simple access (read or write) to all Blocks data.
19 *
20 * @author Ahmed Said
21 * @version 6
22 */
23 class CJTBlocksModel {
24
25 /**
26 *
27 */
28 const MAX_REVISIONS_PER_BLOCK = 10;
29
30 /**
31 * put your comment there...
32 *
33 * @var mixed
34 */
35 protected $dbDriver = null;
36
37 /**
38 * put your comment there...
39 *
40 */
41 public function __construct() {
42 // Initialize CTTTable MYSQL Driver.
43 $this->dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']);
44 }
45
46 /**
47 * put your comment there...
48 *
49 * @param mixed $data
50 * @param mixed $pins
51 * @param mixed $customPins
52 */
53 public function add($block) {
54 // Make sure it array and not stdClass as it has been used from various areas!
55 $block = (array) $block;
56 // Create Tables objects.
57 $blocks = new CJTBlocksTable($this->dbDriver);
58 // Get new id if not specified.
59 if (!$block['id']) {
60 $block['id'] = $blocks->getNextId();
61 }
62 $blocks->insert($block);
63 return $block['id'];
64 }
65
66 /**
67 * put your comment there...
68 *
69 * @param mixed $id
70 */
71 public function addRevision($blockId) {
72 // Create Tables objects.
73 $blocks = new CJTBlocksTable($this->dbDriver);
74 $pins = new CJTBlockPinsTable($this->dbDriver);
75 // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per block.
76 $revisions['fields'] = array('id');
77 $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId);
78 $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']);
79 // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one.
80 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
81 $this->delete(array_shift($revisions)->id);
82 }
83 // Get block data.
84 $block['fields'] = array('id', 'lastModified', 'pinPoint', 'code', 'links', 'expressions');
85 // get() developed to return multiple blocks, fetch the first.
86 $result = $blocks->get($blockId, $block['fields']);
87 $block = reset($result);
88 // Set other fields.
89 $block->location = $block->state = '';
90 $block->parent = $blockId;
91 $block->type = 'revision';
92 $block->created = current_time('mysql');
93 $block->owner = get_current_user_id();
94 $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
95 // Add block data.
96 $blocks->insert($block);
97 // Get block pins and insert pins for the revision block.
98 $blockPins = $pins->get($blockId);
99 if (!empty($blockPins)) {
100 $pins->insertRaw($block->id, $blockPins);
101 }
102 }
103
104 /**
105 * Put your comments here...
106 *
107 *
108 * @return
109 */
110 public function dbDriver() {
111 return $this->dbDriver;
112 }
113
114 /**
115 * put your comment there...
116 *
117 * @param mixed $ids
118 */
119 public function delete($ids) {
120 // Create Tables objects.
121 $blocks = new CJTBlocksTable($this->dbDriver);
122 $pins = new CJTBlockPinsTable($this->dbDriver);
123 // Get blocks revisions.
124 $revisions['fields'] = array('id');
125 $revisions['filters']['parent'] = $ids;
126 $revisions['type'] = 'revision';
127 $revisions['result'] = $blocks->get(null, $revisions['fields'], $revisions['filters']);
128 // Revisions ids used as revision key.
129 $revisions = array_keys($revisions['result']);
130 // Delete all revisions for all "DELETED" blocks
131 // only if there is at least one revision.
132 if (!empty($revisions)) {
133 $blocks->delete($revisions);
134 $pins->delete($revisions);
135 }
136 // Delete blocks.
137 $blocks->delete($ids);
138 $pins->delete($ids);
139 // Chaining!
140 return $this;
141 }
142
143 /**
144 * put your comment there...
145 *
146 * @param mixed $id
147 * @param mixed $fields
148 */
149 public function getBlock($id, $filters = array(), $fields = array('*')) {
150 $blocks = $this->getBlocks($id, $filters, $fields);
151 $block = !empty($blocks) ? reset($blocks) : null;
152 return $block;
153 }
154
155 /**
156 * put your comment there...
157 *
158 * @param mixed $ids
159 */
160 public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array()) {
161 $blocks = array();
162 // Create Tables objects.
163 $blocksTable = new CJTBlocksTable($this->dbDriver);
164 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
165 // Read blocks.
166 $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy);
167 // Get only pins for retrieved blocks.
168 $ids = array_keys($blocks);
169 $pins = empty($ids) ? array() : $pinsTable->get($ids);
170 // Push pins into blocks.
171 foreach ($pins as $pin) {
172 // Use pin name as object member.
173 // Each pin is an array.
174 // e.g blocks[ID]->pages[] = PAGE-ID.
175 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
176 }
177 return $blocks;
178 }
179
180 /**
181 * put your comment there...
182 *
183 * @param mixed $id
184 */
185 public function getInfo($id) {
186 // Info fields.
187 $infoFields = array('name', 'id', 'owner', 'created', 'lastModified');
188 // Get info fields from db.
189 $blocksTable = new CJTBlocksTable($this->dbDriver);
190 $info = $blocksTable->get($id, $infoFields);
191 // Get user object from id.
192 $info = reset($info);
193 $info->owner = get_userdata($info->owner);
194 // Set shortcode name.
195 $info->shortcode = "[cjtoolbox name='{$info->name}']";
196 return $info;
197 }
198
199 /**
200 * put your comment there...
201 *
202 */
203 public function getOrder() {
204 return get_option('meta-box-order_cjtoolbox');
205 }
206
207 /**
208 * put your comment there...
209 *
210 */
211 public function save() {
212 $this->dbDriver->processQueue();
213 }
214
215 /**
216 * put your comment there...
217 *
218 * @param mixed $order
219 */
220 public function setOrder($order) {
221 $orderOptionName = 'meta-box-order_cjtoolbox';
222 // Update user order so That jQuery sortable Plugin will display them in correct orders!
223 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
224 // Update CENTRALIZED order in the options table!
225 update_option($orderOptionName, $order);
226 }
227
228 /**
229 * put your comment there...
230 *
231 * @param mixed $block
232 */
233 public function update($block, $updatePins) {
234 $block = (array) $block; // To be used by array_intersect_key.
235 // Create Tables objects.
236 $blocks = new CJTBlocksTable($this->dbDriver);
237 $pins = new CJTBlockPinsTable($this->dbDriver);
238 // Update block pins if requested.
239 if ($updatePins) {
240 // Isolate block pins freom native block data.
241 $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
242 $pins->update($block['id'], $pinsData);
243 }
244 // Isolate block fields.
245 $block = array_intersect_key($block, $blocks->getFields());
246 $blocks->update($block);
247 }
248
249 } // End class.