/**
  * Get the date format used for the appointment date
  *
  * @return array
  */
 public function getDateFormat()
 {
     $model = $this->getModel();
     $format = $model->get('gap_admission_time', 'dateFormat');
     if (!$format) {
         $format = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
     }
     return $format;
 }
 /**
  * Sets the labels, format functions, etc...
  *
  * @param boolean $detailed True when shopwing detailed information
  * @param boolean $edit When true use edit settings
  * @return \Gems_Tracker_Model_TrackModel
  */
 public function applyFormatting($detailed = false, $edit = false)
 {
     $translated = $this->util->getTranslated();
     $translator = $this->getTranslateAdapter();
     if ($edit) {
         $dateFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
     } else {
         $dateFormat = $translated->dateFormatString;
     }
     $this->resetOrder();
     $this->set('gtr_track_name', 'label', $translator->_('Name'));
     $this->set('gtr_track_class', 'label', $translator->_('Track Engine'), 'multiOptions', $this->tracker->getTrackEngineList($detailed));
     $this->set('gtr_survey_rounds', 'label', $translator->_('Surveys'));
     $this->set('gtr_active', 'label', $translator->_('Active'), 'multiOptions', $translated->getYesNo());
     $this->set('gtr_date_start', 'label', $translator->_('From'), 'dateFormat', $dateFormat, 'formatFunction', $translated->formatDate);
     $this->set('gtr_date_until', 'label', $translator->_('Use until'), 'dateFormat', $dateFormat, 'formatFunction', $translated->formatDateForever);
     $this->setIfExists('gtr_code', 'label', $translator->_('Track code'), 'size', 10, 'description', $translator->_('Optional code name to link the track to program code.'));
     if ($detailed) {
         $events = $this->loader->getEvents();
         $caList = $events->listTrackCalculationEvents();
         if (count($caList) > 1) {
             $this->setIfExists('gtr_calculation_event', 'label', $translator->_('Before (re)calculation'), 'multiOptions', $caList);
         }
         $coList = $events->listTrackCompletionEvents();
         if (count($coList) > 1) {
             $this->setIfExists('gtr_completed_event', 'label', $translator->_('After completion'), 'multiOptions', $coList);
         }
         $bfuList = $events->listTrackBeforeFieldUpdateEvents();
         if (count($bfuList) > 1) {
             $this->setIfExists('gtr_beforefieldupdate_event', 'label', $translator->_('Before field update'), 'multiOptions', $bfuList);
         }
         $fuList = $events->listTrackFieldUpdateEvents();
         if (count($fuList) > 1) {
             $this->setIfExists('gtr_fieldupdate_event', 'label', $translator->_('After field update'), 'multiOptions', $fuList);
         }
         $this->setIfExists('gtr_organizations', 'label', $translator->_('Organizations'), 'elementClass', 'MultiCheckbox', 'multiOptions', $this->util->getDbLookup()->getOrganizationsWithRespondents(), 'required', true);
         $ct = new \MUtil_Model_Type_ConcatenatedRow('|', $translator->_(', '));
         $ct->apply($this, 'gtr_organizations');
     }
     if ($edit) {
         $this->set('toggleOrg', 'elementClass', 'ToggleCheckboxes', 'selectorName', 'gtr_organizations');
         $this->set('gtr_track_name', 'minlength', 4, 'size', 30, 'validators[unique]', $this->createUniqueValidator('gtr_track_name'));
     }
     return $this;
 }
 /**
  * Get the date display format (zend style)
  *
  * @return string
  */
 protected function getDateFormat()
 {
     return \MUtil_Model_Bridge_FormBridge::getFixedOption('datetime', 'dateFormat');
 }
 /**
  * Save a single model item.
  *
  * @param array $newValues The values to store for a single model item.
  * @param array $filter If the filter contains old key values these are used
  * to decide on update versus insert.
  * @return array The values as they are after saving (they may change).
  */
 public function save(array $newValues, array $filter = null)
 {
     $keys = $this->getKeys();
     // This is the only key to save on, no matter
     // the keys used to initiate the model.
     $this->setKeys($this->_getKeysFor('gems__respondent2track'));
     // Change the end date until the end of the day
     if (isset($newValues['gr2t_end_date']) && $newValues['gr2t_end_date']) {
         $displayFormat = $this->get('gr2t_end_date', 'dateFormat');
         if (!$displayFormat) {
             $displayFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
         }
         // Of course do not do so when we got a time format
         if (!\MUtil_Date_Format::getTimeFormat($displayFormat)) {
             $newValues['gr2t_end_date'] = new \MUtil_Date($newValues['gr2t_end_date'], $displayFormat);
             $newValues['gr2t_end_date']->setTimeToDayEnd();
         }
     }
     $newValues = parent::save($newValues, $filter);
     $this->setKeys($keys);
     return $newValues;
 }
