| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version $ Id; ?FILE_NAME ?DATE ?TIME ?AUTHOR $ |
| 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 |
* |
| 14 |
* DESCRIPTION |
| 15 |
* |
| 16 |
* @author ?? |
| 17 |
* @version ?? |
| 18 |
*/ |
| 19 |
class CJTTemplatesManagerController extends CJTAjaxController { |
| 20 |
|
| 21 |
/** |
| 22 |
* |
| 23 |
*/ |
| 24 |
const SESSIONED_FILTERS = 'cjt_templates__manager'; |
| 25 |
|
| 26 |
/** |
| 27 |
* put your comment there... |
| 28 |
* |
| 29 |
* @var mixed |
| 30 |
*/ |
| 31 |
protected $controllerInfo = array('model' => 'templates-manager', 'view' => 'templates/manager'); |
| 32 |
|
| 33 |
/** |
| 34 |
* |
| 35 |
* Initialize new object. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
// Initialize parent! |
| 41 |
parent::__construct(); |
| 42 |
// Add actions. |
| 43 |
$this->registryAction('display'); |
| 44 |
$this->registryAction('delete'); |
| 45 |
$this->registryAction('changeState'); |
| 46 |
$this->registryAction('linkExternal'); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* put your comment there... |
| 51 |
* |
| 52 |
*/ |
| 53 |
protected function displayAction() { |
| 54 |
// Set default filters. |
| 55 |
if (!isset($_REQUEST['filter_states'])) { |
| 56 |
$_REQUEST['filter_states'] = 'published'; |
| 57 |
} |
| 58 |
// Save all filters! |
| 59 |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
| 60 |
$filters = array_intersect_key($_REQUEST, array_flip(explode(',', $_REQUEST['allFiltersName']))); |
| 61 |
update_user_option(get_current_user_id(), self::SESSIONED_FILTERS, $filters); |
| 62 |
} |
| 63 |
else { |
| 64 |
// Load sessioned filter from database options table! |
| 65 |
$filters = (array) get_user_option(self::SESSIONED_FILTERS, get_current_user_id()); |
| 66 |
$_REQUEST = array_merge($_REQUEST, $filters); |
| 67 |
} |
| 68 |
// Push inputs into the model! |
| 69 |
$this->model->inputs = $_REQUEST; |
| 70 |
// Display view. |
| 71 |
parent::displayAction(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* put your comment there... |
| 76 |
* |
| 77 |
*/ |
| 78 |
protected function deleteAction() { |
| 79 |
$this->model->inputs['ids'] = $_GET['ids']; |
| 80 |
// Response with changed ids. |
| 81 |
$this->response['changes'] = $this->model->delete(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* put your comment there... |
| 86 |
* |
| 87 |
*/ |
| 88 |
protected function changeStateAction() { |
| 89 |
$this->model->inputs['ids'] = $_GET['ids']; |
| 90 |
$this->model->inputs['state'] = $_GET['params']; |
| 91 |
$this->model->changeState(); |
| 92 |
// Response with changed ids. |
| 93 |
$this->response['changes'] = $this->model->inputs['ids']; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Link external Resources (CSS, HTML, JS and PHP) |
| 98 |
* |
| 99 |
* The action is to create external link as CJT template |
| 100 |
* and link it to the target block. |
| 101 |
*/ |
| 102 |
protected function linkExternalAction() { |
| 103 |
// Import dependencies. |
| 104 |
cssJSToolbox::import('includes:templates:templates.class.php'); |
| 105 |
// Initialize response as successed until error occured! |
| 106 |
$this->response = array('code' => 0, 'message' => ''); |
| 107 |
// List of all the external templates records to create! |
| 108 |
$externalTemplates = array(); |
| 109 |
// Read inputs. |
| 110 |
$externals = explode(',', $_REQUEST['externals']); |
| 111 |
$blockId = (int) $_REQUEST['blockId']; |
| 112 |
// Add as templates. |
| 113 |
foreach ($externals as $externalResourceURI) { |
| 114 |
// Use URI base name as Template Name and the extension as Template Type. |
| 115 |
$externalPathInfo = pathinfo($externalResourceURI); |
| 116 |
// Template Item. |
| 117 |
$item = array(); |
| 118 |
$item['template']['name'] = $externalPathInfo['basename']; |
| 119 |
$item['template']['type'] = CJTTemplates::getExtensionType($externalPathInfo['extension']); |
| 120 |
$item['template']['state'] = 'published'; |
| 121 |
// Get external URI code! |
| 122 |
$externalResponse = wp_remote_get($externalResourceURI); |
| 123 |
if ($error = $externalResponse instanceof WP_Error) { |
| 124 |
// State an error! |
| 125 |
$this->response['code'] = $externalResponse->get_error_code(); |
| 126 |
$this->response['message'] = $externalResponse->get_error_message($this->response['code']); |
| 127 |
break; |
| 128 |
} |
| 129 |
else { |
| 130 |
// Read code content. |
| 131 |
$item['revision']['code'] = wp_remote_retrieve_body($externalResponse); |
| 132 |
// Add to the save list! |
| 133 |
$externalTemplates[] = $item; |
| 134 |
} |
| 135 |
} |
| 136 |
// Save all templates if no error occured |
| 137 |
// Single error will halt all the linked externals! They all added as a single transaction. |
| 138 |
if (!$this->response['code']) { |
| 139 |
// Instantiate Template Models. |
| 140 |
$modelLookup = CJTModel::getInstance('templates-lookup'); |
| 141 |
$modelTemplate = CJTModel::getInstance('template'); |
| 142 |
$modelBlockTemplates = CJTModel::getInstance('block-templates'); |
| 143 |
// Add all templates. |
| 144 |
foreach ($externalTemplates as $item) { |
| 145 |
// Check existance. |
| 146 |
$modelTemplate->inputs['filter']['field'] = 'name'; |
| 147 |
$modelTemplate->inputs['filter']['value'] = $item['template']['name']; |
| 148 |
if (!($existsItem = $modelTemplate->getTemplateBy()) || !property_exists($existsItem, 'id') || !$existsItem->id) { |
| 149 |
// Create template. |
| 150 |
$modelTemplate->inputs['item'] = $item; |
| 151 |
$item = (array) $modelTemplate->save(); |
| 152 |
} |
| 153 |
else { |
| 154 |
// The returned item has 'id' field not 'templateId'!! |
| 155 |
$item = array('templateId' => $existsItem->id); |
| 156 |
} |
| 157 |
// Link only if not already linked! |
| 158 |
if (!$modelBlockTemplates->isLinked($blockId, $item['templateId'])) { |
| 159 |
// Link template to the block. |
| 160 |
$modelLookup->inputs['templateId'] = $item['templateId']; |
| 161 |
$modelLookup->inputs['blockId'] = $blockId; |
| 162 |
$modelLookup->link(); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
} // End class. |