Ejemplo n.º 1
0
 public function layout($type, $data = array())
 {
     $version = panel()->version();
     $base = panel()->urls()->index();
     $cssbase = panel()->urls()->css();
     $jsbase = panel()->urls()->js();
     $defaults = array('title' => panel()->site()->title() . ' | Panel', 'direction' => panel()->direction(), 'meta' => $this->snippet('meta'), 'css' => css($cssbase . '/panel.css?v=' . $version), 'js' => js($jsbase . '/dist/panel.min.js?v=' . $version), 'content' => '', 'bodyclass' => '');
     switch ($type) {
         case 'app':
             $defaults['topbar'] = '';
             $defaults['csrf'] = panel()->csrf();
             $defaults['formcss'] = css($cssbase . '/form.min.css?v=' . $version);
             $defaults['formjs'] = js($jsbase . '/dist/form.min.js?v=' . $version);
             $defaults['appjs'] = js($jsbase . '/dist/app.min.js?v=' . $version);
             // plugin stuff
             $defaults['pluginscss'] = css($base . '/plugins/css?v=' . $version);
             $defaults['pluginsjs'] = js($base . '/plugins/js?v=' . $version);
             break;
         case 'base':
             break;
     }
     $data = array_merge($defaults, $data);
     if (r::ajax() and $type == 'app') {
         $panel = panel();
         $user = $panel->site()->user();
         $response = array('user' => $user ? $user->username() : false, 'direction' => $panel->direction(), 'title' => $data['title'], 'content' => $data['topbar'] . $data['content']);
         return response::json($response);
     } else {
         return new Layout($type, $data);
     }
 }
Ejemplo n.º 2
0
 public function logout()
 {
     if ($user = panel()->user()) {
         $user->logout();
     }
     $this->redirect('login');
 }
Ejemplo n.º 3
0
 public static function combine($type, $files, $compress = false)
 {
     $root = panel::instance()->roots()->assets() . DS . $type;
     $cache = new Media($root . DS . 'panel.' . $type);
     $media = new Collection(array_map(function ($file) use($root) {
         return new Media($root . DS . str_replace('/', DS, $file));
     }, $files));
     // get the max modification date
     $modified = max($media->pluck('modified'));
     if (is_writable($root) and (!$cache->exists() or $cache->modified() < $modified)) {
         $cache->remove();
         $content = '';
         foreach ($media as $asset) {
             $content .= $asset->read() . PHP_EOL;
         }
         if ($compress) {
             $content = static::compress($content);
         }
         f::write($root . DS . 'panel.' . $type, $content);
     }
     if ($cache->exists()) {
         return $type(panel()->urls()->{$type}() . '/panel.' . $type . '?v=' . panel()->version());
     }
     return $type(array_map(function ($item) use($type) {
         return 'panel/assets/' . $type . '/' . $item;
     }, $files));
 }
Ejemplo n.º 4
0
 public function get()
 {
     $history = $this->user->__get('history');
     if (empty($history) or !is_array($history)) {
         return array();
     }
     $update = false;
     $result = array();
     foreach ($history as $item) {
         try {
             $result[] = panel()->page($item);
         } catch (Exception $e) {
             $update = true;
         }
     }
     if ($update) {
         $history = array_map(function ($item) {
             return $item->id();
         }, $result);
         try {
             $this->user->update(array('history' => $history));
         } catch (Exception $e) {
         }
     }
     return $result;
 }
Ejemplo n.º 5
0
 public function logout()
 {
     if ($user = panel()->site()->user()) {
         $user->logout();
     }
     go(panel()->urls()->login());
 }
Ejemplo n.º 6
0
 public function forUser($username, $fieldName, $fieldType, $path)
 {
     $user = panel()->user($username);
     $form = $user->form('user', function () {
     });
     return $this->route($user, $form, $fieldName, $fieldType, $path);
 }
