Example #1
0
 public function __construct($id = 0, $data = null)
 {
     global $USER;
     if (is_array($id) && isset($id['urlid']) && isset($id['ownerurlid'])) {
         $tempdata = get_record_sql('
             SELECT v.*
             FROM {view} v JOIN {usr} u ON v.owner = u.id
             WHERE v.urlid = ? AND u.urlid = ?', array($id['urlid'], $id['ownerurlid']));
         if (empty($tempdata)) {
             throw new ViewNotFoundException(get_string('viewnotfoundbyname', 'error', $id['urlid'], $id['ownerurlid']));
         }
     } else {
         if (is_array($id) && isset($id['urlid']) && isset($id['groupurlid'])) {
             $tempdata = get_record_sql('
             SELECT v.*
             FROM {view} v JOIN {group} g ON v.group = g.id
             WHERE v.urlid = ? AND g.urlid = ? AND g.deleted = 0', array($id['urlid'], $id['groupurlid']));
             if (empty($tempdata)) {
                 throw new ViewNotFoundException(get_string('viewnotfoundbyname', 'error', $id['urlid'], $id['groupurlid']));
             }
         } else {
             if (!empty($id) && is_numeric($id)) {
                 $tempdata = get_record_sql('
             SELECT v.*
             FROM {view} v LEFT JOIN {group} g ON v.group = g.id
             WHERE v.id = ? AND (v.group IS NULL OR g.deleted = 0)', array($id));
                 if (empty($tempdata)) {
                     throw new ViewNotFoundException(get_string('viewnotfound', 'error', $id));
                 }
             }
         }
     }
     if (isset($tempdata)) {
         if (!empty($data)) {
             $data = array_merge((array) $tempdata, $data);
         } else {
             $data = $tempdata;
             // use what the database has
         }
         $this->id = $tempdata->id;
     } else {
         $this->ctime = time();
         $this->mtime = time();
         $this->dirty = true;
     }
     $data = empty($data) ? array() : (array) $data;
     foreach ($data as $field => $value) {
         if (property_exists($this, $field)) {
             $this->{$field} = $value;
         }
     }
     if (empty(self::$layoutcolumns)) {
         self::$layoutcolumns = get_records_assoc('view_layout_columns', '', '', 'columns,id');
     }
     // Add in owner and group objects if we already happen to have them from view_search(), etc.
     if (isset($data['user']) && isset($data['user']->id) && $data['user']->id == $this->owner) {
         $this->ownerobj = $data['user'];
     } else {
         if (isset($data['groupdata']->id) && $data['groupdata']->id == $this->group) {
             $this->groupobj = $data['groupdata'];
         } else {
             if (!isset($data['user']) && !empty($this->owner) && $this->owner == $USER->get('id')) {
                 $this->ownerobj = $USER;
             }
         }
     }
     $this->atime = time();
     $this->rows = array();
     $this->columns = array();
     $this->dirtyrows = array();
     $this->dirtycolumns = array();
     // set only for existing views - _create provides default value
     if (empty($this->columnsperrow)) {
         $this->columnsperrow = get_records_assoc('view_rows_columns', 'view', $this->get('id'), 'row', 'row, columns');
     }
 }