Jump to content

Displaying and restricting total files per template and children


Kiwi Chris
 Share

Recommended Posts

I'm pretty comfortable with Processwire templates, and I've found to date existing modules have met my requirements, but I've run into something where I think I'm going to need to create a module.

The scenario I have is a site with sub-sites, which are basically just pages in the page tree that use a 'Subsite' template, and are assigned to different people to edit using the Admin Restrict Branch module from the module directory.

What I want to be able to do is check how much disk space each sub-site is using for associated files.
I figure I can iterate over the PageFiles array for each page that uses the 'subsite' template, and its children. This won't be 100% accurate as some images may have different sized variations generated by Processwire templates.
This possibly isn't too much of a problem, as I'm not really needing a completely strict limitation on disk space, but at least I want to track how much space has been used directly via uploads.

The bit I'm unsure about is how to go from knowing how much space is used by uploaded files for a page and its children, to getting that value to display on the admin screen when editing a page, and ideally, to prevent further uploads if a sub-site has reached a configurable limit, which may vary for different sub-sites, although initially, just having a common limit for all sub-sites would be a good start.

I'm thinking I might need to hook into the PageFilesManager, but given this will be my first attempt at building a module, any guidance would be appreciated.

  • Like 1
Link to comment
Share on other sites

@Kiwi Chris

Sounds interesting! Take this small file information module as a first approach.
Furthermore there is this related old and (unsupported?) module in the modules directory: http://modules.processwire.com/modules/manage-files/

Have fun with coding. :rolleyes:

<?php namespace ProcessWire;

class AdminFileInfo extends WireData implements Module, ConfigurableModule {

    /**
     *
     * @return array
     *
     */
    public static function getModuleInfo() {

        return array(
            'title' => 'Admin File Info',
            'summary' => __('Overview of all Pagefiles living under a specific branch'),
            'href' => '',
            'author' => 'kixe',
            'version' => 100,
            'icon'     => 'files-o'
        );
    }

       /**
     * Initialize, attach hooks
     *
     */
    public function init() {
    }

    /**
     * create Info Table
     *
     */
     static public function getInfoTable($parentID = 1) {
         $treePages = wire('pages')->find("has_parent=$parentID,include=all");
        $table = wire('modules')->get('MarkupAdminDataTable');
        $table->headerRow(array('PageID','Filename','size'));
        $table->totalSize = 0; // byte counter

        // @todo Notice: Undefined offset: 1 in /Users/c/Sites/_processwire/wire/modules/Markup/MarkupAdminDataTable/MarkupAdminDataTable.module on line 276

        foreach ($treePages as $singlePage) {
            if (PagefilesManager::hasFiles($singlePage) == false) continue; // no files
            $pageFileNames = $singlePage->filesManager->getFiles(); // get all files of this page
            foreach ($pageFileNames as $pageFileName) {
                if(null === $file = $singlePage->filesManager->getFile($pageFileName)) continue; // orphaned stuff
                $table->totalSize += $file->filesize;
                $table->row(array("#$singlePage->id" => $singlePage->editUrl, $pageFileName => $file->url, wireBytesStr($file->filesize)));
            }
        }

        return $table;
     }

     /**
     * module settings
     *
     */
    static public function getModuleConfigInputfields(array $data) {

        $modules = wire('modules');
        $parentID = isset($data['parent'])? $data['parent']: null;

        $fields = new InputfieldWrapper();
        $f = $modules->get('InputfieldPageListSelect');
        $f->attr('id+name', 'parent');
        $f->attr('value', $parentID);
        $f->icon = 'tree';
        $f->label = __('Page Tree Parent');
        $f->description = __("Select a parent page and click submit to get information about all files under this tree.");
        $fields->add($f);
      
        if ($parentID) {
            $page = wire('pages')->get($parentID);
            $table = self::getInfoTable($parentID);

            $f = $modules->get('InputfieldMarkup');
            $f->label = __('File Info for Page Branch: ').$page->path;
            $_description = __("This page branch uses %s of storage space for files.");
            $f->description = sprintf($_description, wireBytesStr($table->totalSize));
            $f->value = $table->render();
            $fields->add($f);
        }

        return $fields;
    }
}

 

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...