Grav\Plugin\Admin\AdminController::taskFilterPages PHP Méthode

taskFilterPages() protected méthode

Handles filtering the page by modular/visible/routable in the pages list.
protected taskFilterPages ( )
    protected function taskFilterPages()
    {
        if (!$this->authorizeTask('filter pages', ['admin.pages', 'admin.super'])) {
            return;
        }
        $data = $this->post;
        $flags = !empty($data['flags']) ? array_map('strtolower', explode(',', $data['flags'])) : [];
        $queries = !empty($data['query']) ? explode(',', $data['query']) : [];
        /** @var Collection $collection */
        $collection = $this->grav['pages']->all();
        if (count($flags)) {
            // Filter by state
            $pageStates = ['modular', 'nonmodular', 'visible', 'nonvisible', 'routable', 'nonroutable', 'published', 'nonpublished'];
            if (count(array_intersect($pageStates, $flags)) > 0) {
                if (in_array('modular', $flags)) {
                    $collection = $collection->modular();
                }
                if (in_array('nonmodular', $flags)) {
                    $collection = $collection->nonModular();
                }
                if (in_array('visible', $flags)) {
                    $collection = $collection->visible();
                }
                if (in_array('nonvisible', $flags)) {
                    $collection = $collection->nonVisible();
                }
                if (in_array('routable', $flags)) {
                    $collection = $collection->routable();
                }
                if (in_array('nonroutable', $flags)) {
                    $collection = $collection->nonRoutable();
                }
                if (in_array('published', $flags)) {
                    $collection = $collection->published();
                }
                if (in_array('nonpublished', $flags)) {
                    $collection = $collection->nonPublished();
                }
            }
            foreach ($pageStates as $pageState) {
                if (($pageState = array_search($pageState, $flags)) !== false) {
                    unset($flags[$pageState]);
                }
            }
            // Filter by page type
            if (count($flags)) {
                $types = [];
                $pageTypes = array_keys(Pages::pageTypes());
                foreach ($pageTypes as $pageType) {
                    if (($pageKey = array_search($pageType, $flags)) !== false) {
                        $types[] = $pageType;
                        unset($flags[$pageKey]);
                    }
                }
                if (count($types)) {
                    $collection = $collection->ofOneOfTheseTypes($types);
                }
            }
            // Filter by page type
            if (count($flags)) {
                $accessLevels = $flags;
                $collection = $collection->ofOneOfTheseAccessLevels($accessLevels);
            }
        }
        if (!empty($queries)) {
            foreach ($collection as $page) {
                foreach ($queries as $query) {
                    $query = trim($query);
                    if (stripos($page->getRawContent(), $query) === false && stripos($page->title(), $query) === false) {
                        $collection->remove($page);
                    }
                }
            }
        }
        $results = [];
        foreach ($collection as $path => $page) {
            $results[] = $page->route();
        }
        $this->admin->json_response = ['status' => 'success', 'message' => $this->admin->translate('PLUGIN_ADMIN.PAGES_FILTERED'), 'results' => $results];
        $this->admin->collection = $collection;
    }