Ejemplo n.º 7
0
 protected function form($user = null)
 {
     $mode = $user ? 'edit' : 'add';
     $fields = data::read(panel()->roots()->forms() . DS . 'user.' . $mode . '.php', 'yaml');
     $content = $user ? $user->data() : array();
     // add all languages
     $fields['language']['options'] = array();
     $fields['language']['default'] = kirby()->option('panel.language', 'en');
     foreach (panel()->languages() as $code => $lang) {
         $fields['language']['options'][$code] = $lang->title();
     }
     // add all roles
     $fields['role']['options'] = array();
     $fields['role']['default'] = site()->roles()->findDefault()->id();
     foreach (site()->roles() as $role) {
         $fields['role']['options'][$role->id()] = $role->name();
     }
     // make the role selector readonly when the user is not an admin
     if (!site()->user()->isAdmin()) {
         $fields['role']['readonly'] = true;
     }
     // make sure the password is never shown in the form
     unset($content['password']);
     return new Form($fields, $content);
 }
Ejemplo n.º 8
0
 public function add()
 {
     $self = $this;
     $model = $this->model();
     $structure = $this->structure($model);
     $modalsize = $this->field()->modalsize();
     $fieldsetName = get("fieldset");
     $fieldsetStructure = $this->fieldsetStructure($fieldsetName);
     if (!$fieldsetStructure) {
         return $this->modal('error', array('text' => 'No fieldset with name "' . $fieldsetName . '" found.'));
     }
     $form = $this->form('add', array($model, $fieldsetStructure), function ($form) use($model, $structure, $self, $fieldsetName) {
         $form->validate();
         if (!$form->isValid()) {
             return false;
         }
         $data = $form->serialize();
         $data["_fieldset"] = $fieldsetName;
         $structure->add($data);
         $self->notify(':)');
         $self->redirect($model);
     });
     $form->attr('action', panel()->urls()->current() . "?fieldset=" . get("fieldset"));
     return $this->modal('add', compact('form', 'modalsize'));
 }
Ejemplo n.º 9
0
 public function index()
 {
     if (site()->users()->count() > 0) {
         go(panel()->urls()->login());
     }
     if ($problems = installation::check()) {
         $content = view('installation/check', array('problems' => $problems));
     } else {
         $form = panel()->form('installation', array('language' => kirby()->option('panel.language', 'en')));
         $form->cancel = false;
         $form->save = l('installation.signup.button');
         $form->centered = true;
         foreach (panel()->languages() as $lang) {
             $form->fields()->get('language')->options[$lang->code()] = $lang->title();
         }
         $form->on('submit', function ($form) {
             try {
                 // fetch all the form data
                 $data = $form->serialize();
                 // make sure that the first user is an admin
                 $data['role'] = 'admin';
                 // try to create the new user
                 $user = panel()->site()->users()->create($data);
                 // store the new username for the login screen
                 s::set('username', $user->username());
                 // redirect to the login
                 go(panel()->urls()->login() . '/welcome');
             } catch (Exception $e) {
                 $form->alert($e->getMessage());
             }
         });
         $content = view('installation/signup', array('form' => $form));
     }
     return layout('installation', array('meta' => new Snippet('meta'), 'content' => $content));
 }
Ejemplo n.º 10
0
 public function add($id = '/')
 {
     $page = $this->page($id);
     $blueprint = blueprint::find($page);
     $templates = $blueprint->pages()->template();
     $options = array();
     $back = array('subpages' => purl('subpages/index/' . $page->id()), 'page' => purl($page, 'show'));
     $form = panel()->form('pages.add');
     $form->save = l('add');
     $form->back = a::get($back, get('to'));
     foreach ($templates as $template) {
         $options[$template->name()] = $template->title();
     }
     $select = form::field('select', array('name' => 'template', 'label' => l('pages.add.template.label'), 'options' => $options, 'required' => true));
     if ($templates->count() == 1) {
         $select->readonly = true;
         $select->value = $templates->first()->name();
     }
     $form->fields()->append('template', $select);
     if (api::maxPages($page, $blueprint->pages()->max())) {
         $form->fields = array('info' => form::field('info', array('label' => 'pages.add.error.max.headline', 'text' => 'pages.add.error.max.text')));
         $form->save = false;
         $form->centered = true;
     }
     return view('pages/add', array('page' => $page, 'form' => $form));
 }
