| 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 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Create new block. |
| 49 |
* |
| 50 |
* Once this method is called a new block is saved into |
| 51 |
* the database. |
| 52 |
* |
| 53 |
* Call this method using GET method with the following parameters. |
| 54 |
* - array ids Ids for all the available blocks. |
| 55 |
* - [name] string Block name. |
| 56 |
* - [state] string Block state. |
| 57 |
* - [location] string Block hook location. |
| 58 |
* |
| 59 |
* Response body is array with the following elements. |
| 60 |
* - integer id New block id. |
| 61 |
* - string view Block HTML code. |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function createBlockAction($blockId = null, $blockType = null, $pinPoint = null, $viewName = null) { |
| 66 |
$response = array(); |
| 67 |
// If viewName not provided read it from request vars. |
| 68 |
if (!$viewName) { |
| 69 |
$viewName = filter_input(INPUT_GET, 'viewName', FILTER_SANITIZE_STRING); |
| 70 |
} |
| 71 |
// Prepare parameters. |
| 72 |
$defaultBlockName = 'block_' . hexdec(substr(md5(time()), 0, 6)); |
| 73 |
$wordpressMYSQLTime = current_time('mysql'); |
| 74 |
// Block data to insert. |
| 75 |
$blockData = array( |
| 76 |
'id' => $blockId, |
| 77 |
'name' => $defaultBlockName, |
| 78 |
'state' => null, |
| 79 |
'location' => null, |
| 80 |
'owner' => get_current_user_id(), |
| 81 |
'created' => $wordpressMYSQLTime, |
| 82 |
'lastModified' => $wordpressMYSQLTime, |
| 83 |
'type' => $blockType, |
| 84 |
'pinPoint' => $pinPoint, |
| 85 |
); |
| 86 |
// Read parameters from the request. |
| 87 |
foreach ($blockData as $name => $default) { |
| 88 |
// Use default if not supplied. |
| 89 |
if (array_key_exists($name, $_GET)) { |
| 90 |
$blockData[$name] = $_GET[$name]; |
| 91 |
} |
| 92 |
} |
| 93 |
// Import block model. |
| 94 |
require_once CJTOOLBOX_MODELS_PATH . '/block.php'; |
| 95 |
$block = new CJTBlockModel($blockData); |
| 96 |
// Add block. |
| 97 |
$blocksModel =& $this->model; |
| 98 |
$blockId = $blocksModel->add($block->getValues()); |
| 99 |
$blocksModel->save(); |
| 100 |
// Read newly added block from database. |
| 101 |
$newBlockData = $blocksModel->getBlock($blockId); |
| 102 |
|
| 103 |
if ($newBlockData === null) { |
| 104 |
throw new Exception('Could not add new block!!!'); |
| 105 |
} |
| 106 |
else { |
| 107 |
$block->setValues($newBlockData); |
| 108 |
if ($viewName ){ |
| 109 |
// Get block view. |
| 110 |
$blockView = CJTController::getView("blocks/{$viewName}"); |
| 111 |
// Push vars into the view. |
| 112 |
$blockView->setBlock($block); |
| 113 |
$response['view'] = $blockView->getTemplate('new'); |
| 114 |
} |
| 115 |
$response['id'] = $id; |
| 116 |
// Set response object. |
| 117 |
$this->response = $response; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get view content through ajax request. |
| 123 |
* |
| 124 |
* The method is useful for requesting Popup forms through ajax (e.g ThickBox). |
| 125 |
* You can request any view specified in the $allowedViews array. |
| 126 |
* |
| 127 |
* Call this method using GET method with the following parameters. |
| 128 |
* - viewName string Name of the view. |
| 129 |
* |
| 130 |
* Response body is the view content string. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function getViewAction() { |
| 135 |
// Some views required objects to be pushed into it before displaying |
| 136 |
// the controller element is a callback that a Dummy Controller from which |
| 137 |
// this variables should be pushed. |
| 138 |
$allowedViews = array( |
| 139 |
'blocks/new' => array(), |
| 140 |
); |
| 141 |
// Prepare parameters. |
| 142 |
$viewName = filter_input(INPUT_GET, 'viewName', FILTER_SANITIZE_STRING); |
| 143 |
if (array_key_exists($viewName, $allowedViews) === FALSE) { |
| 144 |
$this->httpCode = '403 Forbidden'; |
| 145 |
} |
| 146 |
else { |
| 147 |
// Import view file. |
| 148 |
$viewInfo = $allowedViews[$viewName]; |
| 149 |
// Get view object. |
| 150 |
$view = CJTController::getView($viewName); |
| 151 |
// Push view variables. |
| 152 |
foreach ((array) $viewInfo['vars'] as $var) { |
| 153 |
$view->$var = $_GET["view.{$var}"]; |
| 154 |
} |
| 155 |
// Some views required custom pushing, this is can |
| 156 |
// be done by the registered controller. |
| 157 |
if (isset($viewInfo['controller'])) { |
| 158 |
$viewController = $viewInfo['controller']; |
| 159 |
$this->$viewController($view); |
| 160 |
} |
| 161 |
// Set Content type. |
| 162 |
$this->httpContentType = "text/html"; |
| 163 |
// Get view content. |
| 164 |
$this->response = $view->getTemplate('default'); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* put your comment there... |
| 170 |
* |
| 171 |
*/ |
| 172 |
public function saveBlocksAction() { |
| 173 |
$response = array(); |
| 174 |
// Single block model class. |
| 175 |
require_once CJTOOLBOX_MODELS_PATH . '/block.php'; |
| 176 |
// Blocks are sent ins single array list. |
| 177 |
$blocksToSave = filter_input(INPUT_POST, 'blocks', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY); |
| 178 |
$calculatePinPoint = (bool) filter_input(INPUT_POST, 'calculatePinPoint', FILTER_SANITIZE_NUMBER_INT); |
| 179 |
$createRevision = (bool) filter_input(INPUT_POST, 'createRevision', FILTER_SANITIZE_NUMBER_INT); |
| 180 |
// For any reason that cause Client/Javascript to send empty blocks, |
| 181 |
// make sure we're save. |
| 182 |
if (is_array($blocksToSave) && !empty($blocksToSave)) { |
| 183 |
foreach ($blocksToSave as $id => $postedblockPartialData) { |
| 184 |
// Push block id into block data. |
| 185 |
$blockData = (object) $postedblockPartialData; |
| 186 |
$blockData->id = $id; |
| 187 |
// Recalculate pinPoint field value. |
| 188 |
!$calculatePinPoint or CJTBlockModel::calculateBlockPinPoint($blockData); |
| 189 |
// Create block revision. |
| 190 |
!$createRevision or $this->model->addRevision($id); |
| 191 |
// Set lastModified field to current time. |
| 192 |
$blockData->lastModified = current_time('mysql'); |
| 193 |
// Update database. |
| 194 |
$this->model->update($blockData, $calculatePinPoint); |
| 195 |
$this->model->save(); |
| 196 |
// Send the changes properties back to client. |
| 197 |
foreach ($postedblockPartialData as $property => $value) { |
| 198 |
$response[$id][$property]['value'] = $value; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
// Delete other blocks. |
| 203 |
empty($_POST['deletedBlocks']) or $this->model->delete($_POST['deletedBlocks']); |
| 204 |
// Save changes. |
| 205 |
$this->model->save(); |
| 206 |
// Set response. |
| 207 |
$this->response = $response; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* put your comment there... |
| 212 |
* |
| 213 |
*/ |
| 214 |
public function saveOrderAction() { |
| 215 |
// Read order. |
| 216 |
$order = array('normal' => $_GET['order']); |
| 217 |
// Centralized orders to be shared between all users! |
| 218 |
$this->model->setOrder($order); |
| 219 |
$this->response = array('order' => $order, 'state' => 'saved'); |
| 220 |
} |
| 221 |
|
| 222 |
} // End class. |