You are here

class MostPopularInterval in Drupal Most Popular 7

Same name and namespace in other branches
  1. 6 classes/intervals.php \MostPopularInterval

@file Defines a wrapper for the mostpopular_intervals table.

@author Andrew Marcus @since Dec 18, 2009

Hierarchy

Expanded class hierarchy of MostPopularInterval

File

classes/intervals.php, line 19
Defines a wrapper for the mostpopular_intervals table.

View source
class MostPopularInterval {
  public static $table = 'mostpopular_intervals';
  public $iid = 0;
  public $title = '';
  public $string = '';
  public $weight = 0;

  /**
   * Constructs a new interval.
   *
   * If a row object is passed as the parameter, the $iid, $title, $interval
   * and $weight are all extracted from the object.
   * Otherwise, the parameters are used for these values.
   *
   * @param array|object $object
   *   An object with settings for this interval.
   */
  public function MostPopularInterval($object) {
    $this
      ->update($object);
  }

  /**
   * Updates this service with the given values
   *
   * @param array|object $object
   *   An object containing the values to update.
   */
  public function update($object) {
    foreach ($object as $key => $val) {
      if (isset($this->{$key})) {
        $this->{$key} = $val;
      }
    }
  }

  /**
   * Creates a new, empty interval with the given weight.
   *
   * @param integer $weight
   *   The weight to assign this interval.
   *
   * @return MostPopularInterval
   */
  public static function create($weight) {
    return new MostPopularInterval(array(
      'weight' => $weight,
    ));
  }

  /**
   * Returns the timestamp, relative to the current time, that marks
   * the start of this interval.
   *
   * @return integer
   *   A timestamp in the past, or 0 if there is no interval string specified.
   */
  public function timestamp() {
    return !empty($this->string) ? strtotime($this->string) : 0;
  }

  /**
   * Returns the full title, which is the interval's title prepended with
   * 'Past '. So, for instance, 'Day' becomes 'Past Day'.
   *
   * @return string
   *   The full title of the interval.
   */
  public function fullTitle() {
    return 'Past ' . $this->title;
  }

  /**
   * Stores this interval into the database.
   */
  public function save() {
    if ($this->iid > 0) {
      drupal_write_record(self::$table, $this, array(
        'iid',
      ));
    }
    else {
      drupal_write_record(self::$table, $this);
      $this->iid = db_last_insert_id(self::$table, 'iid');
    }
  }

  /**
   * Removes this interval from the database.
   */
  public function remove() {
    if ($this->iid > 0) {
      $sql = 'DELETE FROM {' . self::$table . '} WHERE iid = %d';
      db_query($sql, $this->iid);
    }
  }

  /**
   * Removes all intervals from the database.
   */
  public static function clear() {
    db_query('TRUNCATE {' . self::$table . '}');
  }

  /**
   * Resets the list of intervals to the default settings.
   */
  public static function reset() {
    self::clear();
    $day = new MostPopularInterval(array(
      'title' => t('Day'),
      'string' => '-1 day',
      'weight' => 0,
    ));
    $day
      ->save();
    $week = new MostPopularInterval(array(
      'title' => t('Week'),
      'string' => '-1 week',
      'weight' => 1,
    ));
    $week
      ->save();
    $month = new MostPopularInterval(array(
      'title' => t('Month'),
      'string' => '-1 month',
      'weight' => 2,
    ));
    $month
      ->save();
    $year = new MostPopularInterval(array(
      'title' => t('Year'),
      'string' => '-1 year',
      'weight' => 3,
    ));
    $year
      ->save();
  }

  /**
   * Fetches all of the intervals from the database, ordered by weight.
   *
   * @return array<MostPopularInterval>
   *   A list of intervals, sorted by weight.
   */
  public static function fetchAll() {
    $intervals = array();
    $sql = 'SELECT * FROM {' . self::$table . '} ORDER BY weight ASC';
    $result = db_query($sql);
    while ($row = db_fetch_object($result)) {
      $intervals[] = new MostPopularInterval($row);
    }
    return $intervals;
  }

  /**
   * Fetches an interval object for the given interval ID.
   *
   * @param integer $iid
   *   The interval ID.
   *
   * @return MostPopularInterval
   *   The interval with the given ID, or null if none could be found.
   */
  public static function fetch($iid) {
    $sql = 'SELECT * FROM {' . self::$table . '} WHERE iid = %d';
    $result = db_query($sql, (int) $iid);
    if ($row = db_fetch_object($result)) {
      return new MostPopularInterval($row);
    }
    return NULL;
  }

  /**
   * Returns the default interval.
   *
   * @return MostPopularInterval
   *   The interval with the lowest weight.
   */
  public static function getDefault() {
    $sql = 'SELECT * FROM {' . self::$table . '} ORDER BY weight ASC';
    $result = db_query_range($sql, 0, 1);
    if ($row = db_fetch_object($result)) {
      return new MostPopularInterval($row);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MostPopularInterval::$iid public property
MostPopularInterval::$string public property
MostPopularInterval::$table public static property
MostPopularInterval::$title public property
MostPopularInterval::$weight public property
MostPopularInterval::clear public static function Removes all intervals from the database.
MostPopularInterval::create public static function Creates a new, empty interval with the given weight.
MostPopularInterval::fetch public static function Fetches an interval object for the given interval ID.
MostPopularInterval::fetchAll public static function Fetches all of the intervals from the database, ordered by weight.
MostPopularInterval::fullTitle public function Returns the full title, which is the interval's title prepended with 'Past '. So, for instance, 'Day' becomes 'Past Day'.
MostPopularInterval::getDefault public static function Returns the default interval.
MostPopularInterval::MostPopularInterval public function Constructs a new interval.
MostPopularInterval::remove public function Removes this interval from the database.
MostPopularInterval::reset public static function Resets the list of intervals to the default settings.
MostPopularInterval::save public function Stores this interval into the database.
MostPopularInterval::timestamp public function Returns the timestamp, relative to the current time, that marks the start of this interval.
MostPopularInterval::update public function Updates this service with the given values