/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get video parent $item->get_parent = static::lazy_load(function () use($item) { return Model_Application::find_one_by_id($item->content_id); }, $item->id, 'parent', 'object'); // Get video details $item->get_youtube = static::lazy_load(function () use($item) { $object = new \stdClass(); $youtube = \App\Youtube::forge(); $object->data = $youtube->parse($item->url)->get(); $object->embed = $youtube->embed($item->url); return $object; }, $item->id, 'youtube', 'object'); } } } // return the result return $result; }
public static function get_by_slug($slug = false) { if ($seo = \Application\Model_Seo::find_one_by_slug($slug)) { return Model_Application::find_one_by_id($seo->content_id); } return false; }
/** * Delete content image * * @param $content_id = Content ID */ public function action_delete_image($content_id = null) { try { if ($application = Model_Application::find_one_by_id($content_id)) { if (!empty($application->hotspot) && !empty($application->hotspot->images)) { foreach ($application->hotspot->images as $image) { $this->delete_image($image->image); $image->delete(); } } $this->delete_image($application->hotspot_image); $application->hotspot_alt_text = null; $application->hotspot_image = null; if ($application->save()) { \Messages::success('Hotspot image was successfully deleted.'); } else { \Messages::error('There was an error while trying to delete hotspot image.'); } } } catch (\Database_Exception $e) { // show validation errors \Messages::error('There was an error while trying to delete hotspot image.'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); } \Response::redirect(\Input::referrer()); }
public function action_update($id = false) { if (!is_numeric($id)) { throw new \HttpNotFoundException(); } // Get news item to edit if (!($item = Model_Accordion::find_one_by_id($id))) { throw new \HttpNotFoundException(); } \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/application/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 application 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); $application = Model_Application::find_one_by_id($accordion->parent_id); \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('application', $application)->set('accordion', $accordion); }
public function action_delete($id = false) { if (is_numeric($id)) { // Get news item to edit if ($item = Model_Application::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->hotspot) && !empty($item->hotspot->images)) { foreach ($item->hotspot->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/application/accordion/delete/' . $accordion->id)->execute(); } } // Delete item try { $item->seo->delete(); $item->delete(); \Messages::success('Application successfully deleted.'); } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to delete application</strong>'); // Uncomment lines below to show database errors //$errors = $e->getMessage(); //\Messages::error($errors); } } else { \Messages::error('This application has case studies. In order to delete this application first delete all case studies.'); } } } \Response::redirect(\Input::referrer(\Uri::create('admin/application/list'))); }
/** * Manage related products * * @param $id = Product ID */ public function action_list($id) { if (!is_numeric($id)) { \Response::redirect('admin/application/list'); } // Get news item to edit if (!($item = Model_Application::find_one_by_id($id))) { \Response::redirect('admin/application/list'); } if (\Input::post()) { $add = \Input::post('products.add', array()); $remove = \Input::post('products.remove', array()); if (\Input::post('add', false)) { foreach ($add as $value) { $related = Model_Application_To_Related::forge(array('related_id' => $value, 'application_id' => $item->id)); $related->save(); } \Messages::success('Related products successfully added.'); } else { if (\Input::post('remove', false)) { foreach ($remove as $value) { $related = Model_Application_To_Related::find_one_by(array(array('related_id', '=', $value), array('application_id', '=', $item->id))); if (!is_null($related)) { $related->delete(); } } \Messages::success('Related products successfully removed.'); } } if (\Input::is_ajax()) { echo \Messages::display('left', false); exit; } else { \Response::redirect(\Input::referrer(\Uri::create('admin/application/list'))); } } \View::set_global('title', 'List Related Products'); $search = $this->get_search_items($item); $pagination = $search['pagination']; $status = $search['status']; $item = $search['item']; \Theme::instance()->set_partial('content', $this->view_dir . 'list')->set('application', $item)->set('pagination', $pagination, false)->set('status', $status); }