* @since Version 2.0 */ class cmsMessagesController extends cCore { public $dir; public $name; public $module; /** * Class constructor */ public function __construct($uriData, $dir, $name) { $this->dir = $dir; $this->name = $name; parent :: __construct($uriData); $this->module = new cmsMessagesModule(); $this->oView->setTemplateDir($this->dir . "/tmpl/"); if(!$this->oView->cacheUsed()){ $this->oView->setJs(getCurrentFolder($this->dir) . '/js/' . $this->name . '.js'); $this->oView->setCss(getCurrentFolder($this->dir) . '/css/' . $this->name . '.css'); } // Set JS module init title $this->oView->setAsJSON(cCore::JS_MOD, ucfirst($name)); } /** * Function to show main form in the module * * @category cmsMessages Controller * @author Mareks Keišs * @returns bool true */ public function run($initEditMode = false){ $this->oView->TABLE = true; $this->oView->setAsJSON('initEdit', $initEditMode); $this->oView->setAsJSON('baseUrl', $this->gL()); $this->oView->setAsJSON('gM', $this->getTranslations()); $this->oView->setAsJSON('vcfg', $this->getValCfg()); $this->oView->display(); } /** * Function to create cms table & return for direct & ajax input * * @author Mareks Keišs * @category cmsMessages Controller * @param int $page - page to display * @param string $filter_json - (optional) filter json string * @param bool $headers - flag whether to send header data or not * @return array of data * @version 1.1 */ public function createCmsTable($page = 1, $filter_json = '', $headers = false) { $pages = 0; $filter = array(); if($filter_json) $filter = cJSON::decode($filter_json, true); $this->oView->MODULE_TITLE = $this->gM("Pub Messages"); if(!!$filter && is_array($filter)) $this->oView->DEFAULT_SEARCH_VALUES = prepForDisplayHtml($filter); $this->oView->LIST_HEADER = reOrderTableFields($this->getTableFields(), $this->oUser->oCfg->getTableProperty($this->name)); $this->oView->LIST_DATA = $this->getModuleData($page, $filter, $pages); $this->oView->LIST_ACTIONS = $this->getTableActions(); $data = array( 'data' => $this->oView->genHelperView('cmsc/table/data-table'), 'pcount' => $pages, 'rlimit' => $this->cmsTableRecordsPerPage ); // Send data to client if($headers) $data['header'] = $this->oView->genHelperView('cmsc/table/data-table-search'); return $data; } /** * Function to get module data * * @author Mareks Keišs * @category cmsMessages Controller * @param int $page * @param array $filter * @param int $pages * @return array of data * @version 1.1 */ public function getModuleData($page = 1, $filter = array(), &$pages) { $where = "AND 1"; $sort = "id DESC"; $offset = (($page?$page:1)-1) * $this->cmsTableRecordsPerPage; $tFields = $this->getTableFields(); // Create sort by if(getP('sortBy')) $sort = $this->module->db->genOrderByQuery(getP('sortBy'), getP('sortWay'), $tFields); // Create where clause if(count($filter)) $where = $this->module->db->genSearchFieldQuery($filter, $tFields); $subWhere = ""; $data = $this->module->getModuleRecords($where . $subWhere, $sort, $offset, $this->cmsTableRecordsPerPage); if (count($data) > 0) { foreach ($data as &$v) { if(date('d-m-Y', $v['created']) == date('d-m-Y')){ $v['created'] = date('H:i', $v['created']); }else{ $v['created'] = date($this->CMSDateFormat . ' H:i', $v['created']); } } unset($v); } $this->oView->ROWS = $rows = $this->module->db->getSelectedRecordsCount(); $pages = floor($rows / $this->cmsTableRecordsPerPage) + ($rows%$this->cmsTableRecordsPerPage?1:0); return $data; } /** * Function to delete records marked in table * * @author Mareks Keišs * @category cmsMessages Controller * @param string $rows - passed user ids in JSON string * @return bool|array of data */ public function delete($rows) { if($rows = cJSON::decode($rows, true)){ if($this->module->deleteRecords($this->module->db->prepData($rows))) return true; cJSON::sendError(); } return false; } /** * Function to get table record data for edit mode * * @author Aivars Žukovskis * @category cmsMessages Controller * @param int $id - record id * @return array of data */ public function editCmsTableRecord($id) { if($id){ $vData = $this->module->getRecordDataById($id); if(date('d-m-Y', $vData['created']) == date('d-m-Y')){ $vData['created'] = date('H:i', $vData['created']); }else{ $vData['created'] = date($this->CMSDateFormat, $vData['created']); } if(date('d-m-Y', $vData['modified']) == date('d-m-Y')){ $vData['modified'] = date('H:i', $vData['modified']); }else{ $vData['modified'] = date($this->CMSDateFormat, $vData['modified']); } $this->oView->DATA = $vData; } return array( 'show' => array('buttons_top' => false, 'buttons_bottom' => true), 'btn' => $this->getFormButtons(), 'html' => $this->oView->genTemplate('edit'), ); } /** * Function to save Record data * * @author Aivars Žukovskis * @category cmsMessages Controller * @param int $recordId - record id * @param string $fields - param JSON string * @return array of data */ public function saveRecordData($recordId, $fields) { if($fields) $fields = $this->getSubmittedDataAsArray($fields); // Start save process if($fields){ // Validate fields $valid = new cValidator($this); $errors = $valid->checkArray($fields, $this->getValCfg()); if($errors !== true) cJSON::sendError(array('data' => $errors)); // Create base data array $update = array( 'first_name' => (!empty($fields['pbase']['first_name']) ? $fields['pbase']['first_name'] : ''), 'last_name' => (!empty($fields['pbase']['last_name']) ? $fields['pbase']['last_name'] : ''), 'email' => (!empty($fields['pbase']['email']) ? $fields['pbase']['email'] : ''), 'phone' => (!empty($fields['pbase']['phone']) ? $fields['pbase']['phone'] : ''), 'subject' => (!empty($fields['pbase']['subject']) ? $fields['pbase']['subject'] : ''), 'message' => (!empty($fields['pbase']['message']) ? $fields['pbase']['message'] : ''), 'modified' => time() ); if(!$recordId){ $update['created'] = time(); $update['ip_from'] = $_SERVER['REMOTE_ADDR']; $update['user_agent'] = $_SERVER['HTTP_USER_AGENT']; } // Update / Insert record data $oldId = $recordId; if(!($recordId = $this->module->saveRecordDataById($recordId, $update))) cJSON::sendError(); cJSON::sendOk($oldId?"":array('newid' => $recordId)); } } /** * Function to create table headers + search * * @author Mareks Keišs * @category cmsMessages Controller * @return array of data * @version 1.1 */ public function getTableFields(){ if(!$this->oUser->chkPermission('CMS_MESSAGES_SHOW_MANAGEMENT', false, false)) return false; $tFields = array( "checkbox" => array( 'sort' => false, 'title' => '', 'class' => 'w4 center rchk' ), "id" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("ID"), 'class' => 'w8 center sortDesc sort', 'db_field' => 'mc.id', 'type' => 'input', ), "sender" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("Sender"), 'class' => 'w18', 'db_field' => 'sender', 'type' => 'input', ), "email" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("Email"), 'class' => 'w18', 'db_field' => 'mc.email', 'type' => 'input', ), "created" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("Created"), 'class' => 'w12', 'db_field' => 'mc.created', 'type' => 'input', ), "phone" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("Phone"), 'class' => 'w12', 'db_field' => 'mc.phone', 'type' => 'input', ), "subject" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("subject"), 'class' => 'w12', 'db_field' => 'mc.subject', 'type' => 'input', ), "message" => array( 'sort' => true, 'search' => true, 'title' => $this->gM("Message"), 'class' => 'w32', 'db_field' => 'message', 'type' => 'input', ), "buttons" => array( 'sort' => false, 'class' => 'w20', 'title' => $this->gM("Actions"), 'btnset' => array( 'ledit' => array( 'edit' => true, 'title' => $this->gM("Edit"), ), 'ldelete' => array( 'title' => $this->gM("Delete"), ) ) ) ); if(!$this->chkP('SYS_SHOW_TABLE_IDS')) unset($tFields['id']); return $tFields; } /** * Function to get js translations for view * * @author Mareks Keišs * @category cmsMessages Controller * @return array of data */ public function getTranslations(){ return array( 'yes' => $this->gM('Yes'), 'no' => $this->gM('No'), 'delTitle' => $this->gM('Delete entry?'), 'delMessage' => $this->gM('Selected entry will be permanently deleted and cannot be recovered. Confirm delete?'), 'buttonYes' => $this->gM('Delete selected entry'), 'buttonCancel' => $this->gM('Cancel operation'), ); } /** * Function to get table actions * * @author Mareks Keišs * @category cmsMessages Controller * @return array of data */ public function getTableActions(){ $actions = array( 'inew' => array( 'lbl' => $this->gM("Add new"), 'icon' => 'nico-add' ), 'sepline', 'idelete' => array( 'lbl' => $this->gM("Delete selected"), 'icon' => 'nico-delete' ), ); // Reformat drop down reformatDropDown($actions, false); return $actions; } /** * Function to get js validator cfg * * @author Aivars Žukovskis * @category cmsMessages Controller * @return array of data */ public function getValCfg(){ return array( 'first_name' => array( cValidator::P_TYPE => cValidator::TY_STR, cValidator::P_COMP => 1, cValidator::P_TEST => array(cValidator::CHK_STR), ), 'last_name' => array( cValidator::P_TYPE => cValidator::TY_STR, cValidator::P_COMP => 0, cValidator::P_TEST => array(cValidator::CHK_STR), ), 'email' => array( cValidator::P_TYPE => cValidator::TY_STR, cValidator::P_COMP => 1, cValidator::P_TEST => array(cValidator::CHK_EMAIL), ), 'phone' => array( cValidator::P_TYPE => cValidator::TY_STR, cValidator::P_COMP => 0, cValidator::P_TEST => array(cValidator::CHK_PHONE), ), 'subject' => array( cValidator::P_TYPE => cValidator::TY_STR, cValidator::P_COMP => 0, cValidator::P_TEST => array(cValidator::CHK_STR), ), 'message' => array( cValidator::P_TYPE => cValidator::TY_STR, cValidator::P_COMP => 1, cValidator::P_TEST => array(cValidator::CHK_STR), ), ); } /** * Function to get form buttons * * @author Aivars Žukovskis * @category cmsMessages Controller * @return array of data */ public function getFormButtons(){ return array( 1 => array( 'id' => 'fsave', 'lbl' => $this->gM('Save') ), 2 => array( 'id' => 'fcancel', 'lbl' => $this->gM('Cancel') ) ); } }