Example #1
0
 public function ListViewForm()
 {
     $params = $this->request->requestVar('q');
     $list = $this->getList($params, $parentID = $this->request->requestVar('ParentID'));
     $gridFieldConfig = GridFieldConfig::create()->addComponents(new GridFieldSortableHeader(), new GridFieldDataColumns(), new GridFieldPaginator(self::config()->page_length));
     if ($parentID) {
         $gridFieldConfig->addComponent(GridFieldLevelup::create($parentID)->setLinkSpec('?ParentID=%d&view=list')->setAttributes(array('data-pjax' => 'ListViewForm,Breadcrumbs')));
     }
     $gridField = new GridField('Page', 'Pages', $list, $gridFieldConfig);
     $columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns');
     // Don't allow navigating into children nodes on filtered lists
     $fields = array('getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'), 'singular_name' => _t('SiteTree.PAGETYPE'), 'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'));
     $gridField->getConfig()->getComponentByType('GridFieldSortableHeader')->setFieldSorting(array('getTreeTitle' => 'Title'));
     $gridField->getState()->ParentID = $parentID;
     if (!$params) {
         $fields = array_merge(array('listChildrenLink' => ''), $fields);
     }
     $columns->setDisplayFields($fields);
     $columns->setFieldCasting(array('Created' => 'Datetime->Ago', 'LastEdited' => 'Datetime->FormatFromSettings', 'getTreeTitle' => 'HTMLText'));
     $controller = $this;
     $columns->setFieldFormatting(array('listChildrenLink' => function ($value, &$item) use($controller) {
         $num = $item ? $item->numChildren() : null;
         if ($num) {
             return sprintf('<a class="cms-panel-link list-children-link" data-pjax-target="ListViewForm,Breadcrumbs" href="%s">%s</a>', Controller::join_links($controller->Link(), sprintf("?ParentID=%d&view=list", (int) $item->ID)), $num);
         }
     }, 'getTreeTitle' => function ($value, &$item) use($controller) {
         return sprintf('<a class="action-detail" href="%s">%s</a>', Controller::join_links(singleton('CMSPageEditController')->Link('show'), (int) $item->ID), $item->TreeTitle);
     }));
     $listview = CMSForm::create($this, 'ListViewForm', new FieldList($gridField), new FieldList())->setHTMLID('Form_ListViewForm');
     $listview->setAttribute('data-pjax-fragment', 'ListViewForm');
     $listview->setResponseNegotiator($this->getResponseNegotiator());
     $this->extend('updateListView', $listview);
     $listview->disableSecurityToken();
     return $listview;
 }
 /**
  * @covers GridField::getState
  */
 public function testGetState()
 {
     $obj = new GridField('testfield', 'testfield');
     $this->assertTrue($obj->getState() instanceof GridState_Data);
     $this->assertTrue($obj->getState(false) instanceof GridState);
 }
 /**
  * Handles requests to move an item to the previous or next page.
  */
 public function handleMoveToPage(GridField $grid, $request)
 {
     if (!($paginator = $grid->getConfig()->getComponentByType('GridFieldPaginator'))) {
         $this->httpError(404, 'Paginator component not found');
     }
     $move = $request->postVar('move');
     $field = $this->getSortField();
     $list = $grid->getList();
     $manip = $grid->getManipulatedList();
     $existing = $manip->map('ID', $field)->toArray();
     $values = $existing;
     $order = array();
     $id = isset($move['id']) ? (int) $move['id'] : null;
     $to = isset($move['page']) ? $move['page'] : null;
     if (!isset($values[$id])) {
         $this->httpError(400, 'Invalid item ID');
     }
     $this->populateSortValues($list);
     $page = (int) $grid->getState()->GridFieldPaginator->currentPage ?: 1;
     $per = $paginator->getItemsPerPage();
     if ($to == 'prev') {
         $swap = $list->limit(1, ($page - 1) * $per - 1)->first();
         $values[$swap->ID] = $swap->{$field};
         $order[] = $id;
         $order[] = $swap->ID;
         foreach ($existing as $_id => $sort) {
             if ($id != $_id) {
                 $order[] = $_id;
             }
         }
     } elseif ($to == 'next') {
         $swap = $list->limit(1, $page * $per)->first();
         $values[$swap->ID] = $swap->{$field};
         foreach ($existing as $_id => $sort) {
             if ($id != $_id) {
                 $order[] = $_id;
             }
         }
         $order[] = $swap->ID;
         $order[] = $id;
     } else {
         $this->httpError(400, 'Invalid page target');
     }
     $this->reorderItems($list, $values, $order);
     return $grid->FieldHolder();
 }