PluginProbe
CSS & JavaScript Toolbox / 6.0.7
CSS & JavaScript Toolbox v6.0.7
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.7, at models/blocks.php

248 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 $block = array_shift($blocks->get($blockId, $block['fields']));
87 // Set other fields.
88 $block->location = $block->state = '';
89 $block->parent = $blockId;
90 $block->type = 'revision';
91 $block->created = current_time('mysql');
92 $block->owner = get_current_user_id();
93 $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
94 // Add block data.
95 $blocks->insert($block);
96 // Get block pins and insert pins for the revision block.
97 $blockPins = $pins->get($blockId);
98 if (!empty($blockPins)) {
99 $pins->insertRaw($block->id, $blockPins);
100 }
101 }
102
103 /**
104 * Put your comments here...
105 *
106 *
107 * @return
108 */
109 public function dbDriver() {
110 return $this->dbDriver;
111 }
112
113 /**
114 * put your comment there...
115 *
116 * @param mixed $ids
117 */
118 public function delete($ids) {
119 // Create Tables objects.
120 $blocks = new CJTBlocksTable($this->dbDriver);
121 $pins = new CJTBlockPinsTable($this->dbDriver);
122 // Get blocks revisions.
123 $revisions['fields'] = array('id');
124 $revisions['filters']['parent'] = $ids;
125 $revisions['type'] = 'revision';
126 $revisions['result'] = $blocks->get(null, $revisions['fields'], $revisions['filters']);
127 // Revisions ids used as revision key.
128 $revisions = array_keys($revisions['result']);
129 // Delete all revisions for all "DELETED" blocks
130 // only if there is at least one revision.
131 if (!empty($revisions)) {
132 $blocks->delete($revisions);
133 $pins->delete($revisions);
134 }
135 // Delete blocks.
136 $blocks->delete($ids);
137 $pins->delete($ids);
138 // Chaining!
139 return $this;
140 }
141
142 /**
143 * put your comment there...
144 *
145 * @param mixed $id
146 * @param mixed $fields
147 */
148 public function getBlock($id, $filters = array(), $fields = array('*')) {
149 $blocks = $this->getBlocks($id, $filters, $fields);
150 $block = !empty($blocks) ? reset($blocks) : null;
151 return $block;
152 }
153
154 /**
155 * put your comment there...
156 *
157 * @param mixed $ids
158 */
159 public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array()) {
160 $blocks = array();
161 // Create Tables objects.
162 $blocksTable = new CJTBlocksTable($this->dbDriver);
163 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
164 // Read blocks.
165 $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy);
166 // Get only pins for retrieved blocks.
167 $ids = array_keys($blocks);
168 $pins = empty($ids) ? array() : $pinsTable->get($ids);
169 // Push pins into blocks.
170 foreach ($pins as $pin) {
171 // Use pin name as object member.
172 // Each pin is an array.
173 // e.g blocks[ID]->pages[] = PAGE-ID.
174 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
175 }
176 return $blocks;
177 }
178
179 /**
180 * put your comment there...
181 *
182 * @param mixed $id
183 */
184 public function getInfo($id) {
185 // Info fields.
186 $infoFields = array('name', 'id', 'owner', 'created', 'lastModified');
187 // Get info fields from db.
188 $blocksTable = new CJTBlocksTable($this->dbDriver);
189 $info = $blocksTable->get($id, $infoFields);
190 // Get user object from id.
191 $info = reset($info);
192 $info->owner = get_userdata($info->owner);
193 // Set shortcode name.
194 $info->shortcode = "[cjtoolbox id='{$info->id}']";
195 return $info;
196 }
197
198 /**
199 * put your comment there...
200 *
201 */
202 public function getOrder() {
203 return get_option('meta-box-order_cjtoolbox');
204 }
205
206 /**
207 * put your comment there...
208 *
209 */
210 public function save() {
211 $this->dbDriver->processQueue();
212 }
213
214 /**
215 * put your comment there...
216 *
217 * @param mixed $order
218 */
219 public function setOrder($order) {
220 $orderOptionName = 'meta-box-order_cjtoolbox';
221 // Update user order so That jQuery sortable Plugin will display them in correct orders!
222 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
223 // Update CENTRALIZED order in the options table!
224 update_option($orderOptionName, $order);
225 }
226
227 /**
228 * put your comment there...
229 *
230 * @param mixed $block
231 */
232 public function update($block, $updatePins) {
233 $block = (array) $block; // To be used by array_intersect_key.
234 // Create Tables objects.
235 $blocks = new CJTBlocksTable($this->dbDriver);
236 $pins = new CJTBlockPinsTable($this->dbDriver);
237 // Update block pins if requested.
238 if ($updatePins) {
239 // Isolate block pins freom native block data.
240 $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
241 $pins->update($block['id'], $pinsData);
242 }
243 // Isolate block fields.
244 $block = array_intersect_key($block, $blocks->getFields());
245 $blocks->update($block);
246 }
247
248 } // End class.