Example #1
0
/**
 * Presents browser with a custom 404 page.
 */
function behavior_page_not_found()
{
    $sql = 'SELECT * FROM ' . TABLE_PREFIX . "page WHERE behavior_id='page_not_found'";
    $stmt = Record::getConnection()->prepare($sql);
    $stmt->execute();
    $page = $stmt->fetchObject();
    if ($page) {
        $page = Page::find_page_by_uri($page->slug);
        if (is_object($page)) {
            header("HTTP/1.0 404 Not Found");
            header("Status: 404 Not Found");
            $page->_executeLayout();
            exit;
            // need to exit otherwise true error page will be sent
        }
    }
}
Example #2
0
 /**
  * Finds a Page record based on supplied arguments.
  *
  * Usage:
  *      $page = Page::find('/the/uri/to/your/page');
  *      $page = Page::find(array('where' => 'created_by_id=12'));
  *
  * Argument array can contain:
  *      - where
  *      - order
  *      - offset
  *      - limit
  *
  * Return values can be:
  *      - A single Page object
  *      - An array of Page objects which can be empty
  *      - False
  *
  * @param mixed $args   Uri string or array of arguments.
  * @return mixed        Page or array of Pages, otherwise false.
  */
 public static function find($args = null)
 {
     if (!is_array($args)) {
         // Assumes find was called with a uri
         return Page::find_page_by_uri($args);
     }
     // Collect attributes...
     $where = isset($args['where']) ? trim($args['where']) : '';
     $order_by = isset($args['order']) ? trim($args['order']) : '';
     $offset = isset($args['offset']) ? (int) $args['offset'] : 0;
     $limit = isset($args['limit']) ? (int) $args['limit'] : 0;
     // Prepare query parts
     $where_string = empty($where) ? '' : "WHERE {$where}";
     $order_by_string = empty($order_by) ? '' : "ORDER BY {$order_by}";
     $limit_string = $limit > 0 ? "LIMIT {$limit}" : '';
     $offset_string = $offset > 0 ? "OFFSET {$offset}" : '';
     $tablename = self::tableNameFromClassName('Page');
     $tablename_user = self::tableNameFromClassName('User');
     // Prepare SQL
     $sql = "SELECT page.*, creator.name AS created_by_name, updater.name AS updated_by_name FROM {$tablename} AS page" . " LEFT JOIN {$tablename_user} AS creator ON page.created_by_id = creator.id" . " LEFT JOIN {$tablename_user} AS updater ON page.updated_by_id = updater.id" . " {$where_string} {$order_by_string} {$limit_string} {$offset_string}";
     $stmt = self::$__CONN__->prepare($sql);
     if (!$stmt->execute()) {
         return false;
     }
     // Run!
     if ($limit == 1) {
         $object = $stmt->fetchObject('Page');
         if ($object !== false) {
             $object->part = self::get_parts($object->id);
         }
         return $object;
     } else {
         $objects = array();
         while ($object = $stmt->fetchObject('Page')) {
             $object->part = self::get_parts($object->id);
             $objects[] = $object;
         }
         return $objects;
     }
 }