| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version $ Id; block-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 |
* Server single block OR block specific actions. |
| 14 |
* |
| 15 |
* @author Ahmed Said |
| 16 |
* @version 6 |
| 17 |
* @deprecated DONT ADD MORE ACTIONS. USE CJTBlockController and other controllers instead! |
| 18 |
*/ |
| 19 |
class CJTBlockAjaxController extends CJTAjaxController { |
| 20 |
|
| 21 |
/** |
| 22 |
* Initialize controller object. |
| 23 |
* |
| 24 |
* @see CJTController for more details |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
parent::__construct(); |
| 29 |
// Supported actions. |
| 30 |
add_action('wp_ajax_cjtoolbox_get_info_view', array(&$this, '_doAction')); |
| 31 |
add_action('wp_ajax_cjtoolbox_set_property', array(&$this, '_doAction')); |
| 32 |
add_action('wp_ajax_cjtoolbox_get_revision', array(&$this, '_doAction')); |
| 33 |
add_action('wp_ajax_cjtoolbox_get_revisions', array(&$this, '_doAction')); |
| 34 |
// Redirects |
| 35 |
$this->registryAction('getBlockBy'); |
| 36 |
$this->registryAction('getAPOP'); |
| 37 |
$this->registryAction('restoreRevision'); |
| 38 |
$this->registryAction('loadUrl'); |
| 39 |
$this->registryAction('downloadCodeFile'); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
* @deprecated this is just a redirect to the CJTBlockContoller::getAction(). |
| 46 |
*/ |
| 47 |
protected function downloadCodeFileAction() { |
| 48 |
// Pass to CJTBlockController! |
| 49 |
$this->redirect('block'); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* put your comment there... |
| 54 |
* |
| 55 |
* @deprecated this is just a redirect to the CJTBlockContoller::getAction(). |
| 56 |
*/ |
| 57 |
protected function getAPOPAction() { |
| 58 |
// Pass to CJTBlockController! |
| 59 |
$this->redirect('block'); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* put your comment there... |
| 64 |
* |
| 65 |
* @deprecated this is just a redirect to the CJTBlockContoller::getAction(). |
| 66 |
*/ |
| 67 |
protected function getBlockByAction() { |
| 68 |
// Pass to CJTBlockController! |
| 69 |
$this->redirect('block'); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* put your comment there... |
| 74 |
* |
| 75 |
* @deprecated All will be moved to other controllers in the future versions. |
| 76 |
*/ |
| 77 |
public function getInfoViewAction() { |
| 78 |
$model = $this->getModel('blocks'); |
| 79 |
// Set content type as HTML. |
| 80 |
$this->httpContentType = "text/html"; |
| 81 |
// Get block info. |
| 82 |
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 83 |
$Info = $model->getInfo($id); |
| 84 |
// Create info view object. |
| 85 |
$view = CJTController::getView('blocks/info'); |
| 86 |
// Get view info content. |
| 87 |
$view->info = $Info; |
| 88 |
$this->response = $view->getTemplate('default'); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* put your comment there... |
| 93 |
* |
| 94 |
* @deprecated All will be moved to other controllers in the future versions. |
| 95 |
*/ |
| 96 |
public function getRevisionAction() { |
| 97 |
// Initialize |
| 98 |
$model = $this->getModel('blocks'); |
| 99 |
$tblCodeFile = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 100 |
// Inputs. |
| 101 |
$revisionId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 102 |
// Get request parameters. |
| 103 |
$revision['id'] = $revisionId; |
| 104 |
$revision['fields'] = array('id', 'links', 'expressions', 'masterFile'); |
| 105 |
$revision = $model->getBlock($revision['id'], array(), $revision['fields']); |
| 106 |
// Get revisioned code file. |
| 107 |
$revision->code = $tblCodeFile->set('blockId', $revisionId) |
| 108 |
->set('id', $revision->masterFile) |
| 109 |
->load() |
| 110 |
->getData()->code; |
| 111 |
// Discard Pins. |
| 112 |
$revision->pages = false; |
| 113 |
$revision->posts = false; |
| 114 |
$revision->categories = false; |
| 115 |
// Return revision. |
| 116 |
$this->response = $revision; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* put your comment there... |
| 121 |
* |
| 122 |
* @deprecated All will be moved to other controllers in the future versions. |
| 123 |
*/ |
| 124 |
public function getRevisionsAction() { |
| 125 |
// initialize. |
| 126 |
$model = $this->getModel('blocks'); |
| 127 |
// Get request parameters. |
| 128 |
$blockId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 129 |
// Get block revisions. |
| 130 |
$revisions['filter']['masterFile'] = $_GET['activeFileId']; |
| 131 |
$revisions['filter']['parent'] = $blockId; |
| 132 |
$revisions['filter']['type'] = 'revision'; |
| 133 |
// Its mandatory to select fields instead of using just .*. |
| 134 |
// This is because id field must be first to be used as the array key |
| 135 |
$revisions['fields'] = array('id', 'name', 'created', 'lastModified', 'owner'); |
| 136 |
// Query getBlocks without ids filter or backup. |
| 137 |
$revisions = $model->getBlocks(null, $revisions['filter'], $revisions['fields']); |
| 138 |
// Create view. |
| 139 |
$view = $this->getView('blocks/revisions'); |
| 140 |
// Push view vars. |
| 141 |
$view->blockId = $blockId; |
| 142 |
$view->revisions = $revisions; |
| 143 |
// Set output header. |
| 144 |
$this->httpContentType = 'text/html'; |
| 145 |
// Return view content. |
| 146 |
$this->response = $view->getTemplate('default'); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* put your comment there... |
| 151 |
* |
| 152 |
* @deprecated this is just a redirect to the CJTBlockContoller::getAction(). |
| 153 |
*/ |
| 154 |
protected function loadUrlAction() { |
| 155 |
// Pass to CJTBlockController! |
| 156 |
$this->redirect('block'); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* put your comment there... |
| 161 |
* |
| 162 |
*/ |
| 163 |
protected function restoreRevisionAction() { |
| 164 |
// Initialize. |
| 165 |
$mdlBlocks = new CJTBlocksModel(); |
| 166 |
$tblCodeFile = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 167 |
// Get revision ID. |
| 168 |
$rId = (int) $_GET['rid']; |
| 169 |
$bId = (int) $_GET['bid']; |
| 170 |
// Get Revision Block + Revision Code. |
| 171 |
$revisionBlock = $mdlBlocks->getBlock($rId, array(), array('id', 'pinPoint', 'links', 'expressions', 'masterFile')); |
| 172 |
$revisionBlock->code = $tblCodeFile->set('blockId', $rId) |
| 173 |
->set('id', $revisionBlock->masterFile) |
| 174 |
->load() |
| 175 |
->getData()->code; |
| 176 |
// If code === null set it to empoty string '' as null woulod |
| 177 |
// prevent the field from being in the query, cause SQL error. |
| 178 |
if ($revisionBlock->code === null) { |
| 179 |
$revisionBlock->code = ''; |
| 180 |
} |
| 181 |
// Code File Fields. |
| 182 |
$revisionBlock->activeFileId = $revisionBlock->masterFile; |
| 183 |
$revisionBlock->masterFile = null; // This is just for querying CodeFile. DONT UPDATE. |
| 184 |
// Restore Block. |
| 185 |
$revisionBlock->id = $bId; |
| 186 |
$mdlBlocks->update($revisionBlock, true); |
| 187 |
$mdlBlocks->save(); |
| 188 |
// Return TRUE. |
| 189 |
$this->response = true; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Update exists block property value. |
| 194 |
* |
| 195 |
* |
| 196 |
* Call this method using POST method with the following parameters. |
| 197 |
* - id integer Block id. |
| 198 |
* - property string Property Name to change. |
| 199 |
* - value mixed Property value to change. |
| 200 |
* |
| 201 |
* Response body is array with the following elements. |
| 202 |
* - string oldValue Old property value. |
| 203 |
* - string value New value. |
| 204 |
* |
| 205 |
* @deprecated |
| 206 |
* @return void |
| 207 |
*/ |
| 208 |
public function setPropertyAction() { |
| 209 |
// Initialize. |
| 210 |
$response = array(); |
| 211 |
$blocks = $this->getModel('blocks'); |
| 212 |
// Prepare parameters. |
| 213 |
$blockId = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT); |
| 214 |
$property = filter_input(INPUT_POST, 'property', FILTER_SANITIZE_STRING); |
| 215 |
$newValue = filter_input(INPUT_POST, 'value', FILTER_UNSAFE_RAW); |
| 216 |
// Get old value. |
| 217 |
$block = $blocks->getBlock($blockId); |
| 218 |
$oldValue = $block->$property; |
| 219 |
// Update only if there is a change. |
| 220 |
if ($oldValue != $newValue) { |
| 221 |
// Update block property. |
| 222 |
$block->$property = $newValue; |
| 223 |
$blocks->setBlock($block); |
| 224 |
$blocks->save(); |
| 225 |
// Set response object. |
| 226 |
$response['oldValue'] = $oldValue; |
| 227 |
$response['value'] = $newValue; |
| 228 |
} |
| 229 |
$this->response = $response; |
| 230 |
} |
| 231 |
|
| 232 |
} // End class. |