| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// No 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 |
* |
| 14 |
*/ |
| 15 |
class CJTTinymceBlocksController extends CJTAjaxController { |
| 16 |
|
| 17 |
/** |
| 18 |
* put your comment there... |
| 19 |
* |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
protected $controllerInfo = array('model' => 'tinymce-blocks'); |
| 23 |
|
| 24 |
/** |
| 25 |
* put your comment there... |
| 26 |
* |
| 27 |
*/ |
| 28 |
public function __construct($hasView = null, $request = null) { |
| 29 |
// Initialize parent! |
| 30 |
parent::__construct($hasView, $request); |
| 31 |
// Register actions! |
| 32 |
$this->registryAction('getBlocksList'); |
| 33 |
$this->registryAction('getShortcode'); |
| 34 |
$this->registryAction('getBlockParametersForm'); |
| 35 |
// @TODO: $this->defaultCapability is risky if there is any admin actions added later, please remove! |
| 36 |
$this->defaultCapability = array('edit_posts', 'edit_pages'); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
*/ |
| 43 |
protected function getBlockParametersFormAction() { |
| 44 |
// Get block id. |
| 45 |
$blockId = (int) $_REQUEST['blockId']; |
| 46 |
// Get block form! |
| 47 |
$form = CJTxTable::getInstance('form') |
| 48 |
->setTableKey(array('blockId')) |
| 49 |
->setData(array('blockId' => $blockId)) |
| 50 |
->load(); |
| 51 |
// Set HTTP header |
| 52 |
$this->httpContentType = 'text/html'; |
| 53 |
// Display form view. |
| 54 |
if ($form->get('blockId')) { |
| 55 |
// Load parameters form. |
| 56 |
$groups = new CJT_Models_Block_Parameters_Form_Groups($blockId); |
| 57 |
$params = new CJT_Models_Block_Parameters_Form_Parameters($blockId); |
| 58 |
$blockParams = new CJT_Framework_View_Block_Parameter_Parameters($params); |
| 59 |
// Return view content! |
| 60 |
$paramsView = CJTView::getInstance('tinymce/params', array( |
| 61 |
'groups' => $groups, |
| 62 |
'params' => $blockParams->getParams(), |
| 63 |
'form' => $form->getData(), |
| 64 |
'blockId' => $blockId |
| 65 |
) |
| 66 |
); |
| 67 |
// Return paramters form content! |
| 68 |
$this->response = $paramsView->getTemplate('default'); |
| 69 |
} |
| 70 |
else { // Error loading form! |
| 71 |
$this->response = cssJSToolbox::getText('The requested Block doesnt has parameters form!'); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* put your comment there... |
| 77 |
* |
| 78 |
*/ |
| 79 |
protected function getBlocksListAction() { |
| 80 |
// Read all blocks! |
| 81 |
$blocks = $this->model->getItems(); |
| 82 |
// retreive owener name! |
| 83 |
foreach ($blocks as $id => $block) { |
| 84 |
$user = get_userdata($block->owner); |
| 85 |
$block->owner = !$user ? 'N/A' : $user->display_name; |
| 86 |
$this->response['list'][$id] = $block; |
| 87 |
} |
| 88 |
$this->response['count'] = count($this->response['list']); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* put your comment there... |
| 93 |
* |
| 94 |
*/ |
| 95 |
protected function getShortcodeAction() { |
| 96 |
// Get block id. |
| 97 |
$blockId = $_REQUEST['blockId']; |
| 98 |
// Load block |
| 99 |
$block = CJTxTable::getInstance('block') |
| 100 |
->setTableKey(array('id')) |
| 101 |
->setData(array('id' => $blockId)) |
| 102 |
->load() |
| 103 |
->getData(); |
| 104 |
$blockForm = (array) CJTxTable::getInstance('form') |
| 105 |
->setTableKey(array('blockId')) |
| 106 |
->setData(array('blockId' => $blockId)) |
| 107 |
->load() |
| 108 |
->getData(); |
| 109 |
// Load block parameters. |
| 110 |
$parameters = new CJT_Models_Block_Parameters_Parameters($blockId); |
| 111 |
$blockParams = new CJT_Framework_Developer_Interface_Block_Shortcode_Parameters_Parameters($parameters); |
| 112 |
// The the block has FORM associated |
| 113 |
if (isset($blockForm['blockId'])) { |
| 114 |
// If the data is submitted then load the parameters object with the post data! |
| 115 |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
| 116 |
// Get RAW data from the data submitted (avoid magic_quotes, Why called MAGIC!!!!)! |
| 117 |
$rawPostData = filter_input(INPUT_POST, 'form-data', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY); |
| 118 |
// Fill the form by the posted data |
| 119 |
$blockParams->setValue($rawPostData); |
| 120 |
// Validate the submitted data against their parameters. |
| 121 |
if ($blockParams->validate()) { |
| 122 |
// Generate and Return Shortcode string! |
| 123 |
$this->response['state'] = 'shortcode-notation'; |
| 124 |
$this->response['content'] = $blockParams->shortcode($block->name); |
| 125 |
} |
| 126 |
else { |
| 127 |
$this->response['state'] = 'invalid'; |
| 128 |
$this->response['content'] = $blockParams->getMessages(); |
| 129 |
} |
| 130 |
} |
| 131 |
// Redirect to the parameters form view! |
| 132 |
else { |
| 133 |
$this->response['state'] = 'show-form'; |
| 134 |
} |
| 135 |
} |
| 136 |
else { |
| 137 |
// If block doesn't has a parameters defined return |
| 138 |
// the Shortcode code string from the parameters loader. |
| 139 |
$this->response['state'] = 'shortcode-notation'; |
| 140 |
$this->response['content'] = $blockParams->shortcode($block->name); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
} // End class. |
| 145 |
|