Exemplo n.º 1
0
 public function action_new()
 {
     $snippet = Kohanut_Element::factory('snippet');
     $this->view->title = "Editing Snippet";
     $this->view->body = new View('kohanut/snippets/new', array('snippet' => $snippet, 'errors' => false));
     if ($_POST) {
         $snippet->set($_POST);
         // Make sure there are no twig syntax errors
         if ($snippet->twig) {
             try {
                 $test = Kohanut_Twig::render($_POST['code']);
             } catch (Twig_SyntaxError $e) {
                 $e->setFilename('code');
                 $this->view->body->errors[] = "There was a Twig Syntax error: " . $e->getMessage();
                 return;
             }
         }
         // Try to save
         try {
             $snippet->save();
             $this->request->redirect(Route::get('kohanut-admin')->uri(array('controller' => 'snippets')));
         } catch (Validate_Exception $e) {
             $this->view->body->errors = $e->array->errors('snippet');
         }
     }
 }
Exemplo n.º 2
0
 /** overload values to fix checkboxes
  *
  * @param array values
  * @return $this
  */
 public function values(array $values)
 {
     if ($this->loaded()) {
         $new = array('twig' => 0, 'markdown' => 0);
         return parent::values(array_merge($new, $values));
     } else {
         return parent::values($values);
     }
 }
Exemplo n.º 3
0
 public function action_delete($id)
 {
     // Sanitize
     $id = (int) $id;
     // Find the snippet
     $snippet = Kohanut_Element::factory('snippet')->values(array('id' => $id))->load();
     if (!$snippet->loaded()) {
         return $this->admin_error("Could not find snippet with id <strong>{$id}</strong>.");
     }
     $errors = false;
     if ($_POST) {
         try {
             $snippet->delete();
             $this->request->redirect(Route::get('kohanut-admin')->uri(array('controller' => 'snippets')));
         } catch (Validate_Exception $e) {
             $errors = array('submit' => "Delete failed!");
         }
     }
     $this->view->title = "Delete Snippet";
     $this->view->body = View::factory('kohanut/snippets/delete', array('snippet' => $snippet));
     $this->view->body->snippet = $snippet;
     $this->view->body->errors = $errors;
 }
Exemplo n.º 4
0
 public function set($values, $value = NULL)
 {
     if (!is_array($values)) {
         $values = array($values => $value);
     }
     if ($this->loaded()) {
         $new = array('twig' => 0, 'markdown' => 0);
         return parent::set(array_merge($new, $values));
     } else {
         return parent::set($values);
     }
 }
Exemplo n.º 5
0
 /**
  * Gives a form for confirming deleting of an element
  *
  * @param   int   Block id to delete
  * @return  void
  */
 public function action_delete($id)
 {
     // Sanitize
     $id = (int) $id;
     // Load the block
     $block = Sprig::factory('kohanut_block', array('id' => $id))->load();
     if (!$block->loaded()) {
         return $this->admin_error(__('Couldn\'t find block ID :id.', array(':id' => $id)));
     }
     // Load the type
     $type = $block->elementtype->load();
     if (!$type->loaded()) {
         return $this->admin_error(__('Elementtype :type could not be loaded.', array(':type' => (int) $block->elementtype->id)));
     }
     $class = Kohanut_Element::factory($type->name);
     $class->id = $block->element;
     $class->block = $block;
     $class->load();
     if (!$class->loaded()) {
         return $this->admin_error(__(':type with ID :id could not be found.', array(':type' => $type->name, ':id' => (int) $block->element)));
     }
     $this->view->title = __('Delete :element', array(':element' => __(ucfirst($type->name))));
     $this->view->body = $class->action_delete();
 }
Exemplo n.º 6
0
 /**
  * Return an element of a type, by name.
  *
  * The element must have a "name" field
  * Ex: element('snippet','footer')
  *
  * @param  string  The type of element
  * @param  name    The name of the element
  * @return string
  */
 public static function element($type, $name)
 {
     if (Kohana::$profiling === TRUE) {
         // Start a new benchmark
         $benchmark = Profiler::start('Kohanut', __FUNCTION__);
     }
     // Create an instance of the element
     try {
         $element = Kohanut_Element::factory($type);
         $element->name = $name;
         $element->load();
     } catch (Exception $e) {
         return "Could not render {$type} '{$name}' (" . $e->getMessage() . ")";
     }
     // If its loaded, render it, otherwise display an error.
     if ($element->loaded()) {
         $out = $element->render();
     } else {
         $out = "Could not render {$type} '{$name}', I could not find a {$type} with the name '{$name}'.";
     }
     if (isset($benchmark)) {
         // Stop the benchmark
         Profiler::stop($benchmark);
     }
     return $out;
 }