Ejemplo n.º 11
0
 public function defaults()
 {
     $kirby = kirby();
     $root = panel()->roots()->widgets();
     foreach (dir::read($root) as $dir) {
         $kirby->registry->set('widget', $dir, $root . DS . $dir, true);
     }
 }
Ejemplo n.º 12
0
 public function auth()
 {
     try {
         $user = panel()->user();
     } catch (Exception $e) {
         $this->redirect('login');
     }
 }
Ejemplo n.º 13
0
 public function id()
 {
     $site = panel()->site();
     if ($site->multilang()) {
         return $site->language()->code() . '-' . sha1($this->model->id());
     } else {
         return sha1($this->model->id());
     }
 }
Ejemplo n.º 14
0
 public function input()
 {
     $input = parent::input();
     if ($this->autocomplete) {
         $input->attr('autocomplete', 'off');
         $input->data(array('field' => 'autocomplete', 'url' => panel()->urls()->api() . '/autocomplete/emails'));
     }
     return $input;
 }
Ejemplo n.º 15
0
 public function index($method)
 {
     try {
         $auto = new Kirby\Panel\Autocomplete(panel(), $method, get());
         $result = $auto->result();
     } catch (Exception $e) {
         $result = array();
     }
     return $this->json(array('data' => $result));
 }
Ejemplo n.º 16
0
 public function defaults()
 {
     $root = panel()->roots()->widgets();
     foreach (dir::read($root) as $dir) {
         // add missing widgets to the order array
         if (!array_key_exists($dir, $this->order)) {
             $this->order[$dir] = true;
         }
         $this->load($dir, $root . DS . $dir . DS . $dir . '.php');
     }
 }
Ejemplo n.º 17
0
 public function getMonthBoard($month, $year)
 {
     // Calendar language
     date_default_timezone_set('UTC');
     $l = panel()->language();
     setlocale(LC_ALL, $l . '_' . str::upper($l));
     //setlocale(LC_ALL, 'us_US');
     // Calendar stuff
     $cal = new Calendarboard\calendar();
     $currentMonth = $cal->month($year, $month);
     return tpl::load(__DIR__ . DS . 'template.php', array('currentMonth' => $currentMonth, 'get_day_route_url' => purl($this->model(), 'field/' . $this->field()->name . '/calendarboard/get-day/'), 'calendarboard_url' => $this->model(), 'year_folder' => '/year-' . $year));
 }
Ejemplo n.º 18
0
 public function index()
 {
     $site = site();
     $blueprint = blueprint::find($site);
     $fields = $blueprint->fields($site)->toArray();
     $content = $site->content()->toArray();
     $files = null;
     // create the files
     if ($blueprint->files()->max() !== 0 and $blueprint->files()->hide() == false) {
         $files = new Snippet('pages/sidebar/files', array('page' => $site, 'files' => api::files($site, $blueprint)));
     }
     return view('metatags/index', array('topbar' => new Snippet('pages/topbar', array('breadcrumb' => new Snippet('breadcrumb', array('items' => array(array('title' => l('metatags'), 'url' => purl('metatags/'))))), 'search' => purl('pages/search/'))), 'form' => new Form($fields, $content), 's' => $site, 'files' => $files, 'license' => panel()->license()));
 }
Ejemplo n.º 19
0
 /**
  * Field setup
  *
  * (1) Load language files
  *
  * @since 1.2.0
  *
  * @return \SelectorField
  */
 public function __construct()
 {
     /*
        (1) Load language files
     */
     $baseDir = __DIR__ . DS . self::LANG_DIR . DS;
     $lang = panel()->language();
     if (file_exists($baseDir . $lang . '.php')) {
         require $baseDir . $lang . '.php';
     } else {
         require $baseDir . 'en.php';
     }
 }
