public function getAdd($parentPageId = 0, $groupId = 0)
 {
     $publishingOn = config('coaster::admin.publishing') > 0;
     $cabPublish = $publishingOn && Auth::action('pages.version-publish', ['page_id' => $parentPageId]) || !$publishingOn && Auth::action('pages.edit', ['page_id' => $parentPageId]);
     // set page data
     $page = new Page();
     if ($parentPageId && ($parent = Page::find($parentPageId))) {
         $page->parent = $parent->id;
         $page->template = $parent->child_template;
     } else {
         $page->parent = 0;
     }
     if ($groupId && ($group = PageGroup::find($groupId))) {
         $page->groups->add($group);
         $page->template = $group->default_template;
         $page->parent = $parentPageId ? $page->parent : -1;
     }
     $page->group_container = 0;
     $page->link = 0;
     $page->live = $cabPublish ? 1 : 0;
     $page->sitemap = 1;
     // get item name, or default to page
     $item_name = $page->groupItemsNames() ?: 'Page';
     // get page info tab contents
     $tab_headers = [];
     $tab_contents = [];
     list($tab_headers[0], $tab_contents[0]) = $page->tabInfo();
     $tab_data = ['headers' => View::make('coaster::partials.tabs.header', ['tabs' => $tab_headers])->render(), 'contents' => View::make('coaster::partials.tabs.content', ['tabs' => $tab_contents, 'item' => $item_name, 'new_page' => true, 'publishing' => $publishingOn, 'can_publish' => $cabPublish, 'page' => $page])->render()];
     $this->layoutData['content'] = View::make('coaster::pages.pages.add', ['page' => $page, 'item_name' => $item_name, 'tab' => $tab_data]);
 }
 public function postPages($role_id)
 {
     if (config('coaster::admin.advanced_permissions')) {
         $page_actions = AdminAction::where('controller_id', '=', 2)->where('inherit', '=', 0)->where('edit_based', '=', 0)->get();
         $actionIds = [];
         foreach ($page_actions as $action) {
             $actionIds[$action->action] = $action->id;
         }
         if (!config('coaster::admin.publishing')) {
             unset($actionIds['version-publish']);
         }
         $pages_permissions = Request::input('page');
         $this->_role_permissions = UserRole::find($role_id);
         // defaults
         $defaults = [];
         foreach ($actionIds as $action => $id) {
             $defaults[$id] = false;
         }
         foreach ($this->_role_permissions->actions as $action) {
             if (array_key_exists($action->id, $defaults)) {
                 $defaults[$action->id] = 1;
             }
         }
         // existing
         $existing = [];
         foreach ($this->_role_permissions->page_actions as $page_permission) {
             if (!isset($existing[$page_permission->pivot->page_id])) {
                 $existing[$page_permission->pivot->page_id] = [];
             }
             $existing[$page_permission->pivot->page_id][$page_permission->pivot->action_id] = $page_permission->pivot->access;
         }
         // save updates
         $pages = Page::where('parent', '>=', '0')->get();
         foreach ($pages as $page) {
             foreach ($actionIds as $action => $action_id) {
                 // get value entered
                 if (isset($pages_permissions[$page->id][$action])) {
                     $value = 'allow';
                 } else {
                     $value = 'deny';
                 }
                 // check if update is required
                 if (isset($existing[$page->id][$action_id])) {
                     if ($defaults[$action_id] && $value == 'allow' || !$defaults[$action_id] && $value == 'deny') {
                         // remove existing
                         $this->_role_permissions->page_actions()->newPivotStatementForId($page->id)->whereActionId($action_id)->delete();
                         if ($page->group_container > 0) {
                             $group = PageGroup::find($page->group_container);
                             foreach ($group->pages as $group_page) {
                                 $this->_role_permissions->page_actions()->newPivotStatementForId($group_page->id)->whereActionId($action_id)->delete();
                             }
                         }
                     } elseif ($existing[$page->id][$action_id] != $value) {
                         // update existing
                         $this->_role_permissions->page_actions()->newPivotStatementForId($page->id)->whereActionId($action_id)->update(['access' => $value]);
                         if ($page->group_container > 0) {
                             $group = PageGroup::find($page->group_container);
                             foreach ($group->pages as $group_page) {
                                 $this->_role_permissions->page_actions()->newPivotStatementForId($group_page->id)->whereActionId($action_id)->update(['access' => $value]);
                             }
                         }
                     }
                 } elseif (!$defaults[$action_id] && $value == 'allow' || $defaults[$action_id] && $value == 'deny') {
                     // add new page action
                     $this->_role_permissions->page_actions()->attach($page->id, ['action_id' => $action_id, 'access' => $value]);
                     if ($page->group_container > 0) {
                         $group = PageGroup::find($page->group_container);
                         foreach ($group->pages as $group_page) {
                             $this->_role_permissions->page_actions()->attach($group_page->id, ['action_id' => $action_id, 'access' => $value]);
                         }
                     }
                 }
             }
         }
         $this->addAlert('success', 'Page Permissions Updated');
     }
     $this->getPages($role_id);
 }