Exemplo n.º 1
0
 /**
  * Get index DataTable
  *
  * @param Query $query CakePHP query
  *
  * @return Table
  */
 protected function getIndexDataTable(Query $query)
 {
     $table = new Table('pages');
     $titleCol = new Column();
     $titleCol->setTitle('Title')->setData('Pages.title');
     $authorCol = new Column();
     $authorCol->setTitle('Author')->setData('Users.username')->setFormatter(function ($username, Page $page) {
         return $this->createView()->Html->link($username, ['author' => $page->getUserId()]);
     });
     $commentsCol = new Column();
     $commentsCol->setTitle('Comments')->setData('Pages.comment_count')->isSearchable(false);
     $dateCol = new Column();
     $dateCol->setTitle('Date')->setData('Pages.created_at')->isSearchable(false)->setFormatter(function (Time $cell) {
         return $cell->nice();
     });
     $action = new Column\Action();
     $action->setManager(function (Column\ActionBuilder $action, Page $page) {
         $action->addAction('view', __d('pages', 'View'), Router::url(['prefix' => false, 'controller' => 'Pages', 'action' => 'view', $page->getId()]), ['title' => __d('pages', 'View “%d”', $page->getTitle()), 'rel' => 'permalink']);
         $action->addAction('edit', __d('pages', 'Edit'), Router::url(['controller' => 'Pages', 'action' => 'edit', $page->getId()]), ['title' => __d('pages', 'Edit this item')]);
         $action->addAction('delete', __d('pages', 'Delete'), Router::url(['action' => 'delete', $page->getId()]), ['title' => __d('pages', 'Delete this item')]);
     })->setTitle('Action');
     $table->addColumn($titleCol)->addColumn($authorCol)->addColumn($commentsCol)->addColumn($dateCol)->addColumn($action);
     $table->setDataSource(new CakePHP($query, $this->request->here()));
     return $table;
 }
Exemplo n.º 2
0
 /**
  * Render test
  */
 public function testRenderAndToString()
 {
     $table = new Table();
     $needleTableTag = sprintf('<table id="%s" class="dataTable display">', $table->getTableId());
     $needleTableJsSelector = sprintf("\$('#%s').dataTable({", $table->getTableId());
     $this->assertContains($needleTableTag, $table->render());
     $this->assertContains($needleTableJsSelector, $table->render());
     $this->assertContains($needleTableJsSelector, (string) $table);
 }
Exemplo n.º 3
0
 /**
  * Prepare extensions config
  */
 protected function prepareExtensionsConfig()
 {
     foreach ($this->table->getExtensions() as $extension) {
         $properties = $extension->getProperties();
         if (isset($properties)) {
             $this->table->setProperty($extension->getPropertyName(), $properties);
         }
     }
 }
