'added DESC'); protected $_columns4ExportCsv; protected $_hasFilter = true; protected $_thanksPageAlias = 'thanks.html'; protected $_exportFileName; protected $_settingsPrefix; protected $_userUnsetFields = array('id', 'salt', 'autologinCode', 'recoverCode', 'recoverCodeExpirationDate', 'enabled', 'added', 'changed'); /** * Fields array with callbacks. For example: * * array( * 'userName' => array($this, 'prepareUserName') * ); * ... * public function prepareUserName(array $userData) * { * return $userData['firstName'] . ' ' . $userData['lastName']; * } * * @var array */ protected $_userFieldRules = array(); protected function _getNewForm(array $options = array()) { if ($this->_doc->hasAuth() && ($userData = $this->_doc->getAuthData())) { foreach ($this->_userUnsetFields as $field) { unset($userData[$field]); } if (!array_key_exists('defaults', $options)) { $options['defaults'] = array(); } foreach ($this->_userFieldRules as $field => $callback) { if (is_callable($callback)) { $userData[$field] = call_user_func($callback, $userData); } } $options['defaults'] = array_merge($userData, $options['defaults']); } return $this->_getFormInstance('new', $options); } protected function _postInsert() { $data = $this->_getDataObj()->clearData()->getData(); $this->_sendAdminNotification($data); $this->_log(); $this->_redirect(); return $this; } protected function _redirect() { $this->redirect($this->url() . '/' . $this->_thanksPageAlias); } protected function _sendAdminNotification($data) { $to = App_Settings_Obj::getFormEmails($this->_settingsPrefix . 'To'); $body = App_Settings_Obj::get($this->_settingsPrefix . 'Body'); if (empty($to) || empty($body)) { return false; } $subject = App_Settings_Obj::get($this->_settingsPrefix . 'Subject'); $from = App_Settings_Obj::getEmailFrom($this->_settingsPrefix . 'From'); $mailData = $this->_getMailData($data); foreach ($mailData as $field => $value) { $body = str_replace('{' . $field . '}', $value, $body); } $mail = new Qs_Mail(); $mail->setFrom($from); $mail->setSubject($subject); Qs_Mail::cutImageBaseUrl($body, BASE_URL); $mail->setHtml($body, null, WWW_PATH); $mail->addTo($to); $mail->send(); return $this; } protected function _doView() { $item = $this->_getDataObj()->getData(); if (empty($item)) { $this->_doc->displayError('Invalid submission ID'); return $this; } $this->_getDataObj()->setViewed(); $item['config'] = $this->getConfig()->toArray(); $item['tpl'] = $this->getTemplate('view.tpl'); $this->_doc->addItem($item); $this->_postView(); return $this; } protected function _getDefaultLinks() { $links = parent::_getDefaultLinks(); $params = array(); if ($orderBy = Qs_Request::getGetValue('orderBy')) { $params['orderBy'] = $orderBy; } $filter = $this->_dataObj->getFilter(); foreach ($filter as $field => $value) { if ($value) { $params[$field] = $value; } } $params['action'] = 'exportCsv'; $links['exportCsv'] = array('title' => 'Export to .csv', 'url' => $this->url($params)); return $links; } protected function _doExportCsv() { if ($this->_getDataObj()->hasFilter()) { $form = $this->_getFilterForm(); if ($form->validate()) { $this->_getDataObj()->addFilter($form->getValues()); } } $options = array(); $options['order'] = array($this->_getList()->getOrderBy()); $this->_dataObj->setSelectOptions($options); $stmt = $this->_getDataObj()->getListStatement($options); $columns = $this->_getColumns4ExportCsv(); $blankRow = array_combine(array_keys($columns), array_fill(0, count($columns), '')); $rowCallback = function () use ($stmt, $columns, $blankRow) { $row = $stmt->fetch(); if ($row) { $row = $this->_getRow4ExportCsv($row); $row = array_intersect_key($row, $columns); $row = array_merge($blankRow, $row); } return $row; }; $helper = new ExportCsv([ 'fileName' => $this->getConfig('itemsName') . ' {date}.csv', 'columns' => $columns, 'rowCallback' => $rowCallback, ]); $helper->export(); $this->_postExportCsv(); exit; } protected function _postExportCsv() { $this->_log(); return $this; } protected function _getColumns4ExportCsv() { if (null === $this->_columns4ExportCsv) { $this->_columns4ExportCsv = array('added' => 'Submitted'); $fields = array_keys(Qs_Array::excludeKey($this->_getDataObj()->getTableMeta(), 'id', 'added', 'changed')); foreach ($fields as $field) { $this->_columns4ExportCsv[$field] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $field)); } } return $this->_columns4ExportCsv; } protected function _getRow4ExportCsv(&$data) { $row = str_replace(array("\n", "\r"), ' ', $data); return $row; } /** * @param array $data * @return array; * @throws Qs_Exception */ protected function _getMailData(array $data) { throw new Qs_Exception('_getMailData() not implemented'); } protected function _getFilterFields() { if (null === $this->_filterFields) { $this->_filterFields = $this->_getDataObj()->getFilterFields(); foreach ($this->_filterFields as $key => $value) { if (!is_scalar($value)) { unset($this->_filterFields[$key]); } } $this->_filterFields = array_flip($this->_filterFields); $columns = $this->_getColumns4ExportCsv(); $this->_filterFields = array_intersect_key($columns, $this->_filterFields); } return $this->_filterFields; } public function getApplicationName() { if (null === $this->_applicationName) { $name = get_class($this); $name = substr($name, 4); $this->_applicationName = substr($name, 0, strrpos($name, '_') ?: null); } return $this->_applicationName; } protected function _getLog() { if (null === $this->_log) { parent::_getLog(); $this->_log->setAction('insert', 'Submitted new "%formHeader%" Form'); } return $this->_log; } protected function _getThankYouMessage() { $settingsField = $this->_settingsPrefix . 'ThanksMessage'; if (($message = App_Settings_Obj::get($settingsField))) { return $message; }; return $this->getConfig('thanksPageDefaultMessage'); } }