Ejemplo n.º 20
0
 public function delete()
 {
     if (!panel()->user()->isAdmin() and !$this->user->isCurrent()) {
         throw new Exception(l('users.avatar.delete.error.permission'));
     } else {
         if (!$this->exists()) {
             return true;
         }
     }
     if (!parent::delete()) {
         throw new Exception(l('users.avatar.delete.error'));
     }
     kirby()->trigger('panel.avatar.delete', $this);
 }
Ejemplo n.º 21
0
 public function input()
 {
     $input = parent::input();
     $input->addClass('input-with-tags');
     $input->data(array('field' => 'tags', 'lowercase' => $this->lower, 'separator' => $this->separator));
     if (isset($this->data)) {
         $input->data('url', html(json_encode($this->data), false));
     } else {
         if ($page = $this->page()) {
             $query = array('uri' => $page->id(), 'index' => $this->index(), 'field' => $this->name(), 'separator' => $this->separator());
             $input->data('url', panel()->urls()->api() . '/autocomplete/field?' . http_build_query($query));
         }
     }
     return $input;
 }
Ejemplo n.º 22
0
 public function __construct($view, $input)
 {
     $this->view = $view;
     if (is_object($input) and method_exists($input, 'topbar')) {
         $input->topbar($this);
     } else {
         $class = is_object($input) ? str_replace('model', '', strtolower(get_class($input))) : (string) $input;
         $file = panel()->roots()->topbars() . DS . str::lower($class) . '.php';
         if (file_exists($file)) {
             $callback = (require $file);
             $callback($this, $input);
         } else {
             throw new Exception(l('topbar.error.class.definition') . $class);
         }
     }
 }
Ejemplo n.º 23
0
 public function input()
 {
     $input = parent::input();
     $input->addClass('input-with-tags');
     $input->data(array('field' => 'tags', 'lowercase' => $this->lower ? 'true' : false, 'separator' => $this->separator));
     if (isset($this->data)) {
         $input->data('url', html(json_encode($this->data), false));
     } else {
         if ($page = $this->page()) {
             $field = empty($this->field) ? $this->name() : $this->field;
             $model = is_a($this->model, 'File') ? 'file' : 'page';
             $query = array('uri' => $page->id(), 'index' => $this->index(), 'field' => $field, 'yaml' => $this->parentField, 'model' => $model, 'separator' => $this->separator(), '_csrf' => panel()->csrf());
             $input->data('url', panel()->urls()->api() . '/autocomplete/field?' . http_build_query($query));
         }
     }
     return $input;
 }
Ejemplo n.º 24
0
 public function delete()
 {
     if (!panel()->user()->isAdmin() and !$this->user->isCurrent()) {
         throw new Exception(l('users.avatar.delete.error.permission'));
     } else {
         if (!$this->exists()) {
             return true;
         }
     }
     if (!parent::delete()) {
         throw new Exception(l('users.avatar.delete.error'));
     }
     // flush the cache in case if the user data is
     // used somewhere on the site (i.e. for profiles)
     kirby()->cache()->flush();
     kirby()->trigger('panel.avatar.delete', $this);
 }
Ejemplo n.º 25
0
 public function index()
 {
     $site = site();
     $widgets = array();
     $wroot = kirby()->roots()->widgets();
     $wdirs = dir::read($wroot);
     // fetch all top-level pages in the right order
     $blueprint = blueprint::find($site);
     $pages = api::subpages($site->children(), $blueprint);
     foreach ($wdirs as $dir) {
         $file = $wroot . DS . $dir . DS . $dir . '.php';
         if (file_exists($file)) {
             $widgets[$dir] = (require $file);
         }
     }
     return view('dashboard/index', array('topbar' => new Snippet('pages/topbar', array('breadcrumb' => new Snippet('breadcrumb'), 'search' => purl('pages/search/'))), 'history' => history::get(), 'site' => $site, 'pages' => $pages, 'addbutton' => !api::maxPages($site, $blueprint->pages()->max()), 'widgets' => $widgets, 'user' => site()->user(), 'license' => panel()->license()));
 }
