Beispiel #1
0
 /**
  * Check if a row is owned by a user
  *
  * Abstracts the //owned by// operation. It works properly also on
  * missing info and fully supports the "ownership_expiration" property.
  *
  * @param  array   &$row  The subject row
  * @param  int|null $user A user id or null to use the logged user
  * @return bool           true if $row is owned by $user, false if it is
  *                        not owned, null on errors
  * @internal
  */
 private function _isOwner(&$row, $user = null)
 {
     if (!array_key_exists($this->owner_field, $row)) {
         // No owner field found: error
         return null;
     }
     isset($user) || ($user = TIP::getUserId());
     if ($row[$this->owner_field] != $user) {
         // Owner and user do not match: return "not owned row" condition
         return false;
     }
     if ($this->privilege == TIP_PRIVILEGE_MANAGER || $this->privilege == TIP_PRIVILEGE_ADMIN || is_null($this->ownership_expiration)) {
         // Ownership does not expire: return "owned row" condition
         return true;
     }
     if (!array_key_exists($this->creation_field, $row)) {
         // Ownership expiration set but creation field not found: error
         return null;
     }
     // Check if the ownership expired
     $creation = TIP::getTimestamp($row[$this->creation_field], 'sql');
     if (is_null($creation)) {
         // TIP::getTimestamp() failed: error
         return null;
     }
     $expiration = strtotime($this->ownership_expiration, $creation);
     if ($expiration === false) {
         // strtotime() failed (wrong expiration format?): error
         return null;
     }
     // The row is owned only if now is before the expiration time
     return time() < $expiration;
 }
Beispiel #2
0
 /**
  * Date/time formatter
  *
  * Converts a date, specified in $input_format format, in the $format
  * format and returns the result. If $input is not defined, the current
  * time is used as input.
  *
  * The $format parameter can be one of the following values:
  * - 'date' for a string with a date description (current locale)
  * - 'date_sql' for date in common SQL format ('%Y-%m-%d')
  * - 'date_iso8601' same as 'date_sql'
  * - 'datetime' for a string with date and time description (current locale)
  * - 'datetime_sql' for a string with a time description as '%Y-%m-%d %H:%M:%S'
  * - 'datetime_iso8601' for a string with time description in ISO 8601 format
  * - 'datetime_rfc3339' for a string as described in RFC3339 (atom format)
  *
  * Any other value will be passed directly to strftime().
  *
  * The $input_format parameter can be one of the following values:
  * - 'timestamp' for UNIX timestamps
  * - 'sql' for common SQL date/datetime
  *
  * @param  string     $format       The format of the resulting date
  * @param  mixed      $input        The source date to format
  * @param  string     $input_format The format of the source date
  * @return mixed|null               The formatted date or null on errors
  */
 public static function formatDate($format, $input = null, $input_format = 'timestamp')
 {
     if (is_null($input)) {
         $timestamp = time();
     } elseif ($input_format == 'timestamp') {
         $timestamp = $input;
     } else {
         $timestamp = TIP::getTimestamp($input, $input_format);
     }
     return empty($timestamp) ? null : TIP::_formatTimestamp($timestamp, $format);
 }
Beispiel #3
0
 private function &_widgetDate(&$field, $args)
 {
     HTML_QuickForm::registerRule('date', 'callback', '_ruleDate', 'TIP_Form');
     $id = $field['id'];
     $label = $this->getLocale('label.' . $id);
     $info = TIP::getLocale('comment.' . $id, $this->locale_prefix);
     // Set the date in a format suitable for HTML_QuickForm_date
     $sql_date = @$this->_defaults[$id];
     $timestamp = empty($sql_date) ? time() : TIP::getTimestamp($sql_date, 'sql');
     $this->_defaults[$id] = $timestamp;
     $field_year = date('Y', $this->_defaults[$id]);
     $this_year = date('Y');
     // $min_year > $max_year, so the year list is properly sorted in reversed
     // order
     $options = array('language' => substr(TIP::getLocaleId(), 0, 2), 'format' => 'dFY', 'minYear' => $this_year + 1, 'maxYear' => $field_year < $this_year - 5 ? $field_year : $this_year - 5);
     ++$this->_tabindex;
     $element =& $this->_form->addElement('date', $id, $label, $options, array('tabindex' => $this->_tabindex));
     $element->setInfo($info);
     $this->_addRule($id, 'date');
     $this->_addConverter($id, 'SqlDate');
     return $element;
 }
Beispiel #4
0
 /**
  * 'on_row' callback for TIP_Data_View
  *
  * Adds the following calculated fields to every data row:
  * - 'EXPIRED': true if the row has expired
  *
  * @param  array &$row The row as generated by TIP_Data_View
  * @return bool        always true
  */
 public function _onDataRow(&$row)
 {
     $row['EXPIRED'] = TIP::getTimestamp($row[$this->expiration_field], 'sql') < time();
     return parent::_onDataRow($row);
 }