'sorter'); protected $_filterFields = array('School'); protected $_actions = array('list', 'cancel', 'new', 'insert', 'unlink', 'loadUsers', 'back'); /** * @var Qs_ViewController_List */ protected $_list; public function exec() { $this->_pageHeaderTemplates['headerNew'] = 'Add Contact'; $this->_pageHeaderTemplates['headerInsert'] = $this->_pageHeaderTemplates['headerNew']; return parent::exec(); } protected function _getDefaultLinks() { $links = array(); $links[] = array( 'title' => 'Add Contact', 'url' => $this->url(array('action' => 'new')), ); return $links; } protected function _getList() { if ($this->_list) { return $this->_list; } parent::_getList(); $this->_list->urlPatternUnlink = $this->url(array('action' => 'unlink')) . '&id=%s&userId=%s'; if (($url = Qs_SiteMap::findFirst(null, array('type' => 'School_Admin'), null, 'url'))) { $this->_list->urlPatternSchool = $url . '?action=edit&id=%s'; } if (($url = Qs_SiteMap::findFirst(null, array('type' => 'User_Admin'), null, 'url'))) { $this->_list->urlPatternUser = $url . '?action=edit&id=%s'; } return $this->_list; } /** * @param Qs_ViewController_List $list * * @return $this */ protected function _bindListColumns($list) { $list->addColumn('no', 'no', array('orderBy' => 'sorter')); $list->addColumn('school', 'name', array('title' => 'School', 'orderBy' => 'name')); $list->addColumn('contacts', 'contacts', array('title' => 'Contacts')); $list->addColumn( 'options', 'options', array( 'title' => 'Options', 'actions' => array( 'new' => array( 'title' => 'Add Contact', 'link' => $this->url(array('action' => 'new')) ) ) ) ); return $this; } protected function _doUnlink() { $id = Qs_Request::getGetValue('id'); $userId = Qs_Request::getGetValue('userId'); if (empty($id) || empty($userId)) { $this->_setBackError('Wrong request'); $this->_doBack(); exit; } $this->_getDataObj()->setPrimaryKey($id); $this->_getDataObj()->unlink($userId); $this->_doBack(); } protected function _doNew() { if ($this->_getDataObj()->getPrimaryKey() && !$this->_getDataObj()->getData()) { $this->_setBackError('School is not available'); $this->_doBack(); exit; } $this->_setHeader(); return parent::_doNew(); } protected function _getNewForm() { if (($id = $this->_getDataObj()->getPrimaryKey())) { $this->_formDefaults['id'] = $id; } return parent::_getNewForm(); } /** * @param $form Qs_Form * * @return $this */ protected function _bindFormFields($form) { $form->addElement( 'select', 'id', array( 'label' => 'School', 'required' => true, 'multiOptions' => array('' => 'Select School') + (array) $this->_getDataObj()->getSchool4Select(false), ) ); $form->addElement( 'select', 'userId', array( 'label' => 'User', 'required' => true, 'multiOptions' => array('' => 'Select User'), ) ); if ($this->_getDataObj()->getPrimaryKey()) { $form->getElement('userId')->setMultiOptions( array('' => 'Select User') + (array)$this->_getDataObj()->getNonContactUser4Select() ); } else { $form->getElement('userId')->setAttrib('disabled', true); } return $this; } /** * @param Qs_Form $form * * @return $this */ protected function _renderMainForm($form) { $options = array( 'requestUrl' => $this->url(), 'ids' => array( 'form' => $form->getId(), 'id' => $form->getElement('id')->getId(), 'userId' => $form->getElement('userId')->getId(), ), ); $this->_doc->addScript('js/app/report/contact/admin/form.js'); $this->_doc->addInitObject('app.report.contact.admin.Form', array($options)); return parent::_renderMainForm($form); } protected function _doLoadUsers() { $this->_doLoadUsersAjax(); exit; } protected function _doLoadUsersAjax() { if (null == ($schoolId = Qs_Request::getRequestValue('id'))) { header('HTTP/1.0 400 Bad Request'); exit; } $userList = $this->_getDataObj()->getNonContactUserList($schoolId); $this->_displayJson(array('userList' => $userList)); exit; } protected function _doInsert() { $this->_setHeader(); $form = $this->_getNewForm(); if ($form->validate()) { $this->_initFromForm($form); $this->_getDataObj()->link($form->getValue('userId')); $this->_postInsert(); $this->_setBackMessage(Qs_ViewController::MSG_ADDED); $this->_doBack(); } else { $this->_renderMainForm($form); } return $this; } }