/**
  * Get the adverstised stop date (does not include timestamp_max)
  *
  * Note: there is a priority order in the params to determine the stop date.
  *  -dstop
  *  -week + m
  *  -m
  *  -dstart + x days
  */
 function get_advertised_stop_date()
 {
     if ($this->getting_adv_stop_date) {
         // We would be entering an infinite loop, stop now:
         // We cannot determine a stop date, save an empty string (to differentiate from NULL)
         $this->advertised_stop_date = '';
         // Reset anti infinite loop:
         $this->getting_adv_stop_date = false;
         return $this->advertised_stop_date;
     }
     // Anti infinite loop:
     $this->getting_adv_stop_date = true;
     if (is_null($this->advertised_stop_date)) {
         // We haven't determined the stop date yet:
         if (!empty($this->filters['ymdhms_max'])) {
             // We have requested an end date (8 digits)
             $m = $this->filters['ymdhms_max'];
             $this->advertised_stop_date = mktime(0, 0, 0, substr($m, 4, 2), substr($m, 6, 2), substr($m, 0, 4));
         } elseif (!is_null($this->filters['week']) && !empty($this->filters['ymdhms'])) {
             // we want to restrict on a specific week
             $this->advertised_stop_date = get_start_date_for_week(substr($this->filters['ymdhms'], 0, 4), $this->filters['week'], locale_startofweek());
             $this->advertised_stop_date += 518400;
             // + 6 days
         } elseif (!empty($this->filters['ymdhms'])) {
             // We want to restrict on an interval:
             if (strlen($this->filters['ymdhms']) >= 8) {
                 // We have requested a day interval
                 $m = $this->filters['ymdhms'];
                 $this->advertised_stop_date = mktime(0, 0, 0, substr($m, 4, 2), substr($m, 6, 2), substr($m, 0, 4));
             } elseif (strlen($this->filters['ymdhms']) == 6) {
                 // We want to go to the end of the month:
                 $m = $this->filters['ymdhms'];
                 $this->advertised_stop_date = mktime(0, 0, 0, substr($m, 4, 2) + 1, 0, substr($m, 0, 4));
                 // 0th day of next mont = last day of month
             } elseif (strlen($this->filters['ymdhms']) == 4) {
                 // We want to go to the end of the year:
                 $m = $this->filters['ymdhms'];
                 $this->advertised_stop_date = mktime(0, 0, 0, 12, 31, substr($m, 0, 4));
             }
         } elseif ($this->filters['unit'] == 'days' && ($start_date = $this->get_advertised_start_date()) != '') {
             // We want to restrict on a specific number of days after the start date:
             $this->advertised_stop_date = $start_date + ($this->limit - 1) * 86400;
         } else {
             // We cannot determine a stop date, save an empty string (to differentiate from NULL)
             $this->advertised_stop_date = '';
         }
     }
     // Reset anti infinite loop:
     $this->getting_adv_stop_date = false;
     return $this->advertised_stop_date;
 }
