| 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 CJTCodeFilesController extends CJTAjaxController { |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
protected $controllerInfo = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* put your comment there... |
| 30 |
* |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
parent::__construct(); |
| 34 |
// Actions! |
| 35 |
$this->registryAction('getList'); |
| 36 |
$this->registryAction('delete'); |
| 37 |
$this->registryAction('save'); |
| 38 |
$this->registryAction('switch'); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* put your comment there... |
| 43 |
* |
| 44 |
*/ |
| 45 |
public function deleteAction() { |
| 46 |
// Block id. |
| 47 |
$blockId = (int) $_GET['blockId']; |
| 48 |
$ids = (array) $_GET['ids']; |
| 49 |
// Fetch code Blocks list. |
| 50 |
$tblCodeFiles = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 51 |
// Delete all |
| 52 |
foreach ($ids as $id) { |
| 53 |
$tblCodeFiles->set('blockId', $blockId) |
| 54 |
->set('id', $id) |
| 55 |
->delete(); |
| 56 |
} |
| 57 |
// Response with list and codeFileIds. |
| 58 |
$this->response['blockId'] = $blockId; |
| 59 |
$this->response['ids'] = $ids; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* put your comment there... |
| 64 |
* |
| 65 |
*/ |
| 66 |
public function getListAction() { |
| 67 |
// Block id. |
| 68 |
$blockId = (int) $_GET['blockId']; |
| 69 |
//; Fetch code Blocks list. |
| 70 |
$tblCodeFiles = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 71 |
$result = $tblCodeFiles->set('blockId', $blockId)->fetchAll(); |
| 72 |
foreach ($result as $codeFile) { |
| 73 |
$codeFilesList[] = array( |
| 74 |
'name' => $codeFile['name'], |
| 75 |
'description' => $codeFile['description'], |
| 76 |
'type' => $codeFile['type'], |
| 77 |
'tag' => $codeFile['tag'], |
| 78 |
'id' => $codeFile['id'], |
| 79 |
'order' => $codeFile['order'], |
| 80 |
); |
| 81 |
} |
| 82 |
// Response with list and blockId. |
| 83 |
$this->response['blockId'] = $blockId; |
| 84 |
$this->response['list'] = $codeFilesList; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* put your comment there... |
| 89 |
* |
| 90 |
*/ |
| 91 |
public function saveAction() { |
| 92 |
// Block id. |
| 93 |
$blockId = (int) $_POST['blockId']; |
| 94 |
$codeFile = filter_input(INPUT_POST, 'codeFile', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY); |
| 95 |
// Add blockId to codeFile record. |
| 96 |
$codeFile['blockId'] = (int) $blockId; |
| 97 |
// Get Code Files Table. |
| 98 |
$tblCodeFiles = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 99 |
// Fill |
| 100 |
$tblCodeFiles->setData($codeFile) |
| 101 |
// Update or insert. |
| 102 |
->save(false, true); |
| 103 |
// Return New CodeFile Data. |
| 104 |
$this->response = (array) $tblCodeFiles->getData(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* put your comment there... |
| 109 |
* |
| 110 |
*/ |
| 111 |
public function switchAction() { |
| 112 |
// Block id. |
| 113 |
$blockId = (int) $_GET['blockId']; |
| 114 |
$fileId =(int) $_GET['codeFileId']; |
| 115 |
// Read Code File Record. |
| 116 |
$tblCodeFiles = new CJTBlockFilesTable(cssJSToolbox::getInstance()->getDBDriver()); |
| 117 |
$codeFile = $tblCodeFiles->set('blockId', $blockId) |
| 118 |
->set('id', $fileId) |
| 119 |
->load() |
| 120 |
->getData(); |
| 121 |
// Set author Active Code File Id. |
| 122 |
// TODO: Setting Active File ID should be in the MODEL! |
| 123 |
update_user_meta(get_current_user_id(), "cjt_block_active_file_{$blockId}", $fileId); |
| 124 |
// Return code file. |
| 125 |
$this->response = $codeFile; |
| 126 |
} |
| 127 |
|
| 128 |
} // End class. |
| 129 |
|