Example #1
0
 function post()
 {
     if (!$this->dx_auth->is_logged_in()) {
         $this->dx_auth->deny("login");
     }
     $id = $this->input->post("id");
     $postObject = new Post();
     if ($id > 0) {
         $postObject->get_by_id($id);
     }
     // Author
     $postObject->author_id = $this->dx_auth->get_user_id();
     // Title & Text
     $postObject->title = trim($this->input->post("title", TRUE));
     $postObject->body = $this->input->post("body", TRUE);
     // Unique SEO Friendly URL
     if ($id == 0) {
         $base_url = url_title(strtolower($postObject->title));
         $url = $base_url;
         $counter = 2;
         do {
             $query = $this->db->where("url", $url)->from("posts")->get();
             if ($query->num_rows() > 0) {
                 $url = $base_url . "_" . $counter++;
             }
         } while ($query->num_rows() > 0);
         $postObject->url = $url;
     } else {
         $postObject->url = url_title($this->input->post("url"));
     }
     // Category
     if ($this->input->post("category_id") > 0) {
         $postObject->category_id = $this->input->post("category_id");
     } else {
         if ($this->input->post("category") != FALSE) {
             $new_category = ucwords(trim($this->input->post("category_id", TRUE)));
             $categoryObject = new Category();
             $categoryObject->get_by_name($new_category);
             if (!exists($categoryObject)) {
                 $categoryObject->name = $new_category;
                 $categoryObject->save();
             }
             $postObject->category_id = $categoryObject->id;
         }
     }
     // Status & Publishing Date
     if ($postObject->status < $this->_PUBLISHED && $this->input->post("status") == $this->_PUBLISHED) {
         $postObject->published = date("Y-m-d H:i:s", time());
     }
     $postObject->status = $this->input->post("status");
     // Save
     $postObject->save();
     // Return the post data
     redirect("chronicle/view/{$postObject->url}");
 }