public static function role_fail($page_id)
 {
     $role = CmsPage::find($page_id)->role_level;
     if (!is_null($role)) {
         if ($role > ROLE) {
             return true;
         }
     }
     return false;
 }
 public static function select_zone($page_id = '')
 {
     $zones = array(0 => LL('cms::form.select', CMSLANG));
     if (!empty($page_id)) {
         $rs = CmsPage::find($page_id);
         $layout = $rs->layout;
         if (!empty($layout)) {
             $zone_arr = Config::get('cms::theme.layout_' . $layout);
             foreach ($zone_arr as $key => $value) {
                 $zones[$key] = $value;
             }
         }
     }
     return $zones;
 }
 public function get_edit($id)
 {
     //LOAD JS LIBS
     Asset::container('footer')->add('form', 'bundles/cms/js/jquery.form.js', 'jquery');
     Asset::container('footer')->add('count', 'bundles/cms/js/jquery.charcount.js', 'jquery');
     Asset::container('footer')->add('elastic', 'bundles/cms/js/jquery.elastic.js', 'jquery');
     Asset::container('footer')->add('slug', 'bundles/cms/js/jquery.stringtoslug.js', 'jquery');
     Asset::container('footer')->add('ckcms', 'bundles/cms/js/ck.cms.js', 'jqadapter');
     //CKEDITOR
     if (IS('cms::settings.wysiwyg', 'ckeditor')) {
         Asset::container('footer')->add('ckeditor', 'bundles/cms/ckeditor/ckeditor.js', 'form');
         Asset::container('footer')->add('jqadapter', 'bundles/cms/ckeditor/adapters/jquery.js', 'form');
         Asset::container('footer')->add('ckcms', 'bundles/cms/js/ck.cms.js', 'jqadapter');
     }
     //MARKITUP
     if (IS('cms::settings.wysiwyg', 'markitup')) {
         Asset::container('footer')->add('markitup', 'bundles/cms/markitup/jquery.markitup.js', 'form');
         Asset::container('footer')->add('sethtml', 'bundles/cms/markitup/sets/html/set.js', 'markitup');
         Asset::container('footer')->add('ckcms', 'bundles/cms/js/ck.cms.js', 'jqadapter');
         Asset::container('header')->add('csshtml', 'bundles/cms/markitup/sets/html/style.css');
         Asset::container('header')->add('cssmarkitup', 'bundles/cms/markitup/skins/markitup/style.css');
     }
     //PLUPLOAD
     Asset::container('footer')->add('plupload', 'bundles/cms/js/plupload.js', 'jquery');
     Asset::container('footer')->add('plupload_html4', 'bundles/cms/js/plupload.html4.js', 'plupload');
     Asset::container('footer')->add('plupload_html5', 'bundles/cms/js/plupload.html5.js', 'plupload');
     //LOAD FANCYBOX LIBS
     Asset::container('header')->add('fancyboxcss', 'bundles/cms/css/fancybox.css', 'main');
     Asset::container('footer')->add('fancybox', 'bundles/cms/js/jquery.fancybox.js', 'jquery');
     //LOAD AUTOSUGGEST LIBS
     Asset::container('header')->add('autosuggestcss', 'bundles/cms/css/autosuggest.css', 'main');
     Asset::container('footer')->add('autosuggest', 'bundles/cms/js/jquery.autosuggest.js', 'jquery');
     //DATETIME PICKER
     Asset::container('header')->add('jqueryuicss', 'bundles/cms/css/jquery.ui.css', 'main');
     if (LANG !== 'en') {
         Asset::container('footer')->add('local', 'bundles/cms/js/i18n/jquery.ui.datepicker-' . LANG . '.js', 'jquery');
     }
     Asset::container('footer')->add('datepicker', 'bundles/cms/js/jquery.datepicker.js', 'local');
     Asset::container('footer')->add('timepicker', 'bundles/cms/js/jquery.timepicker.js', 'datepicker');
     //SORTING
     Asset::container('footer')->add('sortable', 'bundles/cms/js/jquery.sortable.js', 'jquery');
     Asset::container('footer')->add('serialize', 'bundles/cms/js/jquery.serializetree.js', 'sortable');
     Asset::container('footer')->add('pages', 'bundles/cms/js/sections/blogs_edit.js', 'cms');
     $this->layout->header_data = array('title' => LL('cms::title.blog_edit', CMSLANG));
     $this->layout->top_data = array('search' => false);
     if (!empty($id)) {
         //GET BLOG DATA
         $blog = CmsBlog::with(array('pages', 'blogrels'))->find($id);
         $pivot = DB::table('blogs_pages')->where_cmsblog_id($id)->where_is_default(1)->first();
         //FILES OF PAGE
         $files = CmsPage::find($pivot->cmspage_id)->files;
         if (!empty($blog)) {
             //GET EXTRA ID
             $extra_ids = Config::get('cms::settings.extra_id');
             //GET PAGE DATA
             $pagedata = CmsPage::where_lang($blog->lang)->where_parent_id(0)->where_extra_id(array_search('blogs', $extra_ids))->order_by('lang', 'asc')->order_by('is_home', 'desc')->order_by('order_id', 'asc')->get();
             $new_data = array();
             foreach ($pagedata as $obj) {
                 $new_data[$obj->id] = $obj;
                 $recursive = call_user_func_array('CmsPage::recursive_pages', array($obj->id));
                 $new_data = $new_data + $recursive;
             }
             //GET BLOG DATA
             $blogdata = CmsBlog::where_lang($blog->lang)->where('id', '<>', $id)->where_is_valid(1)->order_by('datetime_on', 'desc')->order_by('name', 'desc')->paginate(Config::get('cms::settings.pag'));
             if (empty($new_data)) {
                 $new_data = array();
             }
             $this->layout->content = View::make('cms::interface.pages.blog_new_edit')->with('role_fail', CmsRole::role_fail($pivot->cmspage_id))->with('title', LL('cms::title.blog_edit', CMSLANG))->with('blog_id', $id)->with('page_id', $pivot->cmspage_id)->with('blog_lang', $blog->lang)->with('blog_name', $blog->name)->with('blog_parent', CmsPage::select_page_slug($blog->lang, array_search('blogs', $extra_ids)))->with('blog_parent_selected', $pivot->cmspage_id)->with('blog_slug', substr($blog->slug, 1))->with('blog_parent_slug', CmsPage::get_page_slug($pivot->cmspage_id) . '/')->with('blog_zones', CmsElement::select_zone($pivot->cmspage_id))->with('blog_zone_selected', $blog->zone)->with('blog_is_valid', (bool) $blog->is_valid)->with('blog_date_on', $blog->get_datetime_on())->with('blog_date_off', $blog->get_datetime_off())->with('blog_title', $blog->title)->with('blog_preview', $blog->preview)->with('blog_text', $blog->text)->with('blog_keyw', $blog->keyw)->with('blog_descr', $blog->descr)->with('blog_tags', '')->with('files', $files)->with('pagedata', $new_data)->with('pagerels', $blog->pages)->with('blogdata', $blogdata)->with('blogrels', $blog->blogrels);
         }
     }
 }
    $page_lang = $parameters[1];
    $table = $parameters[2];
    if (isset($parameters[3])) {
        $attribute = $parameters[3];
    }
    $query = DB::table($table)->where($attribute, '=', $value)->where('lang', '=', $page_lang)->where('id', '<>', $page_id);
    return $query->count() == 0;
});
Validator::register('unique_element_page', function ($attribute, $value, $parameters) {
    //CHECK UNIQUENESS OF ELEMENT NAME IN A PAGE
    $page_id = $parameters[0];
    $element_id = $parameters[1];
    if (isset($parameters[2])) {
        $attribute = $parameters[2];
    }
    $elements = CmsPage::find($page_id)->elements()->where('cmselement_id', '<>', $element_id)->where($attribute, '=', $value)->count();
    return $elements == 0;
});
Validator::register('unique_file', function ($attribute, $value, $parameters) {
    //CHECK UNIQUENESS OF FILE NAME ON DISK
    $file_name = $parameters[0];
    $path = $parameters[1];
    // CHECK DB NAME
    $query = CmsFile::where_name($file_name);
    return !file_exists(path('public') . $path . $file_name) and $query->count() == 0;
});
Validator::register('unique_filename', function ($attribute, $value, $parameters) {
    //CHECK UNIQUENESS OF FILE NAME
    $ext = $parameters[0];
    if (isset($parameters[1])) {
        $attribute = $parameters[1];
 public function post_clone_element()
 {
     if (Input::has('page_id') and Input::has('element_id') and Input::has('newpage_id')) {
         $pid = Input::get('page_id');
         $nid = Input::get('newpage_id');
         $eid = Input::get('element_id');
         $now = date('Y-m-d H:i:s');
         if (Input::has('to_clone')) {
             //CREATE NEW ELEMENT
             //GET ELEMENT MODEL
             $element = CmsElement::find($eid);
             $new_element_attr = array('author_id' => AUTHORID, 'name' => $element->name, 'label' => $element->label, 'text' => $element->text, 'zone' => $element->zone, 'lang' => LANG, 'is_valid' => 0);
             $new_element = new CmsElement($new_element_attr);
             $page = CmsPage::find($nid);
             $page->elements()->insert($new_element);
         } else {
             //GET ELEMENT MODEL
             $element = CmsElement::find($eid);
             $clone_array = array('cmselement_id' => $eid, 'cmspage_id' => $nid, 'created_at' => $now, 'updated_at' => $now);
             DB::table('elements_pages')->insert($clone_array);
         }
         Notification::success(LL('cms::alert.clone_element_success', CMSLANG, array('element' => $element->name)), 1500);
         return Redirect::to_action('cms::page', array(LANG));
     } else {
         Notification::error(LL('cms::alert.clone_element_error', CMSLANG), 1500);
         return Redirect::to_action('cms::page', array(LANG));
     }
 }
 public function post_save_post()
 {
     $auth = Auth::check();
     if ($auth and is_numeric(AUTHORID)) {
         $input = Input::get();
         //GRAB DATA
         $blog = new CmsBlog();
         if (!empty($input['blog_id'])) {
             $blog = CmsBlog::find($input['blog_id']);
             //CHECK OWNERSHIP
             if (CmsRole::role_fail($input['page_id'])) {
                 $msg = array('noaccess' => LL('cms::ajax_resp.ownership_error', CMSLANG)->get());
                 return json_encode($msg);
             }
         }
         //VALIDATION CHECK
         $rules = array('blog_name' => 'required|between:2,90|unique_lang:' . $input['blog_id'] . ',' . $input['blog_lang'] . ',blogs,name', 'blog_parent' => 'not_in:0', 'blog_slug' => 'required|alpha_slug|unique_slug:blogs,' . $input['blog_lang'] . ',,' . $input['blog_id'], 'blog_date_on' => 'required|valid_datetime', 'blog_date_off' => 'valid_datetime', 'blog_zone' => 'not_in:0');
         $messages = array('required' => LL('cms::validation.required', CMSLANG)->get(), 'between' => LL('cms::validation.between.string', CMSLANG)->get(), 'unique_lang' => LL('cms::validation.unique', CMSLANG)->get(), 'unique_slug' => LL('cms::validation.unique_slug', CMSLANG)->get(), 'alpha_slug' => LL('cms::validation.alpha_slug', CMSLANG)->get(), 'valid_datetime' => LL('cms::validation.valid_datetime', CMSLANG)->get(), 'not_in' => LL('cms::validation.not_in', CMSLANG)->get());
         $validation = Validator::make($input, $rules, $messages);
         if ($validation->fails()) {
             return json_encode($validation->errors);
         }
         //VALIDATION OK
         $blog->author_id = AUTHORID;
         $blog->lang = $input['blog_lang'];
         $blog->name = $input['blog_name'];
         $slug = '/' . $input['blog_slug'];
         $parent_slug = empty($input['blog_parent_slug']) ? '/' : str_replace('//', '/', $input['blog_parent_slug']);
         $blog->slug = $slug;
         //SET PERMISSION LIKE PARENT PAGE
         $page = CmsPage::find($input['blog_parent']);
         $blog->role_id = $page->role_id;
         $blog->role_level = $page->role_level;
         $blog->text = PRETEXT($input['blog_text']);
         $blog->datetime_on = dateTime2Db($input['blog_date_on']);
         if (Input::has('blog_date_off')) {
             $blog->datetime_off = dateTime2Db($input['blog_date_off']);
         } else {
             $blog->datetime_off = dateTimeFuture(dateTime2Db($input['blog_date_on']), 'P2Y');
         }
         $blog->zone = $input['blog_zone'];
         $blog->is_valid = Input::has('is_valid') ? 1 : 0;
         $blog->save();
         $bid = $blog->id;
         $response = 'success';
         $msg = LL('cms::ajax_resp.blog_post_success', CMSLANG)->get();
         $backurl = $input['back_url'];
         $full_slug = str_replace('//', '/', $parent_slug . $slug);
         $pid = $input['blog_parent'];
         $now = date('Y-m-d H:i:s');
         //SYNC PIVOT TABLE
         //NEW BLOG
         if (!Input::has('blog_id')) {
             //INSERT TO PIVOT
             $blog->pages()->attach($pid, array('is_default' => 1));
             //UPDATE BLOG
         } else {
             //SET PAGE_ID WHERE IS_DEFAULT = 1
             DB::table('blogs_pages')->where_cmsblog_id($bid)->where_is_default(1)->update(array('cmspage_id' => $pid));
         }
     } else {
         $bid = null;
         $pid = null;
         $response = 'error';
         $msg = LL('cms::ajax_resp.blog_post_error', CMSLANG)->get();
         $backurl = '#';
         $full_slug = '';
     }
     $data = array('auth' => $auth, 'cls' => 'blog_id', 'id' => $bid, 'pageid' => $pid, 'full_slug' => $full_slug, 'response' => $response, 'message' => $msg, 'backurl' => $backurl);
     return json_encode($data);
 }
 public function post_save_menu()
 {
     $auth = Auth::check();
     if ($auth) {
         $input = Input::get();
         //GRAB DATA
         $menu = new CmsMenu();
         if (!empty($input['menu_id'])) {
             $menu = CmsMenu::find($input['menu_id']);
         }
         //VALIDATION CHECK
         $rules = array('menu_name' => 'required|alpha_dash|between:2,20|unique_lang:' . $input['menu_id'] . ',' . $input['menu_lang'] . ',menus,name');
         $messages = array('required' => LL('cms::validation.required', CMSLANG)->get(), 'between' => LL('cms::validation.between.string', CMSLANG)->get(), 'unique_lang' => LL('cms::validation.unique', CMSLANG)->get(), 'alpha_dash' => LL('cms::validation.alpha_dash', CMSLANG)->get());
         $validation = Validator::make($input, $rules, $messages);
         if ($validation->fails()) {
             return json_encode($validation->errors);
         }
         $menu->name = strtolower($input['menu_name']);
         $menu->lang = strtolower($input['menu_lang']);
         $menu->parent_start = $input['parent_start'];
         $menu->is_nested = Input::has('is_nested') ? 1 : 0;
         //DELETE CACHE
         if (CACHE) {
             Cache::forget('menu_' . strtolower($input['menu_name']) . '_' . LANG);
         }
         if (CACHE) {
             Cache::forget('menu_pages_' . strtolower($input['menu_name']) . '_' . LANG);
         }
         $menu->save();
         $mid = $menu->id;
         // Empty template
         $template = '';
         if (Input::get('page_id') !== '') {
             $pages = Input::get('page_id');
             if (is_array($pages)) {
                 foreach ($pages as $pid) {
                     $check = DB::table('menus_pages')->where_cmspage_id($pid)->where_cmsmenu_id($mid)->first();
                     if (empty($check)) {
                         $menu->pages()->attach($pid, array('order_id' => Config::get('cms::settings.order')));
                     }
                     // Template returned
                     $template .= '<li id="' . $mid . '_' . $pid . '">';
                     $template .= '<a class="btn" href="#">';
                     $template .= '<i class="icon-resize-vertical"></i>';
                     $template .= CmsPage::find($pid)->name;
                     $template .= '</a>';
                     $template .= '</li>';
                 }
                 //DELETE NOT IN
                 DB::table('menus_pages')->where_cmsmenu_id($mid)->where_not_in('cmspage_id', $pages)->delete();
             }
             $response = 'success';
             $msg = LL('cms::ajax_resp.menu_save_success', CMSLANG)->get();
             $backurl = $input['back_url'];
             // Inject container
             $inject = 'ul.sortable';
             $detach = true;
         } else {
             //DELETE ALL MENU_ID
             DB::table('menus_pages')->where_cmsmenu_id($mid)->delete();
             $response = 'success';
             $msg = LL('cms::ajax_resp.menu_save_success', CMSLANG)->get();
             $backurl = $input['back_url'];
             $template = '';
             $inject = '';
             $detach = true;
         }
     } else {
         $response = 'error';
         $msg = LL('cms::ajax_resp.menu_save_error', CMSLANG)->get();
         $backurl = '#';
         $template = '';
         $inject = '';
         $detach = true;
     }
     $data = array('auth' => $auth, 'cls' => 'menu_id', 'id' => $mid, 'response' => $response, 'message' => $msg, 'backurl' => $backurl, 'detach' => $detach, 'inject' => $inject, 'template' => $template);
     return json_encode($data);
 }
 /**
  * PREVIEW Marker - Shows a list of contents from source
  *
  * [$PREVIEW[{
  *	"source":"<source label>",	=> (available: blogs, products...)
  *	"time":"<time label>",		=> (available: future, past, present - default: present)
  *	"n":"<n items per page>",	=> (default: config.design.pag)
  *	"id":"<id>",				=> OPTIONAL <ul> id
  *	"class":"<class>",			=> OPTIONAL <ul><li> class (default: list)
  *	"tpl":"<tpl_name>"			=> OPTIONAL (in /partials/markers)
  * }]]
  *
  * @param  array
  * @return string
  */
 public static function PREVIEW($vars = array())
 {
     //Get variables from array $vars
     if (!empty($vars)) {
         extract($vars);
     }
     //Bind variables
     $_source = '';
     if (isset($source) and !empty($source)) {
         $_source = $source;
     }
     $_time = 'present';
     if (isset($time) and !empty($time)) {
         $_time = $time;
     }
     $_n = Config::get('cms::theme.site_pag');
     if (isset($n) and !empty($n)) {
         $_n = $n;
     }
     $_id = 'preview';
     if (isset($id) and !empty($id)) {
         $_id = $id;
     }
     $_class = 'list';
     if (isset($class) and !empty($class)) {
         $_class = $class;
     }
     $_tpl = 'preview';
     if (isset($tpl) and !empty($tpl)) {
         $_tpl = $tpl;
     }
     if (!empty($_source)) {
         //SET MODEL RELATION
         $_source = $_source . '_preview';
         if ($_time != 'present') {
             $_source = $_source . '_' . $_time;
         }
         //CACHE DATA
         if (CACHE) {
             $page = Cache::remember('page_' . Str::slug(SLUG_LAST, '_') . '_' . SITE_LANG, function () use($_source) {
                 return CmsPage::where_lang(SITE_LANG)->where_slug(SLUG_FULL)->first();
             }, 5);
         } else {
             $page = CmsPage::where_lang(SITE_LANG)->where_slug(SLUG_FULL)->first();
         }
         if (!empty($page)) {
             //CACHE DATA
             if (CACHE) {
                 $list = Cache::remember($_source . '_' . $page->id . '_' . Input::get('page', 1), function () use($_source, $page, $_n) {
                     return CmsPage::find($page->id)->{$_source}()->paginate($_n);
                 }, 5);
             } else {
                 $list = CmsPage::find($page->id)->{$_source}()->paginate($_n);
             }
             $ul_options = array('id' => $_id);
             $li_options = array('class' => $_class);
             $view = LOAD_VIEW($_tpl);
             $view['page'] = $page;
             $view['ul_options'] = HTML::attributes($ul_options);
             $view['li_options'] = HTML::attributes($li_options);
             if (!empty($list)) {
                 $view['list'] = $list;
             } else {
                 $view['list'] = array();
             }
             return $view;
         }
     }
 }
 public function post_order_subpage()
 {
     $order = Input::get('order');
     if (is_array($order)) {
         //SET 1000
         CmsPage::where_order_id(0)->update(array('order_id' => 1000000));
         foreach ($order as $order_id => $item) {
             $order_id++;
             $p = explode("_", $item);
             $page = CmsPage::find($p[1]);
             $page->order_id = $order_id;
             $page->save();
         }
     }
     return true;
 }