| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 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 |
* This class should replace any other controllers that |
| 14 |
* has methods for interacting with a single Block (e.g block-ajax!) |
| 15 |
* |
| 16 |
* All single Block actions (e.g edit, new and save) should be placed/moved here |
| 17 |
* in the future! |
| 18 |
*/ |
| 19 |
class CJTBlockController extends CJTAjaxController { |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
protected $controllerInfo = array('model' => 'x-block', 'model_file' => 'xblock'); |
| 27 |
|
| 28 |
/** |
| 29 |
* put your comment there... |
| 30 |
* |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
parent::__construct(); |
| 34 |
// Actions! |
| 35 |
$this->registryAction('getBlockBy'); |
| 36 |
$this->registryAction('getAPOP'); |
| 37 |
$this->registryAction('loadUrl'); |
| 38 |
$this->registryAction('getCode'); |
| 39 |
$this->registryAction('downloadCodeFile'); |
| 40 |
$this->registryAction('getAllAssignment'); |
| 41 |
$this->registryAction('getCodeFilesCount'); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* put your comment there... |
| 46 |
* |
| 47 |
*/ |
| 48 |
public function downloadCodeFileAction() { |
| 49 |
// BlockId, currentActiveFile. |
| 50 |
$blockId = sanitize_text_field( $_GET['blockId'] ); |
| 51 |
$fileId = sanitize_text_field( $_GET['fileId'] ); |
| 52 |
$returnAs = sanitize_text_field( $_GET['returnAs'] ); |
| 53 |
// Get current File Code. |
| 54 |
$tblCodeFile = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 55 |
$codeFile = $tblCodeFile->set('id', $fileId) |
| 56 |
->set('blockId', $blockId) |
| 57 |
->load() |
| 58 |
->getData(); |
| 59 |
// Return as downloadable-file or JSON. |
| 60 |
if ($returnAs == 'file') { |
| 61 |
// Get Download File info. |
| 62 |
$extension = $codeFile->type ? cssJSToolbox::$config->templates->types[$codeFile->type]->extension : 'txt'; |
| 63 |
$file = "{$codeFile->name}.{$extension}"; |
| 64 |
// Response Header parameters. |
| 65 |
header('Content-Description: File Transfer'); |
| 66 |
header("Content-Disposition: attachment; filename=\"{$file}\""); //<<< Note the " " surrounding the file name |
| 67 |
header('Content-Transfer-Encoding: binary'); |
| 68 |
header('Connection: Keep-Alive'); |
| 69 |
header('Expires: 0'); |
| 70 |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 71 |
header('Pragma: public'); |
| 72 |
header('Content-Length: ' . strlen($codeFile->code)); |
| 73 |
// AJAX Controller parameters. |
| 74 |
$this->httpContentType = 'application/octet-stream'; |
| 75 |
} |
| 76 |
// Output code. |
| 77 |
$this->response = $codeFile->code; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* put your comment there... |
| 83 |
* |
| 84 |
*/ |
| 85 |
public function getAllAssignmentAction() { |
| 86 |
|
| 87 |
$blockId = isset($_GET['blockId']) ? $_GET['blockId'] : null; |
| 88 |
|
| 89 |
try { |
| 90 |
|
| 91 |
if (!$blockId) { |
| 92 |
|
| 93 |
throw new Exception('Invalid Request parameters'); |
| 94 |
} |
| 95 |
|
| 96 |
$blocks = new CJTBlocksModel(); |
| 97 |
|
| 98 |
$block = $blocks->getBlock($blockId); |
| 99 |
|
| 100 |
$this->response = count(CJTBlocksModel::getAllAssignments($block)); |
| 101 |
} |
| 102 |
catch (Exception $exception) { |
| 103 |
|
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Query single block based on the provided criteria! |
| 111 |
* |
| 112 |
*/ |
| 113 |
public function getBlockByAction() { |
| 114 |
// Initialize. |
| 115 |
$returns = array_flip($_GET['returns']); |
| 116 |
// Set inputs. |
| 117 |
$inputs =& $this->model->inputs; |
| 118 |
$inputs['filter'] = $_GET['filter']; |
| 119 |
// Query Block. |
| 120 |
$this->response = array_intersect_key((array) $this->model->getBlockBy(), $returns); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* put your comment there... |
| 125 |
* |
| 126 |
*/ |
| 127 |
protected function getCodeFilesCountAction() { |
| 128 |
|
| 129 |
|
| 130 |
$blockId = isset($_GET['blockId']) ? $_GET['blockId'] : null; |
| 131 |
|
| 132 |
try { |
| 133 |
|
| 134 |
if (!$blockId) { |
| 135 |
|
| 136 |
throw new Exception('Invalid Request parameters'); |
| 137 |
} |
| 138 |
|
| 139 |
$this->response = count(CJTBlocksModel::getCodeFilesCount($blockId)); |
| 140 |
} |
| 141 |
catch (Exception $exception) { |
| 142 |
|
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get assigment panel objects page. |
| 151 |
* |
| 152 |
*/ |
| 153 |
public function getAPOPAction() { |
| 154 |
// Read inputs. |
| 155 |
$iPerPage = (int) $_GET['iPerPage']; |
| 156 |
$blockId = (int) $_GET['block']; |
| 157 |
$oTypeParams = (array) $_GET['typeParams']; |
| 158 |
$offset = (int) $_GET['index']; |
| 159 |
$assignedOnly = ($_GET['assignedOnly'] == 'false') ? false : true; |
| 160 |
$initialize = ($_GET['initialize'] == 'false') ? false : true; |
| 161 |
// Get the corresponding type object |
| 162 |
// for handling the request. |
| 163 |
$typeName = (string) $oTypeParams['targetType']; |
| 164 |
/** |
| 165 |
* put your comment there... |
| 166 |
* |
| 167 |
* @var CJT_Models_Block_Assignmentpanel_Base |
| 168 |
*/ |
| 169 |
$typeObject = CJT_Models_Block_Assignmentpanel_Base |
| 170 |
::getInstance($assignedOnly, |
| 171 |
$typeName, |
| 172 |
$offset, |
| 173 |
$iPerPage, |
| 174 |
$blockId, |
| 175 |
$oTypeParams); |
| 176 |
// Fetch next page. |
| 177 |
$items = $typeObject->getItems(); |
| 178 |
// Return result |
| 179 |
$this->response['count'] = count($items); |
| 180 |
$this->response['items'] = $items; |
| 181 |
// Return count only when the list is activated for |
| 182 |
// the first time. |
| 183 |
if ($initialize) { |
| 184 |
$this->response['total'] = $typeObject->getTotalCount(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* put your comment there... |
| 190 |
* |
| 191 |
* @deprecated this is just a redirect to the CJTBlockContoller::getAction(). |
| 192 |
*/ |
| 193 |
protected function loadUrlAction() { |
| 194 |
// Read inputs. |
| 195 |
$url = (string) $_GET['url']; |
| 196 |
// Read URL. |
| 197 |
$response = wp_remote_get($url); |
| 198 |
if ($error = $response instanceof WP_Error) { |
| 199 |
// State an error! |
| 200 |
$this->response['errorCode'] = $response->get_error_code(); |
| 201 |
$this->response['message'] = $response->get_error_message($response['code']); |
| 202 |
//break; |
| 203 |
} |
| 204 |
else { |
| 205 |
// Read code content. |
| 206 |
$this->response['content'] = wp_remote_retrieve_body($response); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} // End class. |
| 211 |
|