public function save($u = false)
 {
     if ($u) {
         return parent::save($u);
     }
     if ($this->loaded) {
         $p = parent::save($u);
         $model = ORM::factory($this->object_plural . '_position')->where($this->object_name . '_id', $this->id)->find();
         $model->{$this->object_name . '_id'} = $p->id;
         $model->save(true);
         return $p;
     } else {
         $p = parent::save($u);
         $type = Input::instance()->post('form_type');
         $types = array('insert_as_last_child', 'insert_as_first_child', 'insert_as_next_sibling', 'insert_as_prev_sibling');
         if (!in_array($type, $types)) {
             throw new Exception("Insert type not set", 1);
         }
         $parent = Input::instance()->post('form_parent');
         if (!is_numeric($parent)) {
             throw new Exception("Parent id not set", 1);
         }
         $model = ORM::factory($this->object_plural . '_position');
         $model->{$this->object_name . '_id'} = $this->id;
         $model->{$this->object_name . '_pid'} = $this->pid;
         $model->version = $this->version;
         $model->{$type}($parent);
         return $p;
     }
 }
 public function find_all($limit = NULL, $offset = NULL)
 {
     if (Kohana::config('versions.version') == 'max') {
         $w = new Database_Expression('(SELECT max(version) FROM ' . $this->table_name . ' AS q WHERE ' . $this->table_name . '.pid=q.pid)');
     } else {
         $v = versions_helper::get_published_version();
         $w = new Database_Expression('(SELECT max(version) FROM ' . $this->table_name . ' AS q WHERE ' . $this->table_name . '.pid=q.pid AND version <= ' . $v . ')');
     }
     $this->db->join('page_content_positions', 'page_content_positions.page_content_pid', 'page_contents.pid');
     $this->db->where('version', $w);
     $this->db->where('state!=', 'D');
     $this->db_applied['orderby'] = true;
     $this->db->orderby('page_content_positions.order', 'ASC');
     return parent::find_all($limit, $offset);
 }
 public function __construct($id = null)
 {
     parent::__construct($id);
     $this->primary_key = 'pid';
 }