PluginProbe
CSS & JavaScript Toolbox / 11.4
CSS & JavaScript Toolbox v11.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 / controllers / blocks-ajax.php

blocks-ajax.php in CSS & JavaScript Toolbox 11.4, at controllers/blocks-ajax.php

257 lines 7.3 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-ajax.php 21-03-2012 03:22:10 Ahmed Said $
4 */
5
6 // Disallow direct access.
7 defined('ABSPATH') or die("Access denied");
8
9 // import dependencies.
10 cssJSToolbox::import('framework:mvc:controller-ajax.inc.php');
11
12 /**
13 * Serve blocks page Ajax requests.
14 *
15 * The Actions resident here is global for only the blocks page, its not
16 * for a specific/single block. You can find single block
17 * actions in block-ajax.php file.
18 *
19 * @deprecated DONT ADD MORE ACTIONS HERE!
20 * @author Ahmed Said
21 * @version 6
22 */
23 class CJTBlocksAjaxController extends CJTAjaxController {
24
25 /**
26 * put your comment there...
27 *
28 * @var mixed
29 */
30 protected $controllerInfo = array('model' => 'blocks');
31
32 /**
33 * Initialize controller object.
34 *
35 * @see CJTController for more details
36 * @return void
37 */
38 public function __construct() {
39 parent::__construct();
40 // Register action.
41 $this->registryAction('create_block');
42 $this->registryAction('get_view');
43 $this->registryAction('save_blocks');
44 $this->registryAction('saveOrder');
45 $this->registryAction('loadBlock');
46 }
47
48 /**
49 * Create new block.
50 *
51 * Once this method is called a new block is saved into
52 * the database.
53 *
54 * Call this method using GET method with the following parameters.
55 * - array ids Ids for all the available blocks.
56 * - [name] string Block name.
57 * - [state] string Block state.
58 * - [location] string Block hook location.
59 *
60 * Response body is array with the following elements.
61 * - integer id New block id.
62 * - string view Block HTML code.
63 *
64 * @return void
65 */
66 public function createBlockAction($blockId = null, $blockType = null, $pinPoint = null, $viewName = null) {
67 $response = array();
68 // If viewName not provided read it from request vars.
69 if (!$viewName) {
70 $viewName = filter_input(INPUT_GET, 'viewName', FILTER_SANITIZE_STRING);
71 }
72 // Prepare parameters.
73 $defaultBlockName = 'block_' . hexdec(substr(md5(time()), 0, 6));
74 $wordpressMYSQLTime = current_time('mysql');
75 // Block data to insert.
76 $blockData = array(
77 'id' => $blockId,
78 'name' => $defaultBlockName,
79 'state' => null,
80 'location' => null,
81 'owner' => get_current_user_id(),
82 'created' => $wordpressMYSQLTime,
83 'lastModified' => $wordpressMYSQLTime,
84 'type' => $blockType,
85 'pinPoint' => $pinPoint,
86 );
87 // Read parameters from the request.
88 foreach ($blockData as $name => $default) {
89 // Use default if not supplied.
90 if (array_key_exists($name, $_GET)) {
91 $blockData[$name] = $_GET[$name];
92 }
93 }
94 // Import block model.
95 require_once CJTOOLBOX_MODELS_PATH . '/block.php';
96 $block = new CJTBlockModel($blockData);
97 // Add block.
98 $blocksModel =& $this->model;
99 $blockId = $blocksModel->add($block->getValues(), true);
100 $blocksModel->save();
101 // Read newly added block from database.
102 $newBlockData = $blocksModel->getBlock($blockId, array('returnCodeFile' => true));
103
104 if ($newBlockData === null) {
105 throw new Exception('Could not add new block!!!');
106 }
107 else {
108 $block->setValues($newBlockData);
109 if ($viewName ){
110 // Get block view.
111 $blockView = CJTController::getView("blocks/{$viewName}");
112 // Push vars into the view.
113 $blockView->setBlock($block);
114 $response['view'] = $blockView->getTemplate('new');
115 }
116 $response['id'] = $blockId;
117 // Set response object.
118 $this->response = $response;
119 }
120 }
121
122 /**
123 * Get view content through ajax request.
124 *
125 * The method is useful for requesting Popup forms through ajax (e.g ThickBox).
126 * You can request any view specified in the $allowedViews array.
127 *
128 * Call this method using GET method with the following parameters.
129 * - viewName string Name of the view.
130 *
131 * Response body is the view content string.
132 *
133 * @return void
134 */
135 public function getViewAction() {
136 // Some views required objects to be pushed into it before displaying
137 // the controller element is a callback that a Dummy Controller from which
138 // this variables should be pushed.
139 $allowedViews = array(
140 'blocks/new' => array(),
141 );
142 // Prepare parameters.
143 $viewName = filter_input(INPUT_GET, 'viewName', FILTER_SANITIZE_STRING);
144 if (array_key_exists($viewName, $allowedViews) === FALSE) {
145 $this->httpCode = '403 Forbidden';
146 }
147 else {
148 // Import view file.
149 $viewInfo = $allowedViews[$viewName];
150 // Get view object.
151 $view = CJTController::getView($viewName);
152 // Push view variables.
153 foreach ((isset($viewInfo['vars']) ? $viewInfo['vars'] : array()) as $var) {
154 $view->$var = $_GET["view.{$var}"];
155 }
156 // Some views required custom pushing, this is can
157 // be done by the registered controller.
158 if (isset($viewInfo['controller'])) {
159 $viewController = $viewInfo['controller'];
160 $this->$viewController($view);
161 }
162 // Set Content type.
163 $this->httpContentType = "text/html";
164 // Get view content.
165 $this->response = $view->getTemplate('default');
166 }
167 }
168
169 /**
170 * put your comment there...
171 *
172 */
173 public function loadBlockAction() {
174 // Block Id.
175 $blockId = (int) $_GET['blockId'];
176 // Get block content.
177 $view = CJTView::getInstance('blocks/cjt-block');
178 $view->setBlock(CJTModel::create('blocks')->getBlock($blockId, array('returnCodeFile' => true)));
179 // Return View content.
180 $view->getTemplate('default');
181 $this->response = $view->structuredContent;
182 }
183
184 /**
185 * put your comment there...
186 *
187 */
188 public function saveBlocksAction()
189 {
190 $response = array();
191
192 // Blocks are sent ins single array list.
193 $blocksToSave = filter_input( INPUT_POST, 'blocks', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY );
194 $calculatePinPoint = ( bool ) filter_input( INPUT_POST, 'calculatePinPoint', FILTER_SANITIZE_NUMBER_INT );
195 $createRevision = ( bool ) filter_input( INPUT_POST, 'createRevision', FILTER_SANITIZE_NUMBER_INT );
196
197 // For any reason that cause Client/JavaScript to send empty blocks,
198 // make sure we're save.
199 if ( is_array( $blocksToSave ) && ! empty( $blocksToSave ) )
200 {
201
202 foreach ( $blocksToSave as $id => $postedblockPartialData )
203 {
204 // Push block id into block data.
205 $blockData = ( object ) $postedblockPartialData;
206 $blockData->id = $id;
207
208 // Recalculate pinPoint field value.
209 ! $calculatePinPoint or ( CJTBlockModel::arrangePins( $blockData ) && CJTBlockModel::calculateBlockPinPoint( $blockData ) );
210
211 // Create block revision.
212 ! $createRevision or $this->model->addRevision( $id, $blockData->activeFileId );
213
214 // Set lastModified field to current time.
215 $blockData->lastModified = current_time( 'mysql' );
216
217 // Update database.
218 $this->model->update( $blockData, $calculatePinPoint );
219 $this->model->save();
220
221 // Send the changes properties back to client.
222 $updatedBlockData = $this->model->getBlock($id, null, array('*'), ARRAY_A);
223
224 foreach ( $updatedBlockData as $property => $value )
225 {
226 $response[ $id ][ $property ][ 'value' ] = $value;
227 }
228
229 }
230
231 }
232
233 // Delete other blocks.
234 empty( $_POST[ 'deletedBlocks' ] ) or $this->model->delete( $_POST[ 'deletedBlocks' ] );
235
236 // Save changes.
237 $this->model->save();
238
239 // Return
240 // Set response.
241 $this->response = $response;
242
243 }
244
245 /**
246 * put your comment there...
247 *
248 */
249 public function saveOrderAction() {
250 // Read order.
251 $order = array('normal' => $_GET['order']);
252 // Centralized orders to be shared between all users!
253 $this->model->setOrder($order);
254 $this->response = array('order' => $order, 'state' => 'saved');
255 }
256
257 } // End class.