true, 'defaultOrderBy' => 'dueIdx'); protected $_allowedExtensions = array( '.pdf', '.txt', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.odt', '.ods', '.odp', '.png', '.jpg', '.jpeg', ); public function exec() { App_Doc_Acl::requireReportAccess(); return parent::exec(); } protected function _initAction() { $this->createReportsSymlinks(); parent::_initAction(); if ($this->_action === 'upload') { if (($reportId = Qs_Request::getGetValue('id')) && $report = $this->_getDataObj()->getReportById($reportId) ) { $this->_formDefaults = $report; foreach ($this->_formDefaults as $item => &$value) { $value = $value == '' ? '-' : $value; } $this->_formDefaults['dueDate'] = $report['dueDateMonth'] == '' ? '-' : App_Report_AdminObj::getMonthName($report['dueDateMonth']) . ' ' . $report['dueDateDay']; $this->_formDefaults['reportId'] = $reportId; } else { $this->_doc->display404(); } } return $this; } public function preDispatch(&$items) { parent::_initAction(); if ($this->_action === 'upload') { $htmlBlockIndex = null; foreach ($items as $index => $item) { if ($item['type'] == 'HtmlBlock_') { $htmlBlockIndex = $index; break; } } if (null !== $htmlBlockIndex) { unset($items[$htmlBlockIndex]); } } return $this; } protected function _getDefaultLinks() { $links = array(); if ($this->_getDataObj()->hasUserReports()) { $info = App_Report_View::getReportArchiveAccessFileInfo(); if ($info) { $links[] = array( 'title' => 'Download Archive', 'url' => $info['url'] ); $links[] = array( 'title' => 'Remove Archive', 'url' => $this->url(array('action' => 'removeArchive')) ); } else { $links[] = array( 'title' => 'Create Reports Archive', 'url' => $this->url(array('action' => 'archive')) ); } } return $links; } static public function createReportsSymlinks() { $userId = App_User_Auth::getInstance()->getData('id'); $schoolId = App_User_Auth::getInstance()->getData('idSchool'); if ($userId === null) { return false; } self::createReportArchiveSymlink($schoolId); $reportObj = new App_Report_Obj(); $reports = $reportObj->getReportsByUserId($userId); $reportPath = constant('WWW_PATH') . '/' . App_Report_Obj::REPORT_PATH; $reportAccessPath = constant('WWW_PATH') . '/' . App_Report_Obj::REPORT_ACCESS_PATH . '/' . session_id(); if ($reports && !is_dir($reportAccessPath)) { mkdir($reportAccessPath); } foreach ($reports as $report) { if (file_exists($reportPath . '/' . $report) && !file_exists($reportAccessPath . '/' . $report)) { symlink($reportPath . '/' . $report, $reportAccessPath . '/' . $report); } } return true; } public static function createReportArchiveSymlink($schoolId) { if (!$schoolId) { return; } $archiveName = sprintf(App_Report_Archive_View::REPORT_ARCHIVE_FILE_TPL, $schoolId); $filePath = constant('WWW_PATH') . '/' . App_Report_AbstractObj::REPORT_PATH . '/' . $archiveName; if (file_exists($filePath)) { $accessPath = constant('WWW_PATH') . '/' . App_Report_Obj::REPORT_ACCESS_PATH . '/' . session_id(); $accessFile = $accessPath . '/' . $archiveName; if (!is_dir($accessPath)) { mkdir($accessPath); } if (!file_exists($accessFile)) { symlink($filePath, $accessFile); } } } public static function getReportArchiveAccessFileInfo() { $auth = App_User_Auth::getInstance(); if ($auth->isLoggedIn() && ($schoolId = $auth->getData('idSchool'))) { $archiveName = sprintf(App_Report_Archive_View::REPORT_ARCHIVE_FILE_TPL, $schoolId); $suffix = App_Report_Obj::REPORT_ACCESS_PATH . '/' . session_id() . '/' . $archiveName; $file = constant('WWW_PATH') . '/' . $suffix; if (file_exists($file)) { $result = array('url' => constant('BASE_URL') . '/' . $suffix, 'file' => $file); return $result; } } return null; } protected function _doUpload() { $this->_doc->setHeader('Upload Report'); $this->_doc->setTitle('Upload Report'); return parent::_doNew(); } protected function _postInsert() { $this->_sendAdminNotification(); return parent::_postInsert(); } protected function _sendAdminNotification() { $schoolReport = $this->_getDataObj()->clearData()->getData(); $mailData = $this->_prepareAdminNotificationData($schoolReport); $from = App_Settings_Obj::get('reportSubmissionEmailFrom'); $to = App_Settings_Obj::getFormEmails('reportSubmissionEmailTo'); $subject = App_Settings_Obj::get('reportSubmissionEmailSubject'); $body = App_Settings_Obj::get('reportSubmissionEmailBody'); $body = str_replace(array_keys($mailData), $mailData, $body); return $this->_sendMail(compact('from', 'to', 'subject', 'body')); } protected function _prepareAdminNotificationData($schoolReport) { $report = $this->_getDataObj()->getReportById($schoolReport['reportId']); $user = $this->_doc->getAuthData(); $school = App_School_Obj::getById($schoolReport['schoolId']); $data = array( '{dueData}' => date('F j', mktime(0, 0, 0, $report['dueDateMonth'], $report['dueDateDay'])), '{section}' => $report['section'], '{topic}' => $report['topic'], '{description}' => $report['description'], '{reportUrl}' => Qs_SiteMap::findFirst(null, array('type' => 'Report_AdminDetail'), null, 'url') . '/' . $report['id'], '{reportContacts}' => Qs_SiteMap::findFirst(null, array('type' => 'Report_Contact_Admin'), null, 'url') . '?query=' . rawurlencode($school['name']), '{file}' => $schoolReport['file'], '{comments}' => $schoolReport['comments'], '{submittedOn}' => date('m/d/Y', strtotime($schoolReport['changed'])), '{schoolName}' => $school['name'], '{schoolUrl}' => Qs_SiteMap::findFirst(null, array('type' => 'School_Admin'), null, 'url') . '?action=edit&id=' . $school['id'], '{schoolContacts}' => Qs_SiteMap::findFirst(null, array('type' => 'User_Admin'), null, 'url') . '?idSchool=' . $school['id'], '{userName}' => $user['name'], '{userPosition}' => $user['position'], '{userPhone}' => $user['phone'], '{userEmail}' => $user['email'], ); function __escape(&$val) { $val = trim($val); if (empty($val)) { $val = '-'; } $val = htmlspecialchars($val); }; array_filter($data, '__escape'); return $data; } protected function _getFilterForm() { $form = $this->_getBaseForm(); $form->setMethod('get') ->setAttrib('id', strtolower($this->getApplicationName()) . '-filter-form') ->setAttrib('class', 'filter_form ' . strtolower($this->getApplicationName()) . '_filter_form') ->setAjaxValidation(false); $this->_bindFilterFields($form); return $form; } protected function _bindFilterButtons($form) { parent::_bindFilterButtons($form); $form->removeElement('btnCancel'); $form->getElement('btnSearch')->setAttrib('class', 'buttons search_btn'); return $this; } protected function _bindFormButtons($form) { parent::_bindFormButtons($form); $form->getElement('btnSubmit')->setLabel('Upload'); return $this; } protected function _addLegendHeaderItem() { $item = array( 'tpl' => Qs_SiteMap::getTemplate('Report/legend-header.tpl'), 'statistics' => $this->_getDataObj()->getStatusStatistics($this->_getDataObj()->getSchoolId()), ); $this->_additem($item); return $this; } /** * @param Qs_Form $form * * @return $this */ protected function _bindFilterFields($form) { $this->_addLegendHeaderItem(); parent::_bindFilterFields($form); $this->_bindFilterButtons($form); $form->getElement('query')->setLabel('Search:'); $form->addElement( 'static', 'reportFilterStatus', array( 'label' => 'Filter by Report Status:' ) ); $form->addElement( 'select', 'reportsStatus', array( 'label' => '', 'multiOptions' => array('' => 'All Reports') + (array)App_Report_Obj::getStatusPairs(), 'onchange' => 'this.form.submit();' ) ); return $this; } protected function _bindFormFields(Qs_Form $form) { $form->addElement( 'static', 'dueDate', array( 'label' => 'Date Due' ) ); $form->addElement( 'static', 'section', array( 'label' => 'Section' ) ); $form->addElement( 'static', 'topic', array( 'label' => 'Topic' ) ); $form->addElement( 'static', 'description', array( 'label' => 'Description' ) ); $form->addElement( 'hidden', 'reportId' ); $form->addElement( 'hidden', 'userId' ); $form->addElement( 'hidden', 'schoolId' ); $form->addElement( 'extendedFile', 'file', array( 'label' => 'Report', 'destination' => constant('WWW_PATH') . '/' . App_Report_Obj::REPORT_PATH, 'required' => true, 'description' => 'Please note there is a file limit of 10MB ' . 'and allowed formats are: ' . implode(', ', $this->_allowedExtensions) ) ); $extensions = array(); foreach ($this->_allowedExtensions as $ext) { $extensions[] = trim($ext, '.'); } $extensionValidator = new Zend_Validate_File_Extension($extensions); $extensionValidator->setMessage( "File format is not allowed", Zend_Validate_File_Extension::FALSE_EXTENSION ); $form->getElement('file')->addValidator($extensionValidator); $sizeValidator = new Zend_Validate_File_Size(array('max' => '10MB')); $sizeValidator->setMessage( "Maximum allowed file size is '%max%' but '%size%' detected", Zend_Validate_File_Size::TOO_BIG ); $form->getElement('file')->addValidator($sizeValidator); $form->addElement( 'textarea', 'comments', array( 'label' => 'Comments', 'rows' => 5, ) ); return $this; } protected function _addListItem($list = null, $template = 'list.tpl') { self::initTips(); $list = $list ? $list : $this->_getList(); $list->userReportUploadUrl = $this->url(array('action' => 'upload')); return parent::_addListItem($list, $template); } /** * @param Qs_ViewController_List $list * * @return $this */ protected function _bindListColumns($list) { $list->addColumn( 'dueDate', 'dueDate', array('title' => 'Date Due', 'orderBy' => 'dueIdx', 'defaultText' => '-') ); $list->addColumn( 'text', 'section', array('title' => 'Section', 'orderBy' => 'section', 'attribs' => 'class=text-center') ); $list->addColumn( 'text', 'topic', array('title' => 'Topic', 'orderBy' => 'topic') ); $list->addColumn( 'text', 'description', array('title' => 'Description', 'defaultText' => '-') ); $list->addColumn( 'reports', 'reports', array('title' => 'Reports') ); $list->addColumn( 'uploads', 'uploads', array('title' => 'Upload') ); return $this; } public static function initTips() { /** @var Qs_Doc $doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/jquery-qtip.js'); $doc->addScript('js/jquery-qtip-extended.js'); $doc->addInitFunction('$(".rep-links").find("[title]").qtip'); } protected function _doArchive() { $userId = App_User_Auth::getInstance()->getData('id'); $schoolId = App_User_Auth::getInstance()->getData('idSchool'); $archiveView = new App_Report_Archive_View(); $archiveView->setDoc($this->_doc); $archiveView->actionArchive($schoolId, $userId); } protected function _doRemoveArchive() { $schoolId = App_User_Auth::getInstance()->getData('idSchool'); $archiveView = new App_Report_Archive_View(); $archiveView->setDoc($this->_doc); $archiveView->actionRemoveArchive($schoolId); } }