controller.php
73 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | class WaicFormsController extends WaicController { |
| 6 | |
| 7 | protected $_code = 'forms'; |
| 8 | |
| 9 | public function getNoncedMethods() { |
| 10 | return array('saveForm', 'sendForm', 'getHistoryPage'); |
| 11 | } |
| 12 | public function allowNoprivAjax( $action ) { |
| 13 | return in_array(strtolower($action), array('sendform'), true); |
| 14 | } |
| 15 | public function saveForm() { |
| 16 | $res = new WaicResponse(); |
| 17 | $params = WaicReq::getVar('params', 'post'); |
| 18 | $taskId = WaicReq::getVar('task_id', 'post'); |
| 19 | $error = ''; |
| 20 | $error = ''; |
| 21 | if (!$this->getModel()->controlTaskParameters($params, $error, $taskId)) { |
| 22 | $res->pushError($error); |
| 23 | } else { |
| 24 | $workspace = WaicFrame::_()->getModule('workspace'); |
| 25 | $params = $this->getModel()->convertTaskParameters($params); |
| 26 | $id = $workspace->getModel('tasks')->saveTask($this->_code, WaicReq::getVar('task_id', 'post'), $params); |
| 27 | $this->getModel()->createTable(); |
| 28 | if (empty($id)) { |
| 29 | $res->pushError(WaicFrame::_()->getErrors()); |
| 30 | } else if (empty($taskId)) { |
| 31 | $res->addData('taskUrl', $workspace->getTaskUrl($id, $this->_code)); |
| 32 | } else { |
| 33 | $res->addMessage(esc_html__('Done', 'ai-copilot-content-generator')); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return $res->ajaxExec(); |
| 38 | } |
| 39 | |
| 40 | public function getHistoryPage() { |
| 41 | $res = new WaicResponse(); |
| 42 | $res->ignoreShellData(); |
| 43 | |
| 44 | $params = WaicReq::get('post'); |
| 45 | $result = $this->getModel()->getHistory($params); |
| 46 | |
| 47 | if ($result) { |
| 48 | $res->data = $result['data']; |
| 49 | |
| 50 | $res->recordsFiltered = $result['total']; |
| 51 | $res->recordsTotal = $result['total']; |
| 52 | $res->draw = WaicUtils::getArrayValue($params, 'draw', 0, 1); |
| 53 | |
| 54 | } else { |
| 55 | $res->pushError(WaicFrame::_()->getErrors()); |
| 56 | } |
| 57 | $res->ajaxExec(); |
| 58 | } |
| 59 | |
| 60 | public function sendForm() { |
| 61 | $res = new WaicResponse(); |
| 62 | $taskId = WaicReq::getVar('task_id', 'post'); |
| 63 | $submit = WaicReq::getVar('submit', 'post'); |
| 64 | $fields = WaicReq::getVar('fields', 'post'); |
| 65 | |
| 66 | $result = $this->getModel()->sendForm($taskId, $submit, $fields); |
| 67 | $result['submit'] = $submit; |
| 68 | $res->addData('result', $result); |
| 69 | |
| 70 | return $res->ajaxExec(); |
| 71 | } |
| 72 | } |
| 73 |