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

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