예제 #1
0
 function wikiparse()
 {
     $this->load->helper('input_wikitext');
     $_GET['input_wikitext_preview_field'] = 'parse';
     $wiki = new InputWikitextInterface('parse');
     $wiki->SetWikiparser();
     // should have exited by now
     show_404();
 }
 function __construct($category_id = null, $crossword_id = null, $office = false, $allow_add = true)
 {
     $this->category_id = $category_id;
     $this->crossword_id = $crossword_id;
     $this->office = $office;
     $ci =& get_instance();
     if ($office) {
         $ci->load->helper('input');
         $ci->load->helper('input_wikitext');
         $categories = $ci->crosswords_model->GetTipCategories();
         $category_options = array();
         foreach ($categories as &$category) {
             $category_options[$category['id']] = $category['name'];
         }
         // Allow adding of new tips to specific crosswords
         if (null != $crossword_id && $allow_add) {
             // Can't add if there aren't any categories!
             if (!empty($category_options)) {
                 $this->add_form = new InputInterfaces();
                 $new_tip = array('category_id' => $this->category_id === null ? $categories[0]['id'] : (int) $this->category_id, 'crossword_id' => $this->crossword_id, 'content_wikitext' => '', 'content_xhtml' => '');
                 if (null === $this->category_id) {
                     // Tip category
                     $category_interface = new InputSelectInterface('new_tip_category', $new_tip['category_id']);
                     $category_interface->SetOptions($category_options);
                     $this->add_form->Add('Tip category', $category_interface);
                 } else {
                     $category_interface = null;
                 }
                 // Wikitext
                 $content_interface = new InputWikitextInterface('new_tip_content', $new_tip['content_wikitext']);
                 $content_interface->SetRequired(true);
                 $content_interface->SetWikiparser();
                 $this->add_form->Add('Content (wikitext)', $content_interface);
                 $num_errors = $this->add_form->Validate();
                 if (0 == $num_errors && $this->add_form->Updated()) {
                     if (null === $this->category_id) {
                         $new_tip['category_id'] = (int) $category_interface->Value();
                     }
                     $new_tip['content_wikitext'] = $content_interface->Value();
                     $new_tip['content_xhtml'] = $content_interface->ValueXhtml();
                     if (null !== $ci->crosswords_model->AddTip($new_tip)) {
                         $ci->messages->AddMessage('success', 'Tip added');
                         $content_interface->Reset();
                     } else {
                         $ci->messages->AddMessage('error', 'Tip could not be added');
                     }
                 }
             } else {
                 $ci->messages->AddMessage('information', 'There are no crossword tip categories. You will need to <a href="/office/crosswords/tips/add?ret=' . urlencode($ci->uri->uri_string()) . '">create one</a> before you can add any tips for this crossword.');
             }
         }
     }
     $this->tips = $ci->crosswords_model->GetTips($category_id, $crossword_id, null, $office ? null : true);
     if ($this->office) {
         foreach ($this->tips as $index => &$tip) {
             $form = new InputInterfaces();
             $tip['edit_form'] =& $form;
             $name = 'tip_' . $tip['id'] . '_';
             $inputs = array();
             $tip['inputs'] =& $inputs;
             // Delete tip
             $inputs['delete'] = new InputCheckboxInterface($name . 'delete', false);
             $form->Add('Delete tip', $inputs['delete']);
             // Tip category
             $inputs['category'] = new InputSelectInterface($name . 'category', $tip['category_id']);
             $inputs['category']->SetOptions($category_options);
             $form->Add('Tip category', $inputs['category']);
             // Wikitext
             $inputs['content'] = new InputWikitextInterface($name . 'content', $tip['content_wikitext']);
             $inputs['content']->SetRequired(true);
             $inputs['content']->SetWikiparser();
             $form->Add('Content (wikitext)', $inputs['content']);
             // Delete it?
             if ($inputs['delete']->Value()) {
                 $success = $ci->crosswords_model->DeleteTipById($tip['id']);
                 if ($success) {
                     unset($this->tips[$index]);
                     $ci->messages->AddMessage('success', 'Tip deleted');
                 } else {
                     $ci->messages->AddMessage('error', 'Tip could not be deleted');
                 }
             } else {
                 $num_errors = $form->Validate();
                 if (0 == $num_errors && $form->Changed()) {
                     $changes = $form->ChangedValues();
                     $values = array();
                     if (isset($changes[$name . 'category'])) {
                         $values['category_id'] = $changes[$name . 'category'];
                     }
                     if (isset($changes[$name . 'content'])) {
                         $values['content_wikitext'] = $changes[$name . 'content'];
                         $values['content_xhtml'] = $inputs['content']->ValueXhtml();
                     }
                     $values['id'] = $tip['id'];
                     if (!$ci->crosswords_model->UpdateTip($values)) {
                         $ci->messages->AddMessage('error', 'Tip could not be saved');
                     } else {
                         $ci->messages->AddMessage('success', 'Tip saved successfully');
                         foreach ($values as $id => $value) {
                             $tip[$id] = $value;
                         }
                         if (isset($values['category_id'])) {
                             $tip['category_name'] = $category_options[$values['category_id']];
                             // Remove if no longer matching criteria
                             if (null !== $this->category_id && $this->category_id != $values['category_id']) {
                                 unset($this->tips[$index]);
                             }
                         }
                         $form->ResetDefaults();
                     }
                 }
             }
             unset($form);
             unset($inputs);
         }
     }
 }