예제 #1
0
 public static function get_by_slug($slug = false)
 {
     if ($seo = \Page\Model_Seo::find_one_by_slug($slug)) {
         return Model_Page::find_one_by_id($seo->content_id);
     }
     return false;
 }
예제 #2
0
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         throw new \HttpNotFoundException();
     }
     // Get accordion item to edit
     if (!($item = Model_Accordion::find_one_by_id($id))) {
         throw new \HttpNotFoundException();
     }
     // Accordion from home page?
     if ($item->parent_id == 1) {
         \Config::load('page::accordion_banner', 'details', true, true);
     }
     \View::set_global('title', 'Edit Accordion');
     if (\Input::post()) {
         $val = Model_Accordion::validate('update');
         // Upload image and display errors if there are any
         $image = $this->upload_image();
         if (!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images)) {
             // No previous images and image is not selected and it is required
             \Messages::error('<strong>There was an error while trying to upload accordion image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload accordion image</strong>');
             foreach ($image['errors'] as $error) {
                 \Messages::error($error);
             }
         }
         if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images))) {
             /** IMAGES **/
             // Get all alt texts to update if there is no image change
             foreach (\Arr::filter_prefixed(\Input::post(), 'alt_text_') as $image_id => $alt_text) {
                 if (strpos($image_id, 'new_') === false) {
                     $item_images[$image_id] = array('id' => $image_id, 'data' => array('alt_text' => \Input::post('alt_text_' . $image_id, '')));
                 }
             }
             // Save images if new files are submitted
             if (isset($this->_image_data)) {
                 foreach ($this->_image_data as $image_data) {
                     $cover_count = count($item->images);
                     if (strpos($image_data['field'], 'new_') === false) {
                         // Update existing image
                         if (str_replace('image_', '', $image_data['field']) != 0) {
                             $image_id = (int) str_replace('image_', '', $image_data['field']);
                             $cover_count--;
                             $item_images[$image_id] = array('id' => $image_id, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, '')));
                             $this->delete_image(\Input::post('image_db_' . $image_id, ''));
                         }
                     } else {
                         // Save new image
                         $image_tmp = str_replace('image_new_', '', $image_data['field']);
                         $item_images[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1));
                     }
                 }
             }
             Model_Accordion::bind_images($item_images);
             /** END OF IMAGES **/
             // Get POST values
             $insert = \Input::post();
             // Prepare some values
             $insert['active_from'] = !empty($insert['active_from']) ? strtotime($insert['active_from']) : NULL;
             $insert['active_to'] = !empty($insert['active_to']) ? strtotime($insert['active_to']) : NULL;
             if ($insert['status'] != 2) {
                 unset($insert['active_from']);
                 unset($insert['active_to']);
             }
             $item->set($insert);
             try {
                 $item->save();
                 \Messages::success('Accordion successfully updated.');
                 \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/page/accordion/list/' . $item->parent_id) : \Uri::admin('current'));
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to update accordion</strong>');
                 // Uncomment lines below to show database errors
                 //$errors = $e->getMessage();
                 //\Messages::error($errors);
             }
         } else {
             // Delete uploaded images if there is page saving error
             if (isset($this->_image_data)) {
                 foreach ($this->_image_data as $image_data) {
                     $this->delete_image($image_data['saved_as']);
                 }
             }
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to update accordion</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     $accordion = Model_Accordion::find_one_by_id($id);
     $page = Model_Page::find_one_by_id($accordion->parent_id);
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('page', $page)->set('accordion', $accordion);
 }
예제 #3
0
 public function action_delete($id = false)
 {
     if (is_numeric($id)) {
         // Get news item to edit
         if ($item = Model_Page::find_one_by_id($id)) {
             if (empty($item->children)) {
                 // Delete other content data like images, files, etc.
                 if (!empty($item->images)) {
                     foreach ($item->images as $image) {
                         $this->delete_image($image->image);
                         $image->delete();
                     }
                 }
                 if (!empty($item->files)) {
                     foreach ($item->files as $file) {
                         $this->delete_file($file->file);
                         $file->delete();
                     }
                 }
                 if (!empty($item->videos)) {
                     foreach ($item->videos as $video) {
                         $this->delete_image($video->thumbnail, 'video');
                         $video->delete();
                     }
                 }
                 if (!empty($item->accordions)) {
                     foreach ($item->accordions as $accordion) {
                         \Request::forge('admin/page/accordion/delete/' . $accordion->id)->execute();
                     }
                 }
                 // Delete item
                 try {
                     $item->seo->delete();
                     $item->delete();
                     \Messages::success('Page successfully deleted.');
                 } catch (\Database_Exception $e) {
                     // show validation errors
                     \Messages::error('<strong>There was an error while trying to delete page</strong>');
                     // Uncomment lines below to show database errors
                     //$errors = $e->getMessage();
                     //\Messages::error($errors);
                 }
             } else {
                 \Messages::error('This page has subpages. In order to delete this page first delete all subpages.');
             }
         }
     }
     \Response::redirect(\Input::referrer());
 }