/**
  * Handles creating or replacing the view for this survey
  *
  * @param \Gems_Tracker_Survey       $viewName
  * @param \MUtil_Model_ModelAbstract $answerModel
  */
 protected function replaceCreateView(\Gems_Tracker_Survey $survey, \MUtil_Model_ModelAbstract $answerModel)
 {
     $viewName = $this->getViewName($survey);
     $responseDb = $this->project->getResponseDatabase();
     $fieldSql = '';
     foreach ($answerModel->getItemsOrdered() as $name) {
         if (true === $answerModel->get($name, 'survey_question') && !in_array($name, array('submitdate', 'startdate', 'datestamp')) && !$answerModel->is($name, 'type', \MUtil_Model::TYPE_NOVALUE)) {
             // Only real answers
             $fieldSql .= ',MAX(IF(gdr_answer_id = ' . $responseDb->quote($name) . ', gdr_response, NULL)) AS ' . $responseDb->quoteIdentifier($name);
         }
     }
     if ($fieldSql > '') {
         $dbConfig = $this->db->getConfig();
         $tokenTable = $this->db->quoteIdentifier($dbConfig['dbname'] . '.gems__tokens');
         $createViewSql = 'CREATE OR REPLACE VIEW ' . $responseDb->quoteIdentifier($viewName) . ' AS SELECT gdr_id_token';
         $createViewSql .= $fieldSql;
         $createViewSql .= "FROM gemsdata__responses join " . $tokenTable . " on (gto_id_token=gdr_id_token and gto_id_survey=" . $survey->getSurveyId() . ") GROUP BY gdr_id_token;";
         try {
             $responseDb->query($createViewSql)->execute();
         } catch (Exception $exc) {
             $responseConfig = $responseDb->getConfig();
             $dbUser = $this->db->quoteIdentifier($responseConfig['username']) . '@' . $this->db->quoteIdentifier($responseConfig['host']);
             $statement = "GRANT SELECT ON  " . $tokenTable . " TO " . $dbUser;
             $this->getBatch()->addMessage(sprintf($this->_("Creating view failed, try adding rights using the following statement: %s"), $statement));
         }
     }
 }
 /**
  * Use this function for a default application of this dependency to the model
  *
  * @param \MUtil_Model_ModelAbstract $model Try not to store the model as variabe in the dependency (keep it simple)
  */
 public function applyToModel(\MUtil_Model_ModelAbstract $model)
 {
     if ($this->applyOnChange) {
         foreach ($this->getDependsOn() as $name) {
             if ($model->is($name, 'elementClass', 'Checkbox')) {
                 if (!$model->has($name, 'onclick')) {
                     $model->set($name, 'onclick', $this->onChangeJs);
                 }
             } else {
                 if (!$model->has($name, 'onchange')) {
                     $model->set($name, 'onchange', $this->onChangeJs);
                 }
             }
         }
     }
 }
 /**
  * Restores the header position of question before their corresponding question_sub
  *
  * When sub-questions with the same parent are shown continuous the parent is shown
  * once before them. When the sub-questions are displayed in seperate groups the
  * parent is shown once at their start.
  *
  * Stand alone headers without any corresponding value are removed. When they do have
  * a value of their own they are still shown, but their position may change according
  * to their sub-questions position. (NOTE: As in LimeSurvey their are no question
  * headers with values we leave it at this for the moment.)
  *
  * @param \MUtil_Model_ModelAbstract $model
  * @param array $currentNames The current names in use (allows chaining)
  * @return array Of the names of labels that should be shown
  */
 protected function restoreHeaderPositions(\MUtil_Model_ModelAbstract $model, array $currentNames)
 {
     $lastParent = null;
     $results = array();
     foreach ($currentNames as $name) {
         if ($model->is($name, 'type', \MUtil_Model::TYPE_NOVALUE)) {
             // Skip header types that contain no value
             continue;
         }
         if ($parent = $model->get($name, 'parent_question')) {
             // Check for change of parent
             if ($lastParent !== $parent) {
                 if (isset($results[$parent])) {
                     // Add another copy of the parent to the array
                     $results[] = $parent;
                 } else {
                     // Insert parent header on name if it was not shown before
                     $results[$parent] = $parent;
                 }
                 $lastParent = $parent;
             }
         } else {
             // Make sure a question (without parent) is picked up as parent too
             $lastParent = $name;
         }
         // If already set (as a $parent) this will not
         // redisplay the $parent as $result[$name] does
         // not change position
         $results[$name] = $name;
     }
     return $results;
 }
 /**
  * Translate textual null values to actual PHP nulls and trim any whitespace
  *
  * @param mixed $value
  * @param scalar $key The array key, optionally a model key as well
  * @return mixed
  */
 public function filterBasic(&$value, $key)
 {
     if (is_string($value) && $this->nullValue === strtoupper($value)) {
         $value = null;
         return;
     }
     if ($this->_targetModel instanceof \MUtil_Model_ModelAbstract) {
         if ($this->_targetModel->is($key, 'type', \MUtil_Model::TYPE_DATE)) {
             $format = $this->dateFormat;
         } elseif ($this->_targetModel->is($key, 'type', \MUtil_Model::TYPE_DATETIME)) {
             $format = $this->datetimeFormat;
         } elseif ($this->_targetModel->is($key, 'type', \MUtil_Model::TYPE_TIME)) {
             $format = $this->timeFormat;
         } else {
             $format = false;
         }
         if ($this->dateLocale && is_string($this->dateLocale)) {
             $this->dateLocale = new \Zend_Locale($this->dateLocale);
         }
         if ($format && \Zend_Date::isDate($value, $format, $this->dateLocale)) {
             $value = new \MUtil_Date($value, $format, $this->dateLocale);
             return;
         }
         $options = $this->_targetModel->get($key, 'multiOptions');
         if ($options && !isset($options[$value]) && in_array($value, $options)) {
             $value = array_search($value, $options);
         }
     }
     if (is_string($value)) {
         $value = trim($value);
         return;
     }
     return;
 }