'added DESC'); protected $_columns4ExportCsv; protected $_hasFilter = true; protected $_thanksPageAlias = 'thanks.html'; protected $_exportFileName; protected $_settingsPrefix; 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->_doc->addStylesheet('css/print/admin-print.css', array('media' => 'print')); $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); $fp = fopen('php://output', 'w'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); header('Content-type: text/comma-separated-values'); header('Content-Disposition: attachment; filename="' . $this->_getExportFileName() . '"'); $columns = $this->_getColumns4ExportCsv(); fputcsv($fp, $columns); $blankRow = array_combine(array_keys($columns), array_fill(0, count($columns), '')); while ($row = $stmt->fetch()) { $row = $this->_getRow4ExportCsv($row); $row = array_intersect_key($row, $columns); $row = array_merge($blankRow, $row); fputcsv($fp, $row); } fclose($fp); $this->_postExportCsv(); exit; } protected function _postExportCsv() { $this->_log(); return $this; } protected function _getExportFileName() { if (null == $this->_exportFileName) { $this->_exportFileName = $this->getConfig('itemsName') . ' ' . date('M d Y') . '.csv'; } return $this->_exportFileName; } 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; } 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() { $content = ''; $currentItem = str_replace(array('App_', 'App\\', 'View'), '', get_class($this)); $currentPage = Qs_SiteMap::findFirst(null, array('type' => $currentItem)); if ($currentPage && !empty($currentPage['sub']) && array_key_exists($this->_thanksPageAlias, $currentPage['sub']) ) { $page = $currentPage['sub'][$this->_thanksPageAlias]; $htmlBlock = null; foreach ($page['items'] as $item) { if ($item['type'] == 'HtmlBlock_') { $htmlBlock = $item; break; } } if (!empty($htmlBlock)) { $htmlBlockView = new App_HtmlBlock_View($htmlBlock); $data = $htmlBlockView->getData(); if (is_array($data) && array_key_exists('content', $data)) { $content = $data['content']; } } } return ($content) ? $content : $this->getConfig('thanksPageDefautlMessage'); } }