Пример #5
0
 /**
  * A ModelAbstract->setOnSave() function that tracks the change
  *
  * @see \MUtil_Model_ModelAbstract
  *
  * @param mixed $value The value being saved
  * @param boolean $isNew True when a new item is being saved
  * @param string $name The name of the current field
  * @param array $context Optional, the other values being saved
  * @return string Of the values concatenated
  */
 public function saveValue($value, $isNew = false, $name = null, array $context = array())
 {
     // \MUtil_Echo::track($value, $this->_changedValue);
     // Once the value is set (and e.g. stored in the database) do not overwrite it
     if ($this->_changedValue == $value) {
         return $value;
     }
     $compare = $this->_model->get($name, __CLASS__);
     if (!(is_array($compare) && 2 === count($compare))) {
         // Actually a valid setting, do nothring
         return $value;
     }
     list($trackedField, $oldValueField) = $compare;
     if (!isset($context[$trackedField], $context[$oldValueField])) {
         return $value;
     }
     if (!($context[$trackedField] && $context[$oldValueField])) {
         return $context[$trackedField] || $context[$oldValueField] ? $this->_changedValue : $this->_unchangedValue;
     }
     $storageFormat = $this->_model->get($trackedField, 'storageFormat');
     if (!$storageFormat) {
         return $context[$trackedField] == $context[$oldValueField] ? $this->_unchangedValue : $this->_changedValue;
     }
     if ($context[$oldValueField] instanceof \Zend_Date) {
         $oldValue = $context[$oldValueField];
     } else {
         $oldValue = new \MUtil_Date($context[$oldValueField], $storageFormat);
     }
     if ($context[$trackedField] instanceof \Zend_Date) {
         $currentValue = $context[$trackedField];
     } elseif (\Zend_Date::isDate($context[$trackedField], $storageFormat)) {
         $currentValue = new \MUtil_Date($context[$trackedField], $storageFormat);
     } else {
         if ($this->_model->has($trackedField, 'dateFormat')) {
             $secondFormat = $this->_model->get($trackedField, 'dateFormat');
         } else {
             $secondFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
         }
         if (!\Zend_Date::isDate($context[$trackedField], $secondFormat)) {
             // Cannot compare, do nothing
             return $value;
         }
         $currentValue = new \MUtil_Date($context[$trackedField], $secondFormat);
     }
     // \MUtil_Echo::track($trackedField, $oldValueField, $oldValue->toString(), $currentValue->toString(), $oldValue->getTimestamp() === $currentValue->getTimestamp());
     return $oldValue->getTimestamp() === $currentValue->getTimestamp() ? $this->_unchangedValue : $this->_changedValue;
 }
 /**
  * Function to allow the creation of search defaults in code
  *
  * @see getSearchFilter()
  *
  * @return array
  */
 public function getSearchDefaults()
 {
     if (!$this->defaultSearchData) {
         $inFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
         $now = new \MUtil_Date();
         $today = $now->toString($inFormat);
         $this->defaultSearchData = array('datefrom' => $today, 'dateused' => '_gto_valid_from gto_valid_until', 'dateuntil' => $today, 'main_filter' => 'all');
     }
     return parent::getSearchDefaults();
 }
 /**
  * Translate the set form options to a valid filter for the model
  * @param  array data existing options set in the form
  * @return array Filter for the model
  */
 public function getFilters($data)
 {
     $filters = array();
     $dateOutFormat = 'yyyy-MM-dd';
     $dateInFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
     if (isset($data['sid']) && is_array($data['sid'])) {
         foreach ($data['sid'] as $surveyId) {
             if ($surveyId) {
                 $filter = array();
                 if (isset($data['ids'])) {
                     $idStrings = $data['ids'];
                     $idArray = preg_split('/[\\s,;]+/', $idStrings, -1, PREG_SPLIT_NO_EMPTY);
                     if ($idArray) {
                         // Make sure output is OK
                         // $idArray = array_map(array($this->db, 'quote'), $idArray);
                         $filter['gto_id_respondent'] = $idArray;
                     }
                 }
                 if ($this->project->hasResponseDatabase()) {
                     $this->getResponseDatabaseFilter($data, $filter);
                 }
                 /*if ($this->project->hasResponseDatabase()) {
                       $this->_getResponseDatabaseFilter($data, $filter);
                   }*/
                 if (isset($data['tid']) && !empty($data['tid'])) {
                     $filter['gto_id_track'] = $data['tid'];
                 }
                 if (isset($data['sid'])) {
                     $filter['gto_id_survey'] = $surveyId;
                 }
                 if (isset($data['rounds']) && !empty($data['rounds'])) {
                     $filter['gto_round_description'] = $data['rounds'];
                 }
                 if (isset($data['oid'])) {
                     $filter['gto_id_organization'] = $data['oid'];
                 } else {
                     //Invalid id so when nothing selected... we get nothing
                     // $filter['organizationid'] = '-1';
                 }
                 if (isset($data['valid_from']) && !empty($data['valid_from'])) {
                     $filter[] = "gto_valid_from >= " . $this->db->quote(\MUtil_Date::format($data['valid_from'], $dateOutFormat, $dateInFormat)) . "";
                 }
                 if (isset($data['valid_until']) && !empty($data['valid_until'])) {
                     $filter[] = "gto_valid_until >= " . $this->db->quote(\MUtil_Date::format($data['valid_until'], $dateOutFormat, $dateInFormat)) . "";
                 }
                 /*if (isset($data['valid_until'])) {
                       $filter[] = 'gto_valid_until >= ' . $data['valid_until'];
                   }*/
                 $filter['grc_success'] = 1;
                 $filter[] = "gto_completion_time IS NOT NULL";
                 // Consent codes
                 /*$filter['consentcode'] = array_diff(
                                 (array) $this->util->getConsentTypes(),
                                 (array) $this->util->getConsentRejected()
                                 );
                 		*/
                 $filters[] = $filter;
             }
         }
     }
     // \Gems_Tracker::$verbose = true;
     return $filters;
 }
 /**
  * Helper function to generate a period query string
  *
  * @param array $filter A filter array or $request->getParams()
  * @param \Zend_Db_Adapter_Abstract $db
  * @param $inFormat Optional format to use for date when reading
  * @param $outFormat Optional format to use for date in query
  * @return string
  */
 public static function getPeriodFilter(array &$filter, \Zend_Db_Adapter_Abstract $db, $inFormat = null, $outFormat = null)
 {
     $from = array_key_exists('datefrom', $filter) ? $filter['datefrom'] : null;
     $until = array_key_exists('dateuntil', $filter) ? $filter['dateuntil'] : null;
     $period = array_key_exists(self::PERIOD_DATE_USED, $filter) ? $filter[self::PERIOD_DATE_USED] : null;
     unset($filter[self::PERIOD_DATE_USED], $filter['datefrom'], $filter['dateuntil']);
     if (!$period) {
         return;
     }
     if (null === $outFormat) {
         $outFormat = 'yyyy-MM-dd';
     }
     if (null === $inFormat) {
         $inFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
     }
     if ($from && \Zend_Date::isDate($from, $inFormat)) {
         $datefrom = $db->quote(\MUtil_Date::format($from, $outFormat, $inFormat));
     } else {
         $datefrom = null;
     }
     if ($until && \Zend_Date::isDate($until, $inFormat)) {
         $dateuntil = $db->quote(\MUtil_Date::format($until, $outFormat, $inFormat));
     } else {
         $dateuntil = null;
     }
     if (!($datefrom || $dateuntil)) {
         return;
     }
     switch ($period[0]) {
         case '_':
             // overlaps
             $periods = explode(' ', substr($period, 1));
             if ($datefrom && $dateuntil) {
                 return sprintf('(%1$s <= %4$s OR (%1$s IS NULL AND %2$s IS NOT NULL)) AND
                             (%2$s >= %3$s OR %2$s IS NULL)', $db->quoteIdentifier($periods[0]), $db->quoteIdentifier($periods[1]), $datefrom, $dateuntil);
             }
             if ($datefrom) {
                 return sprintf('%2$s >= %3$s OR (%2$s IS NULL AND %1$s IS NOT NULL)', $db->quoteIdentifier($periods[0]), $db->quoteIdentifier($periods[1]), $datefrom);
             }
             if ($dateuntil) {
                 return sprintf('%1$s <= %3$s OR (%1$s IS NULL AND %2$s IS NOT NULL)', $db->quoteIdentifier($periods[0]), $db->quoteIdentifier($periods[1]), $dateuntil);
             }
             return;
         case '-':
             // within
             $periods = explode(' ', substr($period, 1));
             if ($datefrom && $dateuntil) {
                 return sprintf('%1$s >= %3$s AND %2$s <= %4$s', $db->quoteIdentifier($periods[0]), $db->quoteIdentifier($periods[1]), $datefrom, $dateuntil);
             }
             if ($datefrom) {
                 return sprintf('%1$s >= %3$s AND (%2$s IS NULL OR %2$s >= %3$s)', $db->quoteIdentifier($periods[0]), $db->quoteIdentifier($periods[1]), $datefrom);
             }
             if ($dateuntil) {
                 return sprintf('%2$s <= %3$s AND (%1$s IS NULL OR %1$s <= %3$s)', $db->quoteIdentifier($periods[0]), $db->quoteIdentifier($periods[1]), $dateuntil);
             }
             return;
         default:
             if ($datefrom && $dateuntil) {
                 return sprintf('%s BETWEEN %s AND %s', $db->quoteIdentifier($period), $datefrom, $dateuntil);
             }
             if ($datefrom) {
                 return sprintf('%s >= %s', $db->quoteIdentifier($period), $datefrom);
             }
             if ($dateuntil) {
                 return sprintf('%s <= %s', $db->quoteIdentifier($period), $dateuntil);
             }
             return;
     }
 }
Пример #9
0
 /**
  * A ModelAbstract->setOnLoad() function that takes care of transforming a
  * dateformat read from the database to a \Zend_Date format
  *
  * If empty or \Zend_Db_Expression (after save) it will return just the value
  * currently there are no checks for a valid date format.
  *
  * @see \MUtil_Model_ModelAbstract
  *
  * @param mixed $value The value being saved
  * @param boolean $isNew True when a new item is being saved
  * @param string $name The name of the current field
  * @param array $context Optional, the other values being saved
  * @param boolean $isPost True when passing on post data
  * @return \MUtil_Date|\Zend_Db_Expr|null
  */
 public function formatLoadDate($value, $isNew = false, $name = null, array $context = array(), $isPost = false)
 {
     static $formats;
     // If not empty or zend_db_expression and not already a zend date, we
     // transform to a \Zend_Date using the stored formats
     if (null === $value || $value instanceof \Zend_Date || $value instanceof \Zend_Db_Expr) {
         return $value;
     }
     if (!isset($formats[$name][$isPost])) {
         // Stored the used formats (as they are usually used often within a model)
         if ($isPost) {
             // Add possible custom format
             $formats[$name][$isPost][] = $this->_getKeyValue($name, 'dateFormat');
             // Add default format as a fallback
             $type = \MUtil_Date_Format::getDateTimeType($this->_getKeyValue($name, 'dateFormat'));
             $formats[$name][$isPost][] = \MUtil_Model_Bridge_FormBridge::getFixedOption($type, 'dateFormat');
         }
         $formats[$name][$isPost][] = $this->_getKeyValue($name, 'storageFormat');
     }
     return \MUtil_Date::ifDate($value, $formats[$name][$isPost]);
 }
Пример #10
0
 /**
  * Converting the field value when saving to a respondent track
  *
  * @param array $currentValue The current value
  * @param array $fieldData The other values loaded so far
  * @return mixed the new value
  */
 public function onFieldDataSave($currentValue, array $fieldData)
 {
     if (null === $currentValue || $currentValue instanceof \Zend_Db_Expr || \MUtil_String::startsWith($currentValue, 'current_', true)) {
         return $currentValue;
     }
     $saveFormat = $this->getStorageFormat();
     if ($currentValue instanceof \Zend_Date) {
         return $currentValue->toString($saveFormat);
     } else {
         $displayFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
         $returnDate = \MUtil_Date::format($currentValue, $saveFormat, $displayFormat);
         if (is_null($returnDate)) {
             if (\Zend_Date::isDate($currentValue, $saveFormat)) {
                 return $currentValue;
             }
         }
         return $returnDate;
     }
     return (string) $currentValue;
 }