/**
  * ajax filter function - takes the incoming string, matches against columns 
  * and outputs view of the matching data
  */
 public function filters()
 {
     $this->use_layout = false;
     $cat = new CmsCategory();
     if (strlen($fil = $_POST['filter']) < 1) {
         $this->all_categories = $cat->order("name ASC")->all();
     } else {
         $this->all_categories = $cat->filter("name LIKE '%{$fil}%'")->order("name ASC")->all();
     }
     $this->cat_partial = $this->render_partial("cat_list");
 }
 public function __construct()
 {
     $this->server = "http://" . $_SERVER['HTTP_HOST'];
     if (Request::param('warned') || Session::get('warned')) {
         Session::set('warned', 1);
         $this->warned = true;
     }
     if (substr_count($_SERVER['HTTP_USER_AGENT'], "MSIE") > 0) {
         $this->ie = true;
     }
     $cat = new CmsCategory();
     $this->all_categories = $cat->order('name ASC')->all();
 }
 /**
  * the editing function... lets you change all the bits associated with the content record
  * gets the record for the id passed (/admin/content/edit/ID)
  * finds associated images & categories
  * render the partials
  */
 public function edit()
 {
     $this->id = WaxUrl::get("id");
     if (!$this->id) {
         $this->id = $this->route_array[0];
     }
     if (($lang_id = Request::get("lang")) && !$this->languages[$lang_id]) {
         Session::add_message("That language isn't allowed on your system. Here's the {$this->languages[0]} version instead.");
         $this->redirect_to("/admin/" . $this->module_name . "/edit/{$this->id}");
     }
     $master = new $this->model_class($this->id);
     if ($master->status == 4) {
         $this->redirect_to("/admin/" . $this->module_name . "/edit/{$master->preview_master_id}");
     }
     //this isn't a master, jump to the right url
     if ($master->language) {
         $this->redirect_to("/admin/" . $this->module_name . "/edit/{$master->preview_master_id}?lang=" . $master->language);
     }
     if ($lang_id) {
         $master = $this->get_language_model($master, $lang_id);
     }
     $preview = new $this->model_class();
     //preview revision - create a copy of the content if needed or use the existing copy
     if ($master->status == 1 || $master->status == 6) {
         if (!($preview = $preview->filter(array("preview_master_id" => $master->primval, "status" => 4))->first())) {
             //if a preview entry doesn't exist create one
             foreach ($master->columns as $col => $params) {
                 if ($master->{$col}) {
                     $copy_attributes[$col] = $master->{$col};
                 }
             }
             $copy_attributes = array_diff_key($copy_attributes, array($this->model->primary_key => false));
             //take out ID
             $preview = new $this->model_class();
             $preview->status = 4;
             $preview->save();
             $preview->set_attributes($copy_attributes);
             $preview->status = 4;
             $preview->url = $master->url;
             $preview->master = $master->primval;
             $preview->save();
         }
         $this->model = $preview;
     } else {
         $this->model = $master;
     }
     if ($this->model->is_posted()) {
         if ($_POST['publish']) {
             if ($master->status != 1 && $master->status != 6) {
                 $master->set_attributes($_POST[$master->table]);
                 if ($master->status == 5) {
                     $master->status = 6;
                 } else {
                     $master->status = 1;
                 }
                 $master->save();
             } else {
                 $this->update_master($preview, $master);
                 if ($preview->primval) {
                     $preview->delete();
                 }
             }
             Session::add_message($this->display_name . " " . "Successfully Published");
             $this->redirect_to("/admin/{$this->module_name}/");
         } elseif ($_POST['close']) {
             //delete the preview if it has no changes from the master
             if ($preview->equals($master) && $preview->primval) {
                 $preview->delete();
             }
             $this->redirect_to(Session::get("list_refer"));
         } else {
             //save button is default post, as it's the least destructive thing to do
             if ($preview->primval && ($_POST[$this->model->table]['status'] == 0 || $_POST[$this->model->table]['status'] == 5)) {
                 $this->update_master($preview, $master);
                 if ($preview->primval) {
                     $preview->delete();
                 }
                 $this->save($master, "/admin/{$this->module_name}/edit/" . $master->id . "/");
             } else {
                 $this->save($this->model, "/admin/{$this->module_name}/edit/" . $master->id . "/");
             }
         }
     }
     //images
     if (!($this->attached_images = $this->model->images)) {
         $this->attached_images = array();
     }
     //categories assocaited
     if (!($this->attached_categories = $this->model->categories)) {
         $this->attached_categories = array();
     }
     $cat = new CmsCategory();
     //all categories
     if (!($this->all_categories = $cat->order("name ASC")->all())) {
         $this->all_categories = array();
     }
     $this->image_model = new WildfireFile();
     //partials
     $this->image_partial = $this->render_partial("page_images");
     $this->cat_partial = $this->render_partial("list_categories");
     $this->cat_list = $this->render_partial("cat_list");
     $this->category_partial = $this->render_partial("apply_categories");
     $files = new WildfireFile();
     $this->all_links = $files->find_all_files();
     $this->link_partial = $this->render_partial("apply_links");
     $this->extra_content_partial = $this->render_partial("extra_content");
     $this->flash_files = $files->flash_files();
     $this->video_partial = $this->render_partial("apply_video");
     $this->table_partial = $this->render_partial("wysi_tables");
     $this->form = $this->render_partial("form");
 }