Example #2
0
    /**
     * Restricts the datestart param to a specific date range.
     *
     * Start date gets restricted to minutes only (to make the query more
     * cachable).
     *
     * Priorities:
     *  -dstart and/or dstop
     *  -week + m
     *  -m
     * @todo  -dstart + x days
     * @see ItemList2::get_advertised_start_date()
     *
     * @param string YYYYMMDDHHMMSS (everything after YYYY is optional) or ''
     * @param integer week number or ''
     * @param string YYYYMMDDHHMMSS to start at, '' for first available
     * @param string YYYYMMDDHHMMSS to stop at
     * @param mixed Do not show posts before this timestamp, can be 'now'
     * @param mixed Do not show posts after this timestamp, can be 'now'
     */
    function where_datestart($m = '', $w = '', $dstart = '', $dstop = '', $timestamp_min = '', $timestamp_max = 'now')
    {
        global $time_difference, $DB;
        $this->m = $m;
        $this->w = $w;
        $this->dstart = $dstart;
        $this->dstop = $dstop;
        $this->timestamp_min = $timestamp_min;
        $this->timestamp_max = $timestamp_max;
        $start_is_set = false;
        $stop_is_set = false;
        // if a start date is specified in the querystring, crop anything before
        if (!empty($dstart)) {
            // Add trailing 0s: YYYYMMDDHHMMSS
            $dstart0 = $dstart . '00000000000000';
            // TODO: this is NOT correct, should be 0101 for month
            // Start date in MySQL format: seconds get omitted (rounded to lower to minute for caching purposes)
            $dstart_mysql = substr($dstart0, 0, 4) . '-' . substr($dstart0, 4, 2) . '-' . substr($dstart0, 6, 2) . ' ' . substr($dstart0, 8, 2) . ':' . substr($dstart0, 10, 2);
            $this->WHERE_and($this->dbprefix . 'datestart >= ' . $DB->quote($dstart_mysql) . '
													OR ( ' . $this->dbprefix . 'datedeadline IS NULL AND ' . $this->dbprefix . 'datestart >= ' . $DB->quote($dstart_mysql) . ' )');
            $start_is_set = true;
        }
        // if a stop date is specified in the querystring, crop anything before
        if (!empty($dstop)) {
            switch (strlen($dstop)) {
                case '4':
                    // We have only year, add one to year
                    $dstop_mysql = $dstop + 1 . '-01-01 00:00:00';
                    break;
                case '6':
                    // We have year month, add one to month
                    $dstop_mysql = date("Y-m-d H:i:s ", mktime(0, 0, 0, substr($dstop, 4, 2) + 1, 01, substr($dstop, 0, 4)));
                    break;
                case '8':
                    // We have year mounth day, add one to day
                    $dstop_mysql = date("Y-m-d H:i:s ", mktime(0, 0, 0, substr($dstop, 4, 2), substr($dstop, 6, 2) + 1, substr($dstop, 0, 4)));
                    break;
                case '10':
                    // We have year mounth day hour, add one to hour
                    $dstop_mysql = date("Y-m-d H:i:s ", mktime(substr($dstop, 8, 2) + 1, 0, 0, substr($dstop, 4, 2), substr($dstop, 6, 2), substr($dstop, 0, 4)));
                    break;
                case '12':
                    // We have year mounth day hour minute, add one to minute
                    $dstop_mysql = date("Y-m-d H:i:s ", mktime(substr($dstop, 8, 2), substr($dstop, 8, 2) + 1, 0, substr($dstop, 4, 2), substr($dstop, 6, 2), substr($dstop, 0, 4)));
                    break;
                default:
                    // add one to second
                    // Stop date in MySQL format: seconds get omitted (rounded to lower to minute for caching purposes)
                    $dstop_mysql = substr($dstop, 0, 4) . '-' . substr($dstop, 4, 2) . '-' . substr($dstop, 6, 2) . ' ' . substr($dstop, 8, 2) . ':' . substr($dstop, 10, 2);
            }
            $this->WHERE_and($this->dbprefix . 'datestart < ' . $DB->quote($dstop_mysql));
            // NOT <= comparator because we compare to the superior stop date
            $stop_is_set = true;
        }
        if (!$start_is_set || !$stop_is_set) {
            if (!is_null($w) && strlen($m) == 4) {
                // If a week number is specified (with a year)
                // Note: we use PHP to calculate week boundaries in order to handle weeks
                // that overlap 2 years properly, even when start on week is monday (which MYSQL won't handle properly)
                $start_date_for_week = get_start_date_for_week($m, $w, locale_startofweek());
                $this->WHERE_and($this->dbprefix . "datestart >= '" . date('Y-m-d', $start_date_for_week) . "'");
                $this->WHERE_and($this->dbprefix . "datestart < '" . date('Y-m-d', $start_date_for_week + 604800) . "'");
                // + 7 days
                $start_is_set = true;
                $stop_is_set = true;
            } elseif (!empty($m)) {
                // We want to restrict on an interval:
                $this->WHERE_and('EXTRACT(YEAR FROM ' . $this->dbprefix . 'datestart)=' . intval(substr($m, 0, 4)));
                if (strlen($m) > 5) {
                    $this->WHERE_and('EXTRACT(MONTH FROM ' . $this->dbprefix . 'datestart)=' . intval(substr($m, 4, 2)));
                }
                if (strlen($m) > 7) {
                    $this->WHERE_and('EXTRACT(DAY FROM ' . $this->dbprefix . 'datestart)=' . intval(substr($m, 6, 2)));
                }
                if (strlen($m) > 9) {
                    $this->WHERE_and('EXTRACT(HOUR FROM ' . $this->dbprefix . 'datestart)=' . intval(substr($m, 8, 2)));
                }
                if (strlen($m) > 11) {
                    $this->WHERE_and('EXTRACT(MINUTE FROM ' . $this->dbprefix . 'datestart)=' . intval(substr($m, 10, 2)));
                }
                if (strlen($m) > 13) {
                    $this->WHERE_and('EXTRACT(SECOND FROM ' . $this->dbprefix . 'datestart)=' . intval(substr($m, 12, 2)));
                }
                $start_is_set = true;
                $stop_is_set = true;
            }
        }
        // TODO: start + x days
        // TODO: stop - x days
        // SILENT limits!
        // Timestamp limits:
        if ($timestamp_min == 'now') {
            // echo 'hide past';
            $timestamp_min = time();
        }
        if (!empty($timestamp_min)) {
            // Hide posts before
            // echo 'hide before '.$timestamp_min;
            $date_min = remove_seconds($timestamp_min + $time_difference);
            $this->WHERE_and($this->dbprefix . 'datestart >= ' . $DB->quote($date_min));
        }
        if ($timestamp_max == 'now') {
            // echo 'hide future';
            $timestamp_max = time();
        }
        if (!empty($timestamp_max)) {
            // Hide posts after
            // echo 'after';
            $date_max = remove_seconds($timestamp_max + $time_difference);
            $this->WHERE_and($this->dbprefix . 'datestart <= ' . $DB->quote($date_max));
        }
    }