Exemplo n.º 4
0
    /**
     * Get DataTable
     *
     * @return Table
     * @throws \DataTable\Exception
     */
    protected function getDataTable()
    {
        TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100);
        TwigHelper::addJs('file:///Admin/vendor/jquery/dist/jquery.min.js', 20);
        TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100);
        TwigHelper::addJs('
        function deleteUser(id) {
    if (confirm(\'Delete this user?\')) {
        $.ajax({
            type: "DELETE",
            url: \'users/\' + id,
            success: function(affectedRows) {
                if (affectedRows > 0) window.location = \'users\';
            }
        });
    }
}', 110);
        $table = new Table();
        $col = new Column();
        $col->setTitle('Username')->setData('Users.username');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('Email')->setData('Users.email');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('Status')->setData('Users.status')->isSearchable(false)->setFormatter(function ($status, User $user) {
            $statuses = [0 => 'Inactive', 1 => 'Active', 2 => 'Banned'];
            return $statuses[$status];
        });
        $table->addColumn($col);
        $router = $this->getRouter();
        $col = new Column\Action();
        $col->setManager(function (Column\ActionBuilder $action, User $user) use($router) {
            /* TODO Admin RBAC */
            if (true) {
                $action->addAction('edit', 'Edit', $router->generateUrl(['users', $user->get('id')]));
                $action->addAction('delete', 'Delete', 'javascript:deleteUser("' . $user->get('id') . '");');
            }
        })->setTitle('Actions');
        $table->addColumn($col);
        $table->setDataSource(new CakePHP($this->getUsers(), $this->getRequest()->getRequestTarget()));
        return $table;
    }
Exemplo n.º 5
0
 /**
  * Get data
  *
  * @return array
  */
 protected function getData()
 {
     $data = [];
     foreach ($this->data as $dataValue) {
         $row = [];
         foreach ($this->table->getColumns() as $column) {
             if ($column instanceof ColumnInterface) {
                 $row[$column->getData()] = $column->getContent($dataValue);
                 continue;
             }
             if (is_callable($column->getFormatter())) {
                 $row[$column->getData()] = call_user_func_array($column->getFormatter(), [$dataValue[$column->getData()], $dataValue]);
             } else {
                 $row[$column->getData()] = (string) $dataValue[$column->getData()];
             }
         }
         $data[] = $row;
     }
     return $data;
 }
Exemplo n.º 6
0
    /**
     * Get DataTable
     *
     * @return Table
     * @throws \DataTable\Exception
     */
    protected function getDataTable()
    {
        TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100);
        TwigHelper::addJs('file:///Admin/vendor/jquery/dist/jquery.min.js', 20);
        TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100);
        TwigHelper::addJs('
        function deleteRole(id) {
    if (confirm(\'Delete this role?\')) {
        $.ajax({
            type: "DELETE",
            url: \'roles/\' + id,
            success: function(affectedRows) {
                if (affectedRows > 0) window.location = \'users/roles\';
            }
        });
    }
}', 110);
        $table = new Table();
        $col = new Column();
        $col->setTitle('name')->setData('Roles.name');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('title')->setData('Roles.title');
        $table->addColumn($col);
        $router = $this->getRouter();
        $col = new Column\Action();
        $col->setManager(function (Column\ActionBuilder $action, Role $role) use($router) {
            /* TODO Admin RBAC */
            if (true) {
                $action->addAction('edit', 'Edit', $router->generateUrl(['users', 'roles', $role->get('id')]));
                $action->addAction('delete', 'Delete', 'javascript:deleteRole("' . $role->get('id') . '");');
            }
        })->setTitle('Actions');
        $table->addColumn($col);
        $table->setDataSource(new CakePHP($this->getRoles(), $this->getRequest()->getRequestTarget()));
        return $table;
    }
Exemplo n.º 7
0
 /**
  * Extension interface test
  */
 public function testExtensionInterface()
 {
     $table = new Table();
     $table->addExtension(new ColVis());
     $this->assertEquals($table->getExtension(ColVis::PROPERTY_NAME)->getPropertyName(), ColVis::PROPERTY_NAME);
     $this->assertEquals($table->getExtension(ColVis::PROPERTY_NAME)->getDomName(), ColVis::DOM_NAME);
     $this->assertEquals($table->getExtension(ColVis::PROPERTY_NAME)->getProperties(), []);
     $this->assertEquals($table->getExtension(ColVis::PROPERTY_NAME)->getCallbacks(), []);
 }
Exemplo n.º 8
0
    private function generateDatatable()
    {
        TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100);
        TwigHelper::addJs('file:///Admin/vendor/jquery/dist/jquery.min.js', 20);
        TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100);
        TwigHelper::addJs('
        function deletePage(id) {
    if (confirm(\'Delete this user?\')) {
        $.ajax({
            type: "DELETE",
            url: \'pages/\' + id,
            success: function(affectedRows) {
                if (affectedRows > 0) window.location = \'pages\';
            }
        });
    }
}', 110);
        /** @var \Cake\ORM\Table $pagesTable */
        $pagesTable = TableRegistry::get('Pages.Pages');
        $pages = $pagesTable->find();
        $table = new Table();
        $col = new Column();
        $col->setTitle('Title')->setData('Pages.title');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('Slug')->setData('Pages.slug');
        $table->addColumn($col);
        $router = $this->getRouter();
        $col = new Column\Action();
        $col->setManager(function (Column\ActionBuilder $action, Entity $page) use($router) {
            if (true) {
                $action->addAction('edit', 'Edit', $router->generateUrl(['pages', $page->get('slug'), 'edit']));
                $action->addAction('delete', 'Delete', 'javascript:deletePage("' . $page->get('slug') . '");');
            }
        })->setTitle('Actions');
        $table->addColumn($col);
        $table->setDataSource(new CakePHP($pages, $this->getRequest()->getRequestTarget()));
        if ($this->getRequest()->isAjax()) {
            die($table->getResponse());
        }
        $this->getResponder()->setData('table', $table->render());
        $this->getResponder()->setData('pages', $pages);
    }
Exemplo n.º 9
0
 /**
  * Actions render
  *
  * @param string $template Action template name
  *
  * @return string
  */
 public function render($template)
 {
     $actions = $this->getActions();
     $this->flushActions();
     return $this->table->getRender()->getTwig()->render($template, ['actions' => $actions]);
 }
Exemplo n.º 10
0
 /**
  * Returns an array to be used as the `data` property of the output Javascript object
  *
  * @return mixed[]
  */
 private function createDataProperty()
 {
     $data = array();
     foreach ($this->data->getRows() as $row) {
         $item = new \stdClass();
         foreach ($row->getCells() as $cell) {
             if ($cell->getColumn()->getType() == Column::TYPE_DATETIME) {
                 $item->{$cell->getColumn()->getId()} = $cell->value->format('Y-m-d h:i:s.u');
             } elseif ($cell->getColumn()->getType() == Column::TYPE_DATE) {
                 $item->{$cell->getColumn()->getId()} = $cell->value->format('Y-m-d');
             } else {
                 $item->{$cell->getColumn()->getId()} = $cell->value;
             }
         }
         $data[] = $item;
     }
     return $data;
 }
Exemplo n.º 11
0
    /**
     * Get DataTable
     *
     * @return Table
     * @throws \DataTable\Exception
     */
    protected function getDataTable()
    {
        TwigHelper::addCss('file:///Admin/vendor/datatables/media/css/jquery.dataTables.min.css', 100);
        TwigHelper::addJs('file:///Admin/vendor/datatables/media/js/jquery.dataTables.min.js', 100);
        TwigHelper::addJs('
        function deleteCategory(id) {
    if (confirm(\'Delete this category?\')) {
        $.ajax({
            type: "DELETE",
            url: \'categories/\' + id,
            success: function(affectedRows) {
                if (affectedRows > 0) window.location = \'categories\';
            }
        });
    }
}', 110);
        $table = new Table();
        $col = new Column();
        $col->setTitle('Slug')->setData('Categories.slug');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('Title')->setData('Categories.title');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('Scope')->setData('Categories.scope');
        $table->addColumn($col);
        $col = new Column();
        $col->setTitle('Parent')->setData('Categories.parent_id')->isSearchable(false)->setFormatter(function ($parentId, Category $category) {
            if (null === $parentId) {
                return '-';
            }
            return $category->parentCategory->get('title');
        });
        $table->addColumn($col);
        $router = $this->getRouter();
        $col = new Column\Action();
        $col->setManager(function (Column\ActionBuilder $action, Category $category) use($router) {
            /* TODO Admin RBAC */
            if (true) {
                $action->addAction('edit', 'Edit', $router->generateUrl(['categories', $category->get('id')]));
                $action->addAction('delete', 'Delete', 'javascript:deleteCategory("' . $category->get('id') . '");');
            }
        })->setTitle('Actions');
        $table->addColumn($col);
        $table->setDataSource(new CakePHP($this->getCategories(), $this->getRequest()->getRequestTarget()));
        return $table;
    }
Exemplo n.º 12
0
 /**
  * Initialize data source
  *
  * @param Table $table Table object
  *
  * @return void
  */
 public function initialize(Table $table)
 {
     $this->table = $table;
     $this->table->setTableId($this->tableId);
 }
Exemplo n.º 13
0
 /**
  * Getter not exist property
  *
  * @expectedException BadMethodCallException
  * @expectedExceptionMessage Method "getNotExist" not exist.
  */
 public function testGetterNotExistProperty()
 {
     $table = new Table();
     $table->getNotExist();
 }
Exemplo n.º 14
0
 /**
  * Prepare CakePHP order
  *
  * @param Request $request DataTable request
  */
 protected function prepareOrder(Request $request)
 {
     foreach ($request->getOrder() as $order) {
         $this->query->order([$this->table->getColumns()[$order->getColumn()]->getData() => strtoupper($order->getDir())]);
     }
 }
Exemplo n.º 15
0
 /**
  * Render js test
  */
 public function testRenderJs()
 {
     $table = new Table();
     $this->assertNotContains('</table>', $table->getRender()->renderJs());
 }