public function action_edit_content()
 {
     $id = $this->request->param('options');
     $content = new Content_Content($id);
     if ($content->get_content_id()) {
         $this->xml_content->appendChild($this->dom->createElement('content_id', $id));
         if (count($_POST)) {
             $post = new Validation($_POST);
             $post->filter('trim');
             $post_values = $post->as_array();
             $tags = array();
             foreach ($post_values['tag'] as $nr => $tag_name) {
                 if ($tag_name) {
                     if (!isset($tags[$tag_name])) {
                         $tags[$tag_name] = array();
                     }
                     $tags[$tag_name][] = $post_values['tag_value'][$nr];
                 }
             }
             $content->update_content($post_values['content'], $tags);
             $this->add_message('Content #' . $id . ' updated');
         }
         $content_node = $this->xml_content->appendChild($this->dom->createElement('content'));
         $content_node->appendChild($this->dom->createTextNode($content->get_content()));
         $tags_node = $this->xml_content->appendChild($this->dom->createElement('tags'));
         foreach ($content->get_tags() as $tag) {
             if (!$tag['values']) {
                 $tag_node = $tags_node->appendChild($this->dom->createElement('tag'));
                 $tag_node->setAttribute('name', $tag['name']);
             } else {
                 foreach ($tag['values'] as $tag_value) {
                     $tag_node = $tags_node->appendChild($this->dom->createElement('tag', $tag_value));
                     $tag_node->setAttribute('name', $tag['name']);
                 }
             }
         }
     } else {
         $this->redirect();
     }
 }
 public function action_index()
 {
     $this->xslt_stylesheet = 'admin/galleries';
     xml::to_XML(array('admin_page' => 'Galleries'), $this->xml_meta);
     if (!empty($_POST)) {
         if (!empty($_POST['gallery'])) {
             foreach ($_POST['gallery'] as $counter => $gallery_URI) {
                 $contents = Content_Content::get_contents_by_tags(array('gallery' => $gallery_URI, 'type' => 'sort'));
                 if (count($contents)) {
                     list($sort_content_id) = array_keys($contents);
                     list($sort_content_content) = array_values($contents);
                     $content = new Content_Content($sort_content_id);
                     $content->update_content($counter);
                 } else {
                     Content_Content::new_content($counter, array('gallery' => array($gallery_URI), 'type' => array('sort')));
                 }
             }
             foreach ($_POST['category'] as $counter => $category_name) {
                 $contents = Content_Content::get_contents_by_tags(array('category' => $category_name, 'type' => 'sort'));
                 if (count($contents)) {
                     list($sort_content_id) = array_keys($contents);
                     list($sort_content_content) = array_values($contents);
                     $content = new Content_Content($sort_content_id);
                     $content->update_content($counter);
                 } else {
                     Content_Content::new_content($counter, array('category' => array($category_name), 'type' => array('sort')));
                 }
             }
         }
         if (!empty($_POST['new_gallery_name'])) {
             $this->redirect('admin/gallery?uri=' . url::title($_POST['new_gallery_name'], '-', TRUE));
         }
     }
     // Get all galleries
     $galleries = array();
     $gallery_names = array();
     $categories = array();
     foreach (Content_Image::get_tags() as $tag) {
         if ($tag['name'] == 'gallery') {
             foreach ($tag['values'] as $tag_value) {
                 if (!in_array($tag_value, $gallery_names)) {
                     $name = '';
                     $sort = 0;
                     $category = '';
                     // Get some content
                     $contents = Content_Content::get_contents_by_tags(array('gallery' => $tag_value));
                     foreach ($contents as $content) {
                         if ($content['tags']['type'][0] == 'name') {
                             $name = $content['content'];
                         } elseif ($content['tags']['type'][0] == 'sort') {
                             $sort = $content['content'];
                         } elseif ($content['tags']['type'][0] == 'category') {
                             $category = $content['content'];
                         }
                     }
                     if ($name == '') {
                         $name = $tag_value;
                     }
                     if (!in_array($category, $categories)) {
                         $categories[] = $category;
                     }
                     $galleries[] = array('URI' => $tag_value, 'name' => $name, '@sort' => $sort, 'category' => $category);
                 }
                 $gallery_names[] = $tag_value;
             }
         }
     }
     $this->xml_content_galleries = $this->xml_content->appendChild($this->dom->createElement('galleries'));
     xml::to_XML($galleries, $this->xml_content_galleries, 'gallery');
     foreach ($categories as $nr => $category_real_name) {
         if ($category_real_name != '') {
             $sort = 0;
             foreach (Content_Content::get_contents_by_tags(array('category' => $category_real_name, 'type' => 'sort')) as $content) {
                 $sort = $content['content'];
             }
             $categories[$nr . 'category'] = array('URI' => URL::title($category_real_name, '-', TRUE), 'name' => $category_real_name, 'sort' => $sort);
         }
         unset($categories[$nr]);
     }
     $this->xml_content_categories = $this->xml_content->appendChild($this->dom->createElement('categories'));
     xml::to_XML($categories, $this->xml_content_categories);
 }
 public function action_index()
 {
     if ($_GET['uri'] == 'all') {
         $this->redirect();
     }
     // Set all-gallery-settings and sort
     if (isset($_POST['show_in_all']) && isset($_POST['sort'])) {
         foreach ($_POST['sort'] as $order => $name) {
             $image = new Content_Image($name);
             $tags = array('sort' => array($order), 'gallery' => array($_GET['uri']));
             if (in_array($name, $_POST['show_in_all'])) {
                 $tags['gallery'][] = 'all';
             }
             $image->set_data($tags);
         }
     }
     // Remove an image
     if (isset($_GET['rm'])) {
         $image = new Content_Image($_GET['rm']);
         $image->rm_image();
         $this->redirect();
     }
     // Set gallery name
     if (isset($_POST['name'])) {
         $contents = Content_Content::get_contents_by_tags(array('gallery' => $_GET['uri'], 'type' => 'name'));
         if (count($contents)) {
             list($name_content_id) = array_keys($contents);
             list($name_content_content) = array_values($contents);
             $content = new Content_Content($name_content_id);
             $content->update_content($_POST['name']);
         } else {
             Content_Content::new_content($_POST['name'], array('gallery' => array($_GET['uri']), 'type' => array('name')));
         }
     }
     // Set gallery category
     if (isset($_POST['category'])) {
         $contents = Content_Content::get_contents_by_tags(array('gallery' => $_GET['uri'], 'type' => 'category'));
         if (count($contents)) {
             list($category_content_id) = array_keys($contents);
             list($category_content_content) = array_values($contents);
             $content = new Content_Content($category_content_id);
             $content->update_content($_POST['category']);
         } else {
             Content_Content::new_content($_POST['category'], array('gallery' => array($_GET['uri']), 'type' => array('category')));
         }
     }
     // Set gallery description
     if (isset($_POST['description'])) {
         $contents = Content_Content::get_contents_by_tags(array('gallery' => $_GET['uri'], 'type' => 'description'));
         if (count($contents)) {
             list($description_content_id) = array_keys($contents);
             list($description_content_content) = array_values($contents);
             $content = new Content_Content($description_content_id);
             $content->update_content($_POST['description']);
         } else {
             Content_Content::new_content($_POST['description'], array('gallery' => array($_GET['uri']), 'type' => array('description')));
         }
     }
     // Set show in menu
     if (isset($_POST['show_in_menu'])) {
         $contents = Content_Content::get_contents_by_tags(array('gallery' => $_GET['uri'], 'type' => 'show_in_menu'));
         if (count($contents)) {
             list($description_content_id) = array_keys($contents);
             list($description_content_content) = array_values($contents);
             $content = new Content_Content($description_content_id);
             $content->update_content('yes');
         } else {
             Content_Content::new_content('yes', array('gallery' => array($_GET['uri']), 'type' => array('show_in_menu')));
         }
     } elseif (!empty($_POST)) {
         $contents = Content_Content::get_contents_by_tags(array('gallery' => $_GET['uri'], 'type' => 'show_in_menu'));
         if (count($contents)) {
             list($description_content_id) = array_keys($contents);
             list($description_content_content) = array_values($contents);
             $content = new Content_Content($description_content_id);
             $content->update_content('no');
         } else {
             Content_Content::new_content('no', array('gallery' => array($_GET['uri']), 'type' => array('show_in_menu')));
         }
     }
     // Add image
     if (isset($_POST['upload_image'])) {
         foreach ($_FILES['image']['name'] as $nr => $name) {
             if ($name != '') {
                 $pathinfo = pathinfo($_FILES['image']['name'][$nr]);
                 if (strtolower($pathinfo['extension']) == 'jpg' || strtolower($pathinfo['extension']) == 'png') {
                     $filename = URL::title($_GET['uri'], '-', TRUE) . '.' . strtolower($pathinfo['extension']);
                     $new_filename = $filename;
                     $counter = 1;
                     while (!Content_Image::image_name_available($new_filename)) {
                         $new_filename = substr($filename, 0, strlen($filename) - 4) . '_' . $counter . '.' . strtolower($pathinfo['extension']);
                         $counter++;
                     }
                     if (move_uploaded_file($_FILES['image']['tmp_name'][$nr], APPPATH . '/user_content/images/' . $new_filename)) {
                         if (strtolower($pathinfo['extension']) == 'jpg') {
                             $gd_img_object = ImageCreateFromJpeg(Kohana::$config->load('user_content.dir') . '/images/' . $new_filename);
                         } elseif (strtolower($pathinfo['extension']) == 'png') {
                             $gd_img_object = ImageCreateFromPng(Kohana::$config->load('user_content.dir') . '/images/' . $new_filename);
                         }
                         $details = array('width' => array(imagesx($gd_img_object)), 'height' => array(imagesy($gd_img_object)), 'gallery' => array('all', URL::title($_GET['uri'], '-', TRUE)));
                         Content_Image::new_image($new_filename, $details);
                     } else {
                         $this->add_error('Unknown error uploading image(s)');
                     }
                 }
             }
         }
     }
     // Images
     $images = array();
     foreach (Content_image::get_images(NULL, array('gallery' => array($_GET['uri']))) as $image_name => $image_data) {
         $images[] = array('name' => $image_name, 'sort' => @$image_data['sort'][0], 'all' => strval(@in_array('all', $image_data['gallery'])));
     }
     $this->xml_content_images = $this->xml_content->appendChild($this->dom->createElement('images'));
     xml::to_XML($images, $this->xml_content_images, 'image');
     // Get some content
     $contents = Content_Content::get_contents_by_tags(array('gallery' => $_GET['uri']));
     foreach ($contents as $content) {
         if ($content['tags']['type'][0] == 'name') {
             xml::to_XML(array('gallery_real_name' => $content['content']), $this->xml_content);
         } elseif ($content['tags']['type'][0] == 'description') {
             xml::to_XML(array('gallery_description' => $content['content']), $this->xml_content);
         } elseif ($content['tags']['type'][0] == 'category') {
             xml::to_XML(array('gallery_category' => $content['content']), $this->xml_content);
         } elseif ($content['tags']['type'][0] == 'show_in_menu') {
             xml::to_XML(array('gallery_show_in_menu' => $content['content']), $this->xml_content);
         }
     }
 }