Exemplo n.º 1
0
 /**
  * Action content
  */
 public function action_content()
 {
     /**
      * Get item etc
      */
     // <editor-fold defaultstate="collapsed" desc="Get item etc">
     // Get division
     $division = Tbl::factory('divisions')->where('id', '=', $this->item->division_id)->read(1);
     // Direct set to division
     $this->item->division_segment = $division->segment;
     $this->item->division_name = $division->name;
     // Get content from file and direct set to $this->item
     $this->item->content = Tpl::get_file($this->item->segment, $this->settings->item_dir . '/' . $division->segment);
     // Save present segment
     $oldfile = $this->item->segment;
     // Get divisions
     $divisions = Tbl::factory('divisions')->read()->as_array();
     // Get shapes
     $shapes = Cms_Helper::get_dirfiles('shape', $this->settings->front_tpl_dir);
     // </editor-fold>
     /**
      * If update
      */
     // <editor-fold defaultstate="collapsed" desc="If update">
     if ($this->request->post('update')) {
         // Set post to division
         $this->item->shape_segment = $this->request->post('shape_segment');
         $this->item->content = $this->request->post('content');
         // Database transaction start
         Database::instance()->begin();
         // Try
         try {
             // Update
             Tbl::factory('items')->get($this->item->id)->update(array('shape_segment' => $this->request->post('shape_segment') ?: NULL));
             // New file
             $newfile = $this->item->segment;
             // rename file
             Cms_Helper::rename_file($oldfile, $newfile, $this->settings->item_dir . '/' . $division->segment);
             // Update file
             Cms_Helper::set_file($newfile, $this->settings->item_dir . '/' . $division->segment, $this->request->post('content'));
             // Database commit
             Database::instance()->commit();
             // Add success notice
             Notice::add(Notice::SUCCESS, Kohana::message('general', 'update_success'));
         } catch (HTTP_Exception_302 $e) {
             $this->redirect($e->location());
         } catch (Validation_Exception $e) {
             // Database rollback
             Database::instance()->rollback();
             // Add validation notice
             Notice::add(Notice::VALIDATION, Kohana::message('general', 'update_failed'), NULL, $e->errors('validation'));
         } catch (Exception $e) {
             // Database rollback
             Database::instance()->rollback();
             // Add error notice
             Notice::add(Notice::ERROR, $e->getMessage());
         }
     }
     // </editor-fold>
     /**
      * View
      */
     // <editor-fold defaultstate="collapsed" desc="View">
     $this->partials['local_menu'] = Tpl::get_file('local_menu', $this->settings->back_tpl_dir);
     $content_file = Tpl::get_file('content', $this->settings->back_tpl_dir . '/items', $this->partials);
     $this->content = Tpl::factory($content_file)->set('item', $this->item)->set('divisions', $divisions)->set('shapes', $shapes)->set('post', $this->request->post());
     // </editor-fold>
 }
Exemplo n.º 2
0
 /**
  * Action edit
  */
 public function action_edit()
 {
     // Get id from param, if there is nothing then throw to 404
     $segment = strtolower($this->request->param('key'));
     if (!$segment) {
         throw HTTP_Exception::factory(404);
     }
     // Make part and get content from file and direct set to part
     $part = new stdClass();
     $part->segment = strtolower($segment);
     $part->content = Tpl::get_file($segment, $this->settings->front_tpl_dir . '/part');
     // If there is nothing then throw to 404
     if ($part->content === FALSE) {
         throw HTTP_Exception::factory(404);
     }
     // Set delete url
     $part->delete_url = URL::site("{$this->settings->backend_name}/parts/delete/{$part->segment}", 'http');
     // Save present segment
     $oldname = $part->segment;
     // If there are post
     if ($this->request->post()) {
         // Try
         try {
             // Validation
             $validation = Validation::factory($this->request->post())->rule('segment', 'not_empty')->rule('segment', 'max_length', array(':value', '200'))->rule('segment', 'regex', array(':value', '/^[a-z0-9_]+$/'))->rule('segment', function (Validation $array, $field, $value, $present_segment) {
                 $segments = array_keys((array) Cms_Helper::get_dirfiles('part', $this->settings->front_tpl_dir));
                 if (in_array($value, $segments)) {
                     if ($value !== $present_segment) {
                         $array->error($field, 'uniquely');
                     }
                 }
             }, array(':validation', ':field', ':value', $part->segment))->label('segment', __('Segment'));
             // Check validation
             if (!$validation->check()) {
                 throw new Validation_Exception($validation);
             }
             // new name
             $newname = $this->request->post('segment');
             // Update file これが先じゃないとダメ
             Cms_Helper::set_file($oldname, "{$this->settings->front_tpl_dir}/part", $this->request->post('content'));
             // rename file
             Cms_Helper::rename_file($oldname, $newname, "{$this->settings->front_tpl_dir}/part");
             // Add success notice
             Notice::add(Notice::SUCCESS, Kohana::message('general', 'update_success'));
             // Redirect
             $this->redirect("{$this->settings->backend_name}/parts/edit/{$newname}");
         } catch (HTTP_Exception_302 $e) {
             $this->redirect($e->location());
         } catch (Validation_Exception $e) {
             // Add validation notice
             Notice::add(Notice::VALIDATION, Kohana::message('general', 'update_failed'), NULL, $e->errors('validation'));
         } catch (Exception $e) {
             // Add error notice
             Notice::add(Notice::ERROR);
         }
     }
     /**
      * View
      */
     $content_file = Tpl::get_file('edit', $this->settings->back_tpl_dir . '/parts', $this->partials);
     $this->content = Tpl::factory($content_file)->set('part', $part);
 }
Exemplo n.º 3
0
 /**
  * After
  */
 public function after()
 {
     // Auto render
     if ($this->auto_render) {
         /**
          * build snippets -> snippetsようにつくるようにつくる!
          */
         // <editor-fold defaultstate="collapsed" desc="build snippets">
         // Get site details
         $sites = array();
         $site_details = Tbl::factory('settings')->where('key', '=', 'site_details')->read('value');
         $site_detail_strings = explode("\n", $site_details);
         if ($site_detail_strings) {
             foreach ($site_detail_strings as $site_detail_string) {
                 $array = explode(':', $site_detail_string);
                 $sites[trim($array[0])] = array('key' => trim($array[0]), 'value' => trim($array[1]));
             }
         }
         // Get items for snippets item. 下の$this->snippetsのitemに入れるように取得する
         $snippet_item = NULL;
         if ($this->request->param('key')) {
             $snippet_item_segment = Tbl::factory('items')->where('id', '=', $this->request->param('key'))->read('segment');
             if ($snippet_item_segment) {
                 $snippet_item = Cms_Functions::get_item($snippet_item_segment, TRUE, TRUE, TRUE);
             }
         }
         // Get parts for snippets part. 下の$this->snippetsのpartsに入れるように取得する
         $snippet_parts = Cms_Helper::get_dirfiles('part', $this->settings->front_tpl_dir);
         foreach ($snippet_parts as $snippet_part) {
             $snippet_part->content = Tpl::get_file($snippet_part->segment, $this->settings->front_tpl_dir . '/part');
         }
         // Set snippets
         $this->snippets = array('host' => URL::base(true), 'media_dir' => URL::site('media', 'http') . '/', 'images_dir' => URL::site('media/images_dir', 'http') . '/', 'css_dir' => URL::site('media/css_dir', 'http') . '/', 'js_dir' => URL::site('media/js_dir', 'http') . '/', 'icon_dir' => URL::site('media/icon_dir', 'http') . '/', 'lang' => $this->settings->lang, 'logged_in_user' => array('id' => isset($this->logged_in_user->id) ? $this->logged_in_user->id : NULL, 'email' => isset($this->logged_in_user->email) ? $this->logged_in_user->email : NULL, 'username' => isset($this->logged_in_user->username) ? $this->logged_in_user->username : NULL, 'logins' => isset($this->logged_in_user->logins) ? $this->logged_in_user->logins : NULL, 'details' => isset($this->logged_in_user->details) ? $this->logged_in_user->details : NULL), 'sites' => $sites, 'timestamp' => time(), 'return' => 'PHP_EOL', 'item' => isset($snippet_item) ? (object) $snippet_item : NULL, 'parts' => isset($snippet_parts) ? (object) $snippet_parts : NULL);
         // </editor-fold>
         /**
          * View
          */
         // <editor-fold defaultstate="collapsed" desc="View">
         // Set global value -> Set to contentといっしょ
         Tpl::set_global(array('host' => URL::base(true), 'site_title' => $this->settings->site_title, 'site_email_address' => $this->settings->site_email_address, 'backend_host' => URL::base(true) . $this->settings->backend_name . '/', 'logged_in_user' => $this->logged_in_user, 'logout_url' => URL::site("{$this->settings->backend_name}/logout", 'http'), 'time' => time()));
         // Set to content
         $this->content->set('menus', $this->menus)->set('notice', Notice::render())->set('local_menus', $this->local_menus)->set('frontend_link', $this->frontend_link)->set('snippets', $this->snippets);
         // Get tamplate file
         $template = Tpl::get_file('template', $this->settings->back_tpl_dir);
         $backend_ucfirst = str_replace('_', ' ', Text::ucfirst($this->settings->backend_name, '_'));
         // Factory and set
         $this->template = Tpl::factory($template)->set('title', $backend_ucfirst)->set('keywords', $backend_ucfirst)->set('description', $backend_ucfirst)->set('content', $this->content->render());
         // Render body
         $this->response->body($this->template->render());
         // </editor-fold>
     }
     /**
      * after
      */
     parent::after();
 }