tables
1 year ago
assets.php
1 year ago
baseObject.php
1 year ago
cache.php
1 year ago
controller.php
1 year ago
date.php
1 year ago
db.php
1 year ago
dispatcher.php
1 year ago
errors.php
1 year ago
field.php
1 year ago
fieldAdapter.php
1 year ago
frame.php
1 year ago
helper.php
1 year ago
html.php
1 year ago
installer.php
1 year ago
installerDbUpdater.php
1 year ago
modInstaller.php
1 year ago
model.php
1 year ago
module.php
1 year ago
req.php
1 year ago
response.php
1 year ago
table.php
1 year ago
uri.php
1 year ago
user.php
1 year ago
utils.php
1 year ago
validator.php
1 year ago
view.php
1 year ago
controller.php
214 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | abstract class WaicController { |
| 6 | protected $_models = array(); |
| 7 | protected $_views = array(); |
| 8 | protected $_task = ''; |
| 9 | protected $_defaultView = ''; |
| 10 | protected $_code = ''; |
| 11 | public function __construct( $code ) { |
| 12 | $this->setCode($code); |
| 13 | $this->_defaultView = $this->getCode(); |
| 14 | } |
| 15 | public function init() { |
| 16 | /*load model and other preload data goes here*/ |
| 17 | } |
| 18 | protected function _onBeforeInit() { |
| 19 | |
| 20 | } |
| 21 | protected function _onAfterInit() { |
| 22 | |
| 23 | } |
| 24 | public function setCode( $code ) { |
| 25 | $this->_code = $code; |
| 26 | } |
| 27 | public function getCode() { |
| 28 | return $this->_code; |
| 29 | } |
| 30 | public function exec( $task = '' ) { |
| 31 | if (method_exists($this, $task)) { |
| 32 | $this->_task = $task; //For multicontrollers module version - who know, maybe that's will be?)) |
| 33 | return $this->$task(); |
| 34 | } |
| 35 | return null; |
| 36 | } |
| 37 | public function getView( $name = '' ) { |
| 38 | if (empty($name)) { |
| 39 | $name = $this->getCode(); |
| 40 | } |
| 41 | if (!isset($this->_views[$name])) { |
| 42 | $this->_views[$name] = $this->_createView($name); |
| 43 | } |
| 44 | return $this->_views[$name]; |
| 45 | } |
| 46 | public function getModel( $name = '' ) { |
| 47 | if (!$name) { |
| 48 | $name = $this->_code; |
| 49 | } |
| 50 | if (!isset($this->_models[$name])) { |
| 51 | $this->_models[$name] = $this->_createModel($name); |
| 52 | } |
| 53 | return $this->_models[$name]; |
| 54 | } |
| 55 | protected function _createModel( $name = '' ) { |
| 56 | if (empty($name)) { |
| 57 | $name = $this->getCode(); |
| 58 | } |
| 59 | $parentModule = WaicFrame::_()->getModule( $this->getCode() ); |
| 60 | $className = ''; |
| 61 | if (waicImport($parentModule->getModDir() . 'models' . WAIC_DS . $name . '.php')) { |
| 62 | $className = waicToeGetClassName($name . 'Model'); |
| 63 | } |
| 64 | |
| 65 | if ($className) { |
| 66 | $model = new $className(); |
| 67 | $model->setCode( $this->getCode() ); |
| 68 | return $model; |
| 69 | } |
| 70 | return null; |
| 71 | } |
| 72 | protected function _createView( $name = '' ) { |
| 73 | if (empty($name)) { |
| 74 | $name = $this->getCode(); |
| 75 | } |
| 76 | $parentModule = WaicFrame::_()->getModule( $this->getCode() ); |
| 77 | $className = ''; |
| 78 | |
| 79 | if (waicImport($parentModule->getModDir() . 'views' . WAIC_DS . $name . '.php')) { |
| 80 | $className = waicToeGetClassName($name . 'View'); |
| 81 | } |
| 82 | |
| 83 | if ($className) { |
| 84 | $view = new $className(); |
| 85 | $view->setCode( $this->getCode() ); |
| 86 | return $view; |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | public function display( $viewName = '' ) { |
| 91 | $view = $this->getView($viewName); |
| 92 | if (null === $view) { |
| 93 | $view = $this->getView(); //Get default view |
| 94 | } |
| 95 | if ($view) { |
| 96 | $view->display(); |
| 97 | } |
| 98 | } |
| 99 | public function __call( $name, $arguments ) { |
| 100 | $model = $this->getModel(); |
| 101 | if (method_exists($model, $name)) { |
| 102 | return $model->$name($arguments[0]); |
| 103 | } else { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | /** |
| 108 | * Retrive permissions for controller methods if exist. |
| 109 | * If need - should be redefined in each controller where it required. |
| 110 | * |
| 111 | * @return array with permissions |
| 112 | * Can be used on of sub-array - WAIC_METHODS or WAIC_USERLEVELS |
| 113 | */ |
| 114 | public function getPermissions() { |
| 115 | return array(); |
| 116 | } |
| 117 | /** |
| 118 | * Methods that require nonce to be generated |
| 119 | * If need - should be redefined in each controller where it required. |
| 120 | * |
| 121 | * @return array |
| 122 | */ |
| 123 | public function getNoncedMethods() { |
| 124 | return array(); |
| 125 | } |
| 126 | public function getModule() { |
| 127 | return WaicFrame::_()->getModule( $this->getCode() ); |
| 128 | } |
| 129 | protected function _prepareTextLikeSearch( $val ) { |
| 130 | return ''; // Should be re-defined for each type |
| 131 | } |
| 132 | protected function _prepareModelBeforeListSelect( $model ) { |
| 133 | return $model->setSelectFields('*'); |
| 134 | } |
| 135 | /** |
| 136 | * Common method for list table data |
| 137 | */ |
| 138 | public function getListForTbl() { |
| 139 | $res = new WaicResponse(); |
| 140 | $res->ignoreShellData(); |
| 141 | $model = $this->getModel(); |
| 142 | |
| 143 | $params = WaicReq::get('post'); |
| 144 | |
| 145 | $length = WaicUtils::getArrayValue($params, 'length', 10, 1); |
| 146 | $start = WaicUtils::getArrayValue($params, 'start', 0, 1); |
| 147 | $search = WaicUtils::getArrayValue(WaicUtils::getArrayValue($params, 'search', array(), 2), 'value'); |
| 148 | |
| 149 | if (!empty($search)) { |
| 150 | $model->addWhere(array('additionalCondition' => "title like '%" . $search . "%'")); |
| 151 | } |
| 152 | $order = WaicUtils::getArrayValue($params, 'order', array(), 2); |
| 153 | $orderBy = 'id'; |
| 154 | $sortOrder = 'DESC'; |
| 155 | if (isset($order[0])) { |
| 156 | $orderBy = WaicUtils::getArrayValue($order[0], 'column', $orderBy, 1); |
| 157 | $sortOrder = WaicUtils::getArrayValue($order[0], 'dir', $sortOrder); |
| 158 | } |
| 159 | |
| 160 | // Get total pages count for current request |
| 161 | $totalCount = $model->getCount(array('clear' => array('selectFields'))); |
| 162 | if ($length > 0) { |
| 163 | if ($start >= $totalCount) { |
| 164 | $start = 0; |
| 165 | } |
| 166 | $model->setLimit($start . ', ' . $length); |
| 167 | } |
| 168 | |
| 169 | $model->setOrderBy($orderBy)->setSortOrder($sortOrder); |
| 170 | $data = $this->_prepareModelBeforeListSelect($model)->getFromTbl(); |
| 171 | |
| 172 | $data = empty($data) ? array() : $this->_prepareListForTbl($data); |
| 173 | $res->data = $data; |
| 174 | |
| 175 | $res->recordsFiltered = $totalCount; |
| 176 | $res->recordsTotal = $totalCount; |
| 177 | $res->draw = WaicUtils::getArrayValue($params, 'draw', 0, 1); |
| 178 | |
| 179 | $res = WaicDispatcher::applyFilters($this->getCode() . '_getListForTblResults', $res); |
| 180 | $res->ajaxExec(); |
| 181 | |
| 182 | } |
| 183 | public function removeGroup() { |
| 184 | $res = new WaicResponse(); |
| 185 | if ($this->getModel()->removeGroup(WaicReq::getVar('ids', 'post'))) { |
| 186 | $res->addMessage(esc_html__('Done', 'ai-copilot-content-generator')); |
| 187 | } else { |
| 188 | $res->pushError($this->getModel()->getErrors()); |
| 189 | } |
| 190 | $res->ajaxExec(); |
| 191 | } |
| 192 | public function clear() { |
| 193 | $res = new WaicResponse(); |
| 194 | if ($this->getModel()->clear()) { |
| 195 | $res->addMessage(esc_html__('Done', 'ai-copilot-content-generator')); |
| 196 | } else { |
| 197 | $res->pushError($this->getModel()->getErrors()); |
| 198 | } |
| 199 | $res->ajaxExec(); |
| 200 | } |
| 201 | protected function _prepareListForTbl( $data ) { |
| 202 | return $data; |
| 203 | } |
| 204 | protected function _prepareSearchField( $searchField ) { |
| 205 | return $searchField; |
| 206 | } |
| 207 | protected function _prepareSearchString( $searchString ) { |
| 208 | return $searchString; |
| 209 | } |
| 210 | protected function _prepareSortOrder( $sortOrder ) { |
| 211 | return $sortOrder; |
| 212 | } |
| 213 | } |
| 214 |