/**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 protected function createModel($detailed, $action)
 {
     $fields = array();
     // Export all
     if ('export' === $action) {
         $detailed = true;
     }
     $organizations = $this->util->getDbLookup()->getOrganizations();
     $fields[] = 'gtr_track_name';
     $sql = "CASE WHEN gtr_organizations LIKE '%%|%s|%%' THEN 1 ELSE 0 END";
     foreach ($organizations as $orgId => $orgName) {
         $fields['O' . $orgId] = new \Zend_Db_Expr(sprintf($sql, $orgId));
     }
     $fields['total'] = new \Zend_Db_Expr("(LENGTH(gtr_organizations) - LENGTH(REPLACE(gtr_organizations, '|', ''))-1)");
     $fields[] = 'gtr_id_track';
     $select = $this->db->select();
     $select->from('gems__tracks', $fields);
     $model = new \MUtil_Model_SelectModel($select, 'track-verview');
     $model->setKeys(array('gtr_id_track'));
     $model->resetOrder();
     $model->set('gtr_track_name', 'label', $this->_('Track name'));
     $model->set('total', 'label', $this->_('Total'));
     $model->setOnTextFilter('total', array($this, 'noTextFilter'));
     foreach ($organizations as $orgId => $orgName) {
         $model->set('O' . $orgId, 'label', $orgName, 'tdClass', 'rightAlign', 'thClass', 'rightAlign');
         $model->setOnTextFilter('O' . $orgId, array($this, 'noTextFilter'));
         if ($action !== 'export') {
             $model->set('O' . $orgId, 'formatFunction', array($this, 'formatCheckmark'));
         }
     }
     // \MUtil_Model::$verbose = true;
     return $model;
 }
 /**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 public function createModel($detailed, $action)
 {
     $select = $this->getSelect();
     // \MUtil_Model::$verbose = true;
     $model = new \MUtil_Model_SelectModel($select, 'summary');
     // Make sure of filter and sort for these fields
     $model->set('gro_id_order');
     $model->set('gto_id_track');
     $model->set('gto_id_organization');
     $model->resetOrder();
     $model->set('gro_round_description', 'label', $this->_('Round'));
     $model->set('gsu_survey_name', 'label', $this->_('Survey'));
     $model->set('answered', 'label', $this->_('Answered'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     $model->set('missed', 'label', $this->_('Missed'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     $model->set('open', 'label', $this->_('Open'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     $model->set('total', 'label', $this->_('Total'), 'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('future',   'label', $this->_('Future'),   'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('unknown',  'label', $this->_('Unknown'),  'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('is',       'label', ' ',                  'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('success',  'label', $this->_('Success'),    'tdClass', 'centerAlign', 'thClass', 'centerAlign');
     // $model->set('removed',  'label', $this->_('Removed'),  'tdClass', 'deleted centerAlign',
     //         'thClass', 'centerAlign');
     $model->set('ggp_name', 'label', $this->_('Filler'), 'column_expression', new \Zend_Db_Expr('COALESCE(gems__track_fields.gtf_field_name, gems__groups.ggp_name)'));
     $filter = $this->getSearchFilter($action !== 'export');
     if (!(isset($filter['gto_id_organization']) && $filter['gto_id_organization'])) {
         $model->addFilter(array('gto_id_organization' => $this->currentUser->getRespondentOrgFilter()));
     }
     if (isset($filter['gto_id_track']) && $filter['gto_id_track']) {
         // Add the period filter
         if ($where = \Gems_Snippets_AutosearchFormSnippet::getPeriodFilter($filter, $this->db)) {
             $select->joinInner('gems__respondent2track', 'gto_id_respondent_track = gr2t_id_respondent_track', array());
             $model->addFilter(array($where));
         }
     } else {
         $model->setFilter(array('1=0'));
         $this->autofilterParameters['onEmpty'] = $this->_('No track selected...');
     }
     return $model;
 }
 /**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 protected function createModel($detailed, $action)
 {
     // Export all
     if ('excel' === $action) {
         $detailed = true;
     }
     if ($detailed) {
         $year = $this->_('Year');
         $month = $this->_('Month');
         $fields[$year] = new \Zend_Db_Expr("YEAR(gr2o_created)");
         $fields[$month] = new \Zend_Db_Expr("MONTH(gr2o_created)");
     }
     $consents = $this->util->getDbLookup()->getUserConsents();
     $deleteds = array('' => '') + $this->util->getReceptionCodeLibrary()->getRespondentDeletionCodes();
     $sql = "SUM(CASE WHEN grc_success = 1 AND gr2o_consent = '%s' THEN 1 ELSE 0 END)";
     foreach ($consents as $consent => $translated) {
         $fields[$translated] = new \Zend_Db_Expr(sprintf($sql, $consent));
     }
     $fields[$this->_('Total OK')] = new \Zend_Db_Expr("SUM(CASE WHEN grc_success = 1 THEN 1 ELSE 0 END)");
     $sql = "SUM(CASE WHEN gr2o_reception_code = '%s' THEN 1 ELSE 0 END)";
     foreach ($deleteds as $code => $translated) {
         $fields[$translated] = new \Zend_Db_Expr(sprintf($sql, $code));
     }
     $fields[$this->_('Dropped')] = new \Zend_Db_Expr("SUM(CASE WHEN grc_success = 0 THEN 1 ELSE 0 END)");
     $fields[$this->_('Total')] = new \Zend_Db_Expr("COUNT(*)");
     $select = $this->db->select();
     $select->from('gems__respondent2org', $fields)->joinInner('gems__reception_codes', 'gr2o_reception_code = grc_id_reception_code', array())->joinInner('gems__organizations', 'gr2o_id_organization = gor_id_organization', array('gor_name', 'gor_id_organization'));
     $select->group(array('gor_name', 'gor_id_organization'));
     if ($detailed) {
         $select->group(array($fields[$year], $fields[$month]));
     }
     $model = new \MUtil_Model_SelectModel($select, 'consent-plan');
     $model->setKeys(array('gor_id_organization'));
     $model->resetOrder();
     $model->set('gor_name', 'label', $this->_('Organization'));
     foreach ($fields as $field => $expr) {
         $model->set($field, 'label', $field, 'tdClass', 'rightAlign', 'thClass', 'rightAlign');
     }
     foreach ($deleteds as $code => $translated) {
         $model->set($translated, 'tdClass', 'rightAlign smallTime', 'thClass', 'rightAlign smallTime');
     }
     foreach (array($this->_('Total OK'), $this->_('Dropped'), $this->_('Total')) as $name) {
         $model->set($name, 'itemDisplay', \MUtil_Html::create('strong'), 'tableHeaderDisplay', \MUtil_Html::create('em'), 'tdClass', 'rightAlign selectedColumn', 'thClass', 'rightAlign selectedColumn');
     }
     if ($detailed) {
         $model->set($month, 'formatFunction', $this->util->getLocalized()->getMonthName);
     }
     // Only show organisations the user is allowed to see
     $allowed = $this->currentUser->getAllowedOrganizations();
     $model->setFilter(array('gr2o_id_organization' => array_keys($allowed)));
     // \MUtil_Model::$verbose = true;
     return $model;
 }