Пример #1
0
 public function action_add_page()
 {
     // Get all tags associated with pages and images
     $this->xml_content_tags = $this->xml_content->appendChild($this->dom->createElement('tags'));
     $tags = array();
     foreach (Content_Page::get_tags() as $tag) {
         $tags[] = $tag;
     }
     foreach (Content_Image::get_tags() as $tag) {
         foreach ($tags as $tag_to_check) {
             if ($tag_to_check['name'] == $tag['name']) {
                 break 2;
             }
         }
         $tags[] = $tag;
     }
     foreach ($tags as $tag) {
         $tag_node = $this->xml_content_tags->appendChild($this->dom->createElement('tag', $tag['name']));
         $tag_node->setAttribute('id', $tag['id']);
     }
     if (count($_POST) && isset($_POST['URI']) && isset($_POST['name'])) {
         if ($_POST['URI'] == '') {
             $_POST['URI'] = $_POST['name'];
         }
         $_POST['URI'] = URL::title($_POST['URI'], '-', TRUE);
         $post = new Validation($_POST);
         $post->filter('trim');
         $post->rule('Valid::not_empty', 'name');
         $post->rule('Content_Page::page_name_available', 'name');
         $post->rule('Content_Page::page_URI_available', 'URI');
         $post_values = $post->as_array();
         if ($post->validate()) {
             $tags = array();
             foreach ($post_values['template_position'] as $nr => $template_position) {
                 if ($post_values['tag_id'][$nr] > 0) {
                     if (!isset($tags[$template_position])) {
                         $tags[$template_position] = array();
                     }
                     $tags[$template_position][] = $post_values['tag_id'][$nr];
                 }
             }
             $page_id = Content_Page::new_page($post_values['name'], $post_values['URI'], $tags);
             $this->add_message('Page "' . $post_values['name'] . '" added');
         } else {
             // Form errors detected!
             $this->add_error('Fix errors and try again');
             $this->add_form_errors($post->errors());
             // Fix template position data
             $tmp_node = $this->xml_content->appendChild($this->dom->createElement('tmp'));
             foreach ($post_values['template_position'] as $nr => $template_position) {
                 $template_field_node = $tmp_node->appendChild($this->dom->createElement('template_field'));
                 $template_field_node->setAttribute('id', $template_position);
                 if ($post_values['tag_id'][$nr] > 0) {
                     $tag_node = $template_field_node->appendChild($this->dom->createElement('tag'));
                     $tag_node->setAttribute('id', $post_values['tag_id'][$nr]);
                 }
             }
             unset($post_values['template_position'], $post_values['tag_id']);
             $this->set_formdata($post_values);
         }
     }
 }