Ejemplo n.º 26
0
 public function delete($username)
 {
     $user = $this->user($username);
     $self = $this;
     if (!panel()->user()->isAdmin() and !$user->isCurrent()) {
         return $this->modal('error', array('headline' => l('error'), 'text' => l('users.delete.error.rights'), 'back' => purl('users')));
     } else {
         $form = $user->form('delete', function ($form) use($user, $self) {
             try {
                 $user->delete();
                 $self->notify(':)');
                 $self->redirect('users');
             } catch (Exception $e) {
                 $form->alert($e->getMessage());
             }
         });
         return $this->modal('users/delete', compact('form'));
     }
 }
Ejemplo n.º 27
0
 public function data()
 {
     $pages = $this->cache->get('pages');
     $users = $this->cache->get('users');
     if (empty($pages)) {
         $pages = array();
         foreach (panel()->site()->index() as $page) {
             $pages[] = array('title' => (string) $page->title(), 'uri' => (string) $page->id());
         }
         $this->cache->set('pages', $pages);
     }
     if (empty($users)) {
         foreach (panel()->users() as $user) {
             $users[] = array('username' => $user->username(), 'email' => $user->email());
         }
         $this->cache->set('users', $users);
     }
     return compact('pages', 'users');
 }
Ejemplo n.º 28
0
 public function index()
 {
     $self = $this;
     $site = panel()->site();
     $sidebar = $site->sidebar();
     $form = $site->form('edit', function ($form) use($site, $self) {
         // validate all fields
         $form->validate();
         // stop at invalid fields
         if (!$form->isValid()) {
             return $self->alert(l('pages.show.error.form'));
         }
         try {
             $site->update($form->serialize());
             $self->notify(':)');
             return $self->redirect('options');
         } catch (Exception $e) {
             return $self->alert($e->getMessage());
         }
     });
     return $this->screen('options/index', $site, array('site' => $site, 'form' => $form, 'files' => $sidebar->files(), 'license' => panel()->license(), 'uploader' => $this->snippet('uploader', array('url' => $site->url('upload')))));
 }
Ejemplo n.º 29
0
function purl($obj = '/', $action = false)
{
    if (empty($obj) or is_string($obj)) {
        $base = panel()->urls()->index();
        return ($obj == '/' or empty($obj)) ? $base . '/' : rtrim($base . '/' . $obj, '/');
    } else {
        if (is_a($obj, 'Kirby\\Panel\\Models\\Site')) {
            return $obj->url(!$action ? 'edit' : $action);
        } else {
            if (is_a($obj, 'Kirby\\Panel\\Models\\Page')) {
                return $obj->url(!$action ? 'edit' : $action);
            } else {
                if (is_a($obj, 'Kirby\\Panel\\Models\\File')) {
                    return $obj->url(!$action ? 'edit' : $action);
                } else {
                    if (is_a($obj, 'Kirby\\Panel\\Models\\User')) {
                        return $obj->url(!$action ? 'edit' : $action);
                    }
                }
            }
        }
    }
}
Ejemplo n.º 30
0
 public function assets($type)
 {
     $output = [];
     $defaultRoot = panel()->roots()->fields();
     foreach (kirby()->get('field') as $name => $field) {
         $root = $field->root();
         $base = dirname($root);
         // only fetch assets for custom fields
         if ($base == $defaultRoot) {
             continue;
         }
         $classname = $field->class();
         if (!class_exists($classname)) {
             throw new Exception('The field class is missing for: ' . $classname);
         }
         if (!isset($classname::$assets) || !isset($classname::$assets[$type])) {
             continue;
         }
         foreach ($classname::$assets[$type] as $filename) {
             $output[] = f::read($field->root() . DS . 'assets' . DS . $type . DS . $filename);
         }
     }
     return implode(PHP_EOL . PHP_EOL, $output);
 }