Example #1
0
 public static function delete($path, $cascade = false)
 {
     $view = self::loadFromPath($path);
     if ($cascade == false) {
         foreach ($view->children() as $child) {
             $bits = preg_split('~\\/~', $child->path, -1, PREG_SPLIT_NO_EMPTY);
             unset($bits[count($bits) - 2]);
             View::move($child, trim(implode('/', $bits), '/'));
         }
     }
     General::rmdirr(VIEWS . '/' . trim($path, '/'));
 }
 public function __actionEdit()
 {
     if ($this->_context[0] != 'new' && strlen(trim($this->_context[1])) == 0) {
         redirect(ADMIN_URL . '/blueprints/views/');
     }
     $context = $this->_context;
     array_shift($context);
     $view_pathname = implode('/', $context);
     if (array_key_exists('delete', $_POST['action'])) {
         $this->__actionDelete(array($view_pathname), ADMIN_URL . '/blueprints/views/');
     } elseif (array_key_exists('save', $_POST['action'])) {
         $fields = $_POST['fields'];
         $fields['types'] = preg_split('/\\s*,\\s*/', $fields['types'], -1, PREG_SPLIT_NO_EMPTY);
         if (strlen(trim($fields['url-parameters'])) > 0) {
             $fields['url-parameters'] = preg_split('/\\/+/', trim($fields['url-parameters'], '/'), -1, PREG_SPLIT_NO_EMPTY);
         }
         if (strlen(trim($fields['handle'])) == 0) {
             $fields['handle'] = Lang::createHandle($fields['title']);
         }
         $path = trim($fields['parent'] . '/' . $fields['handle'], '/');
         if ($this->_context[0] == 'edit') {
             $view = self::__loadExistingView($view_pathname);
             $view->types = $fields['types'];
             $view->title = $fields['title'];
             $view->{'data-sources'} = $fields['data-sources'];
             $view->events = isset($fields['events']) ? $fields['events'] : array();
             $view->{'url-parameters'} = $fields['url-parameters'];
             // Path has changed - Need to move the existing one, then save it
             if ($view->path != $path) {
                 $this->errors = new MessageStack();
                 try {
                     // Before moving or renaming, simulate saving to check for potential errors
                     View::save($view, $this->errors, true);
                     View::move($view, $path);
                 } catch (Exception $e) {
                     // Saving failed, catch it further down
                 }
             }
         } else {
             $view = View::loadFromFieldsArray($fields);
             $view->template = file_get_contents(TEMPLATES . '/template.view.txt');
             $view->handle = $fields['handle'];
             $view->path = $path;
         }
         $this->errors = new MessageStack();
         try {
             View::save($view, $this->errors);
             redirect(ADMIN_URL . '/blueprints/views/edit/' . $view->path . '/:saved/');
         } catch (ViewException $e) {
             switch ($e->getCode()) {
                 case View::ERROR_MISSING_OR_INVALID_FIELDS:
                     $this->alerts()->append(__('An error occurred while processing this form. <a href="#error">See below for details.</a>'), AlertStack::ERROR, $this->errors);
                     break;
                 case View::ERROR_FAILED_TO_WRITE:
                     $this->alerts()->append($e->getMessage(), AlertStack::ERROR, $e);
                     break;
             }
         } catch (Exception $e) {
             // Errors!!
             // Not sure what happened!!
             $this->alerts()->append(__('An unknown error has occurred. <a class="more">Show trace information.</a>'), AlertStack::ERROR, $e);
         }
     }
 }