Jump to content
bernhard

Preview/Discussion: RockDataTables

Recommended Posts

hi everybody,

this is a preview of a module that i'm working on for quite a long time. I needed it for an intranet office management application that i'm still working on. It makes it very easy to create very customized Datatables using the awesome jquery datatables plugin (www.datatables.net)

Download - sorry, removed as it changes too frequently; closed alpha - contact me if you think you can contribute

 

 

Remarks:

The module is intended to be used on the backend. Loading styles is at the moment only done via adding files to the $config->styles/scripts arrays. Also the communicaton to javascript is done via the $config->js() method that is built into the admin and would have to be implemented manually on frontend use. But it would not be difficult of course :)

Installation:

Nothing special here, just download + install

 

edit: removed some parts, because i made a complete rewrite of the module (and may not have been the last one)!

[...] removed

Why i created this module:

  • of course i know @Soma s module but i needed a lot more features and the newer datatables version. also i like to define all the columns as objects and have everything on one place.
  • lister & markupadmindatatable: nice for basic tables but lacks of features to modify the appearance of the cell values (like rendering icons, background colors and so on)
  • datatables provides a great frontend API for filtering, showing/hiding columns, getting data, modifying it...
  • it also plays well together with frontend charts like google chart api in this case:
    googlechart.gif

 

todo / roadmap:

  • reload only one row
  • add filters to all columns (in future also dropdowns, smaller than, bigger than, Regex, ...)
  • make it possible to add table on frontend pages
  • make buttons look like pw buttons
  • make it possible to set settings globally and only for one table
  • provide easy way of colorbars (percentage, red/green), maybe at different positions (left, top, right, bottom)
  • provide easy way of adding action items (edit, show, link etc - visible or onhover)
  • make own layout for tables (topleft, topright, bottom etc to make it easy to create extensions and show messages)
  • privide way of simple javascript plugins (like I already have for row sums etc)
  • provide easy way of handling actions for all selected items (delete selected, update selected...)
  • provide easy way of reloading data (--> easy when using ajax source)
  • easy way of showing/hiding columns
  • excel/csv/clipboard export
  • GUI for table setup
  • processmodule to show different tables (lister replacement)

 

  • Like 19

Share this post


Link to post
Share on other sites

need to setup tables via custom sql queries?

PHP

// init datatables module
    $dt2 = $modules->get('RockDataTables2');

// setup columns
    $sql = 'SELECT * FROM invoicetable';
    $dt2->setupColsBySql($sql);

// setup table
    $dt2->id = 'dt_finanzbuch';
    $dt2->js('/site/modules/ProcessRockFinance/dt_finanzbuch.js');
    
    $f = $modules->get('InputfieldMarkup');
    $f->label = 'Tabelle';
    $f->value = $dt2->render();
    $form->add($f);

// ##################################
// ajax request -> return data
// non-ajax -> render form + table
// ##################################
if($config->ajax) {
    echo $dt2->getJSON($dt2->getDataBySql($sql));
    die();
}

JS

$(document).ready(function() {
    // setup variables
        var opt = ProcessWire.config.dt_finanzbuch; // options from backend
        var colDefs = []; // column definitions

    // custom column definitions here

    // load default column definitions
        colDefs = colDefs.concat(dtGetDefaultDefs(opt));

    // initialise table
        $('#dt_finanzbuch').DataTable({
            ajax: { url: './book/', type: 'post' },
            columnDefs: colDefs,
            pageLength: 10,
            footerCallback: columnSums,
        });
});

Result

2017-02-16 17_52_11-.png

As you can see the sum function is not ready. it shows euros for my usecase...

  • Like 5

Share this post


Link to post
Share on other sites

Very nice! Do you think it is feasible to use this - or a future version - on the front end?

  • Like 1

Share this post


Link to post
Share on other sites

hi kathep,

sure it is also usable in the frontend. but it is no click-click solution. the module just provides some helpers to define the columns like i showed in the example. to make the code more clear to read, write and maintain. you would just have to include the right scripts (i'm currently loading all the plugins via cdn) and you would be fine. maybe you would also have to do some styling. but all of that is - at least for now - not intended to be handled by my module.

hope that answer helps you :)

  • Like 1

Share this post


Link to post
Share on other sites

plus DataTables are so easy to setup on the front end, you really don't need a module... just make your table, and then load the datatables scripts and init; the instructions are all there on the datatables site, and it is really well documented..

  • Like 1

Share this post


Link to post
Share on other sites

current version has properly formatted currency fields (atm only euro) and supports colorbars and column sums (also from selected rows)

had to remove this video, sorry

  • Like 2

Share this post


Link to post
Share on other sites

did a total rewrite of the module and will release it the next weeks when i'm done

 

  • Like 10

Share this post


Link to post
Share on other sites

Hi @bernhard Thanks for putting all the work into sharing this module. What came to my mind was that I do not really like providing code in admin input fields, except when it is a few lines of code. So wouldn't it be possible (optionally maybe) to provide paths to files instead?

  • Like 1

Share this post


Link to post
Share on other sites

Already done ;)

The fields are there for really simple tables and the files for more complex ones. The module will include all files related to a field automatically (PHP, js and CSS).

  • Like 1

Share this post


Link to post
Share on other sites

making progress on this but there's still a lot to do until it is releasable as a module (proper documentation mostly)...

sneak peak what's easily doable: a todo-app

5979eafe2b221_2017-07-2715_25_26-BaumrockAdminProcessWirebaumrock_dev.thumb.png.5ea6b9a66bc50536acc38be52edbeba4.png

definition is as easy as that:

	/**
	 * show table of todos
	 */
	public function executeTodos() {
		
    // create form
      $form = modules('InputfieldForm');
      $form->action = './';

      // add new project
        $b = modules('InputfieldButton');
        $b->attr("id+name", "addTodo");
        $b->addClass("ui-priority-primary pw-panel pw-panel-reload");
        $b->value = __("Neues Todo");
        $b->attr('data-href', pages(2)->url . 'page/add/?parent_id=' . pages('template=todos'));
        $b->icon = "plus";

        $f = modules('InputfieldMarkup');
        $f->value = $b->render();
        $form->add($f);

      // table
        $t = modules('InputfieldRockDatatables');
        $t->attr('id+name', 'manageTodos');

        $t->rows = pages('template=todo'); // define the table's source rows with one selector
        $t->ajax = 1; // set table mode to ajax loading making it possible to reload data via a simple $table.ajax.reload();

      [...]

        // remaining
        $col = new dtCol;
        $col->name = "remaining";
        $col->title = 'Tage';
        $col->className = 'minwidth';
        $col->data = function($page) {
          if(!$page->deadline) return '';

          $now = new \DateTime('now');
          $then = new \DateTime();
          $then->setTimestamp($page->deadline);
          $days = $now->diff($then)->format('%R%a');

          $color = config()->colors->lightred;
          if($days > 0) $color = config()->colors->lightorange;
          if($days > 7) $color = config()->colors->lightgreen;

          $obj = new \stdClass();
          $obj->display = $days;
          $obj->colorBars = [ [1, $color] ];

          return $obj;
        };
        $t->cols->add($col);

      [...]

      // add field to form

        $f = modules('InputfieldMarkup');
        $f->value = $t->render();
        $form->add($f);
    
    $out = $form->render();
    return $out;
	}

 

  • Like 7

Share this post


Link to post
Share on other sites

just to show that this module can render any kind of field (seems that this was not clear enough: https://processwire.com/talk/topic/16929-choose-custom-repeater-template/?do=findComment&comment=148803 )

a simple column showing images:

// pic
$col = new dtCol;
$col->name = "pic";
$col->title = 'pic';
$col->className = 'minwidth';
$col->data = function($page) {
  if(!$page->pics->count()) return '';
  return $page->pics->each(function($img) {
    return "<img src='{$img->maxSize(200,200)->url}' alt=''><br>";
  });
};
$t->cols->add($col);

2017-08-08 11_06_16-BaumrockAdmin • ProcessWire • baumrock.dev.png

so you can do whatever you want and whatever the API + PHP can do...

the module even supports a plugin-system where we can easily build cell-renderers that handle common tasks and make rendering even easier.

this for example is the js file of the column filter that shows a filter input on top of each column:

/**
 * this plugin creates filter inputs for every column
 * 
 * #### usage ####
 * just set your table's settings: { colfilter: true }
 * 
 * #### issues ####
 * the column filter breaks the table's column alignment on window resize
 * 
 * #### roadmap ####
 * make it possible to have different kind of inputs like range sliders and dropdowns
 * 
 */

/**
 * make sure that the tables searching setting is set to true
 */
$(document).on('beforeInit.rdt', '.RockDatatable', function(e, dt) {
  if(!dt.settings.colfilter) return;

  $table = $(dt.el);
  if(dt.settings.searching == false) console.log('overwriting setting "searching" to true for table ' + $table.attr('id'));
  dt.settings.searching = true;
});

/**
 * add colfilter row to table header
 */
$(document).on('afterInit.rdt', '.RockDatatable', function(e, dt) {
  if(!dt.settings.colfilter) return;

  var $table = $(e.target); // table dom element
  var table = $table.DataTable();
  var $thead = table.table().header();

  // prepare the new header row
  var $tr = $("<tr role='row' class='colfilter' />");
  $.each(dt.columnNames, function() {
    $tr.append($('<th><input type="text" class="colfilter"></th>'));
  });

  // add it to the table header
  $tr.prependTo($thead);
});

/**
 * handle filterinputs
 */
colfiltertimer = 0;
$(document).on('keypress keyup change', 'input.colfilter', function(e) {
  var $input = $(this);
  var $tr = $input.closest('tr');
  var $th = $input.closest('th');
  // datatables splits the table in 2 divs when scrollX is used! thats why we need to find the table via the wrapper
  var table = $input.closest('.dataTables_wrapper').find('table.RockDatatable').DataTable();
  var regex = false;

  // disable enter form submit
  var keyCode = e.keyCode || e.which;
  if (keyCode === 13) { 
    e.preventDefault();
    return false;
  }

  // delay search while typing
  clearTimeout(colfiltertimer);
  colfiltertimer = setTimeout(function() {
    table.columns($th.index()+':visIdx').search($input.val(), regex, false).draw();
  }, 500);
});

this may look complex in the beginning but once we have such a plugin we only have to set one option of the table and the filter inputs appear :)

 

  • Like 2

Share this post


Link to post
Share on other sites

datatable connected to the pagefield :)

demo.gif.713036b309f24bf69236f5146f7d2dc5.gif

updating the pageautocomplete field seemed a little tricky in the beginning but the final approach was quite easy:

// handle buttonclicks
$('#addcompetences').click(function(e) {
  e.preventDefault();
  var table = $table.DataTable(); // datatable instance
  var $field = $('#wrap_Inputfield_competences'); // asm select field
  var $template = $field.find('li.itemTemplate'); // selected page template
  var $ol = $template.closest('ol');
  
  // get selected competences from table
  var selectedtable = $.map(
    table.rows({selected:true}).data(),
    function(val, i) { return [[ val.id.sort, val.title + ' (' + val.cluster.filter + ')' ]]; }
  );

  // update ASM
  // clone template and populate values
  $.each(selectedtable, function(i,val) {
    // check double
    if($ol.find('.itemValue:contains("' + val[0] + '")').length) return;

    // add clone
    var $new = $template.clone().appendTo($template.parent());
    $new.find('.itemValue').text(val[0]);
    $new.find('.itemLabel').text(val[1]);
    $new.removeClass('itemTemplate');
  });
  
  // add state changed class
  $field.addClass('InputfieldStateChanged');

  // update original input
  InputfieldPageAutocomplete.rebuildInput($ol);
  
  return false;
});

 

  • Like 3

Share this post


Link to post
Share on other sites

very nice - a good alternative to other page select options out there, and intuitive for the user;

  • Like 1

Share this post


Link to post
Share on other sites

looks like i could use this module pretty soon on a project - LMK if you need someone to test it...

Share this post


Link to post
Share on other sites

another example how versatile and extendable this module is: you can write your own plugins to show custom column statistics. here i'm showing max, average and minimum values of some collected feedbacks. X means no answer and is excluded from the stats:

colstats.thumb.gif.21d09aa4282454f3e33ea9b216d3be4d.gif

plugincode is quite simple once you know the basics of datatables api:

$(document).on('update.rdt', 'div.colstats, div.colstatsselected', function(e, table, col) {
  var $div = $(e.target);
  var $table = $(table);
  var table = $table.DataTable();

  // get table settings
  var id = $table.attr('id').replace('RockDatatable_','');
  var dt = ProcessWire.config.RockDatatables[id];
  var colname = dt.columnNames[col.index()];

  var selector = {search: 'applied'};
  if($div.hasClass('colstatsselected')) selector.selected = true;

  // get data
  var data = table
    .rows(selector)
    .data()
    .filter(function(row) {
      // return true if it is a number
      // don't count empty cells or "noanswer" cells that have an X
      return row[colname]*1;
    })
    .pluck(colname);

  // get sum + rows
  var sum = data.sum();
  var rows = data.length;

  // early exit?
  if(!sum || !rows) {
    $div.html('');
    return;
  }

  // add stats for each column
  var avg = sum / rows;
  $div.html(
    ' ↑ ' + Math.max.apply(null, data) + 
    ' ø ' + avg.toFixed(2) +
    ' ↓ ' + Math.min.apply(null, data)
  );
});

:)

  • Like 4

Share this post


Link to post
Share on other sites

Hey @bernhard - I love what I see here, but I am curious how the data is stored? I assume it links to a custom db table that you create manually? 

As an aside in my pre-PW life I used SigmaGrid (which was ahead of its time) in the admin of my custom CMS to handle display/editing of purchases from an online store, so I am excited to play around with this when it's ready.

Share this post


Link to post
Share on other sites

hi adrian,

data is stored just as normal pw pages. nothing fancy here. this makes it very easy to use! the current setup is that you need 3 files to define your field:

// yourfield_data.php
<?php
// return all relations that belong to this project
return $pages->find(
  "template=feedback". // find all relations
  ",has_parent=$page" // project must be the current project
);
// yourfield_columns.php
<?php namespace ProcessWire;
// sample column showing the ID of the page
$col = new dtCol();
$col->name = "id";
$col->title = 'Columns ID';
$col->data = function($page) {
  return $page->id;
};
$this->cols->add($col);
// yourfield_js.js (optional, but in most cases necessary to do advanced stuff)
var settings = {
  select: true,
  pageLength: 100,
};
11 minutes ago, adrian said:

I am excited to play around with this when it's ready.

I'm also very excited to take this further. I cannot release it at the current stage though because major changes are not only likely to come but almost planned :)

 

i have to work more on performance. I'm thinking about some kind of caching technique and maybe also providing AJAX pagination.

some things like column statistics would only work on the client side when all the data is loaded, though. but i know that datatables can handle several thousands of rows quite efficiently so i think i'll find a way to work with those amounts of data.

  • Like 3

Share this post


Link to post
Share on other sites

Any news on this? A beta version maybe under the Christmas tree? :-)

 

 

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, dragan said:

Any news on this? A beta version maybe under the Christmas tree? :-)

Hi dragan,

unfortunately not. I need to do a complete rewrite of the module. As i need it for all my projects it is on top of my todolist for the next year. I'm still not sure if I should release it free or as a pro module... But I hope I can release something around march 2018...

  • Like 2
  • Sad 1

Share this post


Link to post
Share on other sites
4 hours ago, bernhard said:

I'm still not sure if I should release it free or as a pro module...

Maybe you want to consider other ways to get some funding. A quick idea is "croudfounding". I'm not suggesting using such a service because as far as I know they cost money, so maybe a dedicated PW forum thread under the topic Beer Garden will do. We send you money the cheapest way possible (bank transfer?) and you update the first post of the thread announcing who sent what. You can assign a random ID to each donator and that way only you and the donator can identify it (in order to keep this info private.)

  • Like 2

Share this post


Link to post
Share on other sites

got some very good news on this (it feels like christmas for myself, because this made me some headache over the last months and the solution is quite simple and solves a lot of problems).

screencapture-datatables-to-de-test6-1513092836163.thumb.png.ad29f6fcf69094ddfdc1ab9d9047df75.png

 

I did some performance tests on a table with more than 10.000 rows and got the following results here on my local test environment (laragon + i7 @ 2ghz).

  • rendering the table with javascript test-data works instantly (using deferrender option, did not try without)
  • rendering the table with data coming from pw pages and 5 different fields takes around 16s
  • single-language setup without caching takes around 5s to load
  • multi-language setup without caching around 8s
  • with markup cache enabled 180ms :)

the screenshot shows a multi language table with 10.000 rows and 12 fields that loaded in 9,1s when building the cache.

thats some really nice results, because it will make the setup even easier and it will even work with multilanguage wich I didn't know how to tackle before.

my goal always was to stay on the client side, because there you have all the power of datatables: you have a powerful API to filter, sort etc the table and this plays perfectly well together with other clientside libraries like chart.js; my testcase showed that the search and sorting was also very snappy so I think it's definitely doable!

It will be the first project after my master thesis and I may have something ready around 03/2018

On 9.12.2017 at 7:50 PM, szabesz said:

Maybe you want to consider other ways to get some funding. A quick idea is "croudfounding".

I don't think that's a proper way to do this. I think there are only the 2 options: Release it for free (donations are always possible and welcome of course) without any support, documentation etc. or release it as pro module and provide proper support and docs.

maybe @kongondo or @apeisa can share some of their experiences with us (or at least with me in a PM)? :)

  • Like 6

Share this post


Link to post
Share on other sites
1 hour ago, bernhard said:

the screenshot shows a multi language table with 10.000 rows and 12 fields that loaded in 9,1s when building the cache.

Does that mean: 10k PW-pages with 5 fields each? Multi-lang = how many? 2, 3, more?

1 hour ago, bernhard said:

my goal always was to stay on the client side

I don't quite understand this part: Does that mean your tests are done in a PW-module, and previously you only used it in the frontend?

In any case, that's good news.

re: pro/free

I agree. And (if it's reasonably priced) I'd be happy to buy a pro version, even if I wasn't sure if I would have a specific need in a client project right now.

re: cache mentioned above

Are you talking about caching the 10k pages? Or do you save the queried results (JSON?) as a file first, and then use this saved version as DT data-source?
I use the latter method for a custom search that features autosuggest/-complete + fuzzy search, where the generated JSON is around 500-600kb. It speeds up the whole thing tremendously.

Share this post


Link to post
Share on other sites

Ok guys, I got some REALLY nice results today :):):)

screencapture-datatables-to-test10-1513163338292.thumb.png.f5d0bd18d2ab498fad3f68febea73790.png

@dragan sorry, I was unclear in my previous post... I had to leave to a christmas party ;)

So here are some explanations and the new results:

  • Table with 10.000 rows without cache: 400ms (see the screenshot)
  • Table with 10.000 rows with cache: 200ms

The key was to get the data directly from the database. Of course I knew before that this possibility exists, but I didn't know how to solve the problem of multilanguage and returning complex data (like $page->parent->title for one column) and still keep the setup of a table simple and straightforward. Now I found a great way and I'm really eager to start working on this :) 

What I meant by "stay on the client side" was, that I want all the data get transferred to the client and then rendered by datatables. The other option would be to use ajax pagination and load only junks of the data to the client. But having all the data on the client is a huge benefit for manipulating, filtering, sorting, charting etc.; and you where right about my "cache" wording: I'm talking about a cached string holding all the data for the datatable. So if the cache exists it just loads the string - if not it creates the string from the database and then loads the data into the datatables.

Any wishes/ideas for features that I should think of when developing the next version of the module?

...see the feature list in the first post :)

  • Like 5

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      First a note about my other modules...
      I have three existing modules that are similar in that they allow restrictions to be placed on repeating inputfields: Limit Repeater, Limit PageTable, Limit Table
      Restrict Repeater Matrix takes a different approach to the module configuration from those other modules. The module settings for Restrict Repeater Matrix are applied in the field settings rather in a module config screen. I think this new approach is better, but it means that it isn't practical to create different settings for different roles via the admin interface. Instead the module has a hookable method, allowing roles to be targeted and other advanced usages to be achieved via a hook. The result is that the module is more flexible.
      I intend to transition my other modules to the same approach over the coming weeks, but because this will result in breaking changes I will be releasing the updated modules under new names ("Restrict Repeater", etc) to avoid users upgrading via the Upgrades module without full awareness of the changes. The old modules will be marked as deprecated.
      Restrict Repeater Matrix
      A module for ProcessWire CMS/CMF. Allows restrictions and limits to be placed on Repeater Matrix fields. Requires ProcessWire >= v3.0.0 and FieldtypeRepeaterMatrix >= v0.0.5.
      For any matrix type in a Repeater Matrix field you have the option to:
      Disable settings for items (cannot change matrix type) Prevent drag-sorting of items Prevent cloning of items Prevent toggling of the published state of items Prevent trashing of items Limit the number of items that may be added to the inputfield. When the limit is reached the "Add new" button for the matrix type will be removed and the matrix type will not be available for selection in the "Type" dropdown of other matrix items. Please note that restrictions and limits are applied with CSS/JS so should not be considered tamper-proof.
      Usage
      Install the Restrict Repeater Matrix module.
      For each matrix type created in the Repeater Matrix field settings, a "Restrictions" fieldset is added at the bottom of the matrix type settings:

      For newly added matrix types, the settings must be saved first in order for the Restrictions fieldset to appear. Set restrictions for each matrix type as needed. A limit of zero means that no items of that matrix type may be added to the inputfield.
      Setting restrictions via a hook
      Besides setting restrictions in the field settings, you can also apply or modify restrictions by hookingRestrictRepeaterMatrix::checkRestrictions. This allows for more focused restrictions, for example, applying restrictions depending on the template of the page being edited or depending on the role of the user.
      The checkRestrictions() method receives the following arguments:
      $field This Repeater Matrix field $inputfield This Repeater Matrix inputfield $matrix_types An array of matrix types for this field. Each key is the matrix type name and the value is the matrix type integer. $page The page that is open in ProcessPageEdit The method returns a multi-dimensional array of matrix types and restrictions for each of those types. An example of a returned array:

      Example hooks
      Prevent the matrix type "images_block" from being added to "my_matrix_field" in a page with the "basic-page" template:
      $wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) { $field = $event->arguments('field'); $page = $event->arguments('page'); $type_restrictions = $event->return; if($field->name === 'my_matrix_field' && $page->template->name === 'basic-page') { $type_restrictions['images_block']['limit'] = 0; } $event->return = $type_restrictions; }); Prevent non-superusers from trashing any Repeater Matrix items in "my_matrix_field":
      $wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) { $field = $event->arguments('field'); $type_restrictions = $event->return; if($field->name === 'my_matrix_field' && !$this->user->isSuperuser()) { foreach($type_restrictions as $key => $value) { $type_restrictions[$key]['notrash'] = true; } } $event->return = $type_restrictions; }); http://modules.processwire.com/modules/restrict-repeater-matrix/
      https://github.com/Toutouwai/RestrictRepeaterMatrix
    • By BitPoet
      Here's a little text formatter module that redirects all external links found in a textarea (e.g. for hit counting) to a given URL.
      TextformatterExternalRedirect
      GitHub repo
      As TextFormatters do, this module modifies the contents of the textarea (HTML) at rendering time. It prepends the given URL to the link address for external links (http and https), optionally makes sure that the link is opened in a new window and allows you to add css classes to such links.
      All options can be configured in the module's configuration settings.
      Usage:
      Download the zip archive and extract it to site/modules, the rename the top folder to TextformatterExternalRedirect Go to the backend and select "Modules" -> "Refresh", then click "Install" for "External Link Redirect" Click "Settings" and configure the module
      Go into the field configuration for the field(s) you want this formatter to apply to and add it from the dropdown
      Done, now any external links in the configured fields will have the configured settings applied Feel free to leave feedback and post and questions you have here.
       
    • By teppo
      Hey folks!
      Took a couple of late nights, but managed to turn this old gist of mine into a proper module. The name is SearchEngine, and currently it provides support for indexing page contents (into a hidden textarea field created automatically), and also includes a helper feature ("Finder") for querying said contents. No fancy features like stemming here yet, but something along those lines might be added later if it seems useful (and if I find a decent implementation to integrate).
      Though the API and selector engine make it really easy to create site search pages, I pretty much always end up duplicating the same features from site to site. Also – since it takes a bit of extra time – it's tempting to skip over some accessibility related things, and leave features like text highlighting out. Overall I think it makes sense to bundle all that into a module, which can then be reused over and over again 🙂
      Note: markup generation is not yet built into the module, which is why the examples below use PageArray::render() method to produce a simple list of results. This will be added later on, as a part of the same module or a separate Markup module. There's also no fancy JS API or anything like that (yet).
      This is an early release, so be kind – I got the find feature working last night (or perhaps this morning), and some final tweaks and updates were made just an hour ago 😅
      GitHub repository: https://github.com/teppokoivula/SearchEngine Modules directory: https://modules.processwire.com/modules/search-engine/ Demo: https://wireframe-framework.com/search/ Usage
      Install SearchEngine module. Note: the module will automatically create an index field install time, so be sure to define a custom field (via site config) before installation if you don't want it to be called "search_index". You can change the field name later as well, but you'll have to update the "index_field" option in site config or module settings (in Admin) after renaming it.
      Add the site search index field to templates you want to make searchable. Use selectors to query values in site search index. Note: you can use any operator for your selectors, you will likely find the '=' and '%=' operators most useful here. You can read more about selector operators from ProcessWire's documentation.
      Options
      By default the module will create a search index field called 'search_index' and store values from Page fields title, headline, summary, and body to said index field when a page is saved. You can modify this behaviour (field name and/or indexed page fields) either via the Module config screen in the PocessWire Admin, or by defining $config->SearchEngine array in your site config file or other applicable location:
      $config->SearchEngine = [ 'index_field' => 'search_index', 'indexed_fields' => [ 'title', 'headline', 'summary', 'body', ], 'prefixes' => [ 'link' => 'link:', ], 'find_args' => [ 'limit' => 25, 'sort' => 'sort', 'operator' => '%=', 'query_param' => null, 'selector_extra' => '', ], ]; You can access the search index field just like any other ProcessWire field with selectors:
      if ($q = $sanitizer->selectorValue($input->get->q)) { $results = $pages->find('search_index%=' . $query_string . ', limit=25'); echo $results->render(); echo $results->renderPager(); } Alternatively you can delegate the find operation to the SearchEngine module as well:
      $query = $modules->get('SearchEngine')->find($input->get->q); echo $query->resultsString; // alias for $query->results->render() echo $query->pager; // alias for $query->results->renderPager() Requirements
      ProcessWire >= 3.0.112 PHP >= 7.1.0 Note: later versions of the module may require Composer, or alternatively some additional features may require installing via Composer. This is still under consideration – so far there's nothing here that would really depend on it, but advanced features like stemming most likely would.
      Installing
      It's the usual thing: download or clone the SearchEngine directory into your /site/modules/ directory and install via Admin. Alternatively you can install SearchEngine with Composer by executing composer require teppokoivula/search-engine in your site directory.
    • By teppo
      MarkupMenu is a markup module for generating menu trees. When provided a root page as a starting point, it generates a navigation tree (by default as a HTML "<ul>" element wrapped by a "<nav>" element) from that point onwards. If you've also provided it with current (active) page, the menu will be rendered accordingly, with current item highlighted and items rendered up to that item and its children (unless you disable the "collapsed" option, in which case the full page tree will be rendered instead).
      Modules directory: https://modules.processwire.com/modules/markup-menu/ GitHub repository: https://github.com/teppokoivula/MarkupMenu Usage
      As a markup module, MarkupMenu is intended for front-end use, but you can of course use it in a module as well. Typically you'll only need the render() method, which takes an array of options as its only argument:
      echo $modules->get('MarkupMenu')->render([ 'root_page' => $pages->get(1), 'current_page' => $page, ]); Note: if you omit root_page, site root page is used by default. If you omit current_page, the menu will be rendered, but current (active) page won't be highlighted etc.
      A slightly more complex example, based on what I'm using on one of my own sites to render a (single-level) top menu:
      echo $modules->get('MarkupMenu')->render([ 'current_page' => $page, 'templates' => [ 'nav' => '<nav class="{classes} menu--{menu_class_modifier}" aria-label="{aria_label}">%s</nav>', 'item_current' => '<a class="menu__item menu__item--current" href="{item.url}" tabindex="0" aria-label="Current page: {item.title}">{item.title}</a>', ], 'placeholders' => [ 'menu_class_modifier' => 'top', 'aria_label' => 'Main navigation', ], 'include' => [ 'root_page' => true, ], 'exclude' => [ 'level_greater_than' => 1, ], ]); Note: some things you see above may not be entirely sensible, such as the use of {menu_class_modifier} and {aria_label} placeholders. On the actual site the "nav" template is defined in site config, so I can define just these parts on a case-by-case basis while actual nav markup is maintained in one place.
      Please check out the README file for available render options. I'd very much prefer not to keep this list up to date in multiple places. Basically there are settings for defining "templates" for different parts of the menu (list, item, etc.), include array for defining rules for including in the menu and exclude array for the opposite effect, classes and placeholders arrays for overriding default classes and injecting custom placeholders, etc. 🙂
      MarkupMenu vs. MarkupSimpleNavigation
      TL;DR: this is another take on the same concept. There are many similarities, but also some differences – especially when it comes to the supported options and syntax. If you're currently using MarkupSimpleNavigation then there's probably no reason to switch over.
      I'd be surprised if someone didn't draw lines between this module and Soma's awesome MarkupSimpleNavigation. Simply put I've been using MSN (...) for years, and it's been great – but there are some issues with it, particularly in the markup generation area, and it also does some things in a way that doesn't quite work for me – the xtemplates thing being one of these. In some ways less about features, and more about style, I guess 🙂
      Anyhow, in MarkupMenu I've tried to correct those little hiccups, modernise the default markup, and allow for more flexibility with placeholder variables and additional / different options. MarkupMenu was built for ProcessWire 3.0.112+ and with PHP 7.1+ in mind, it's installable with Composer, and I have a few additional ideas (such as conditional placeholders) still on my todo list.
      One more small(ish) difference is that MarkupMenu supports overriding default options via $config->MarkupMenu. I find myself redefining the default markup for every site, which until now meant that each site had a wrapper function for MarkupSimpleNavigation (to avoid code / config repetition), and this way I've been able to leave that out 🙂
      Requirements
      ProcessWire >= 3.0.112 PHP >= 7.1.0 If you're working on an earlier version of ProcessWire or PHP, use MarkupSimpleNavigation instead.
    • By teppo
      Hey folks!
      I'm happy to finally introduce a project I've been working on for quite a while now: it's called Wireframe, and it is an output framework for ProcessWire.
      Note that I'm posting this in the module development area, maily because this project is still in rather early stage. I've built a couple of sites with it myself, and parts of the codebase have been powering some pretty big and complex sites for many years now, but this should still be considered a soft launch 🙂
      --
      Long story short, Wireframe is a module that provides the "backbone" for building sites (and apps) with ProcessWire using an MVC (or perhaps MVVM – one of those three or four letter abbreviations anyway) inspired methodology. You could say that it's an output strategy, but I prefer the term "output framework" since in my mind the word "strategy" means something less tangible. A way of doing things, rather than a tool that actually does things.
      Wireframe (the module) provides a basic implementation for some familiar MVC concepts, such as Controllers and a View layer – the latter of which consists of layouts, partials, and template-specific views. There's no "model" layer, since in this context ProcessWire is the model. As a module Wireframe is actually quite simple – not even nearly the biggest one I've built – but there's still quite a bit of stuff to "get", so I've put together a demo & documentation site for it at https://wireframe-framework.com/.
      In addition to the core module, I'm also working on a couple of site profiles based on it. My current idea is actually to keep the module very light-weight, and implement most of the "opinionated" stuff in site profiles and/or companion modules. For an example MarkupMenu (which I released a while ago) was developed as one of those "companion modules" when I needed a menu module to use on the site profiles.
      Currently there are two public site profiles based on Wireframe:
      site-wireframe-docs is the demo&docs site mentioned above, just with placeholder content replaced with placeholder content. It's not a particularly complex site, but I believe it's still a pretty nice way to dig into the Wireframe module. site-wireframe-boilerplate is a boilerplate (or starter) site profile based on the docs site. This is still very much a work in progress, but essentially I'm trying to build a flexible yet full-featured starter profile you can just grab and start building upon. There will be a proper build process for resources, it will include most of the basic features one tends to need from site to site, etc. --
      Requirements and getting started:
      Wireframe can be installed just like any ProcessWire module. Just clone or download it to your site/modules/ directory and install. It doesn't, though, do a whole lot of stuff on itself – please check out the documentation site for a step-by-step guide on setting up the directory structure, adding the "bootstrap file", etc. You may find it easier to install one of the site profiles mentioned above, but note that this process involves the use of Composer. In the case of the site profiles you can install ProcessWire as usual and download or clone the site profile directory into your setup, but after that you should run "composer install" to get all the dependencies – including the Wireframe module – in place. Hard requirements for Wireframe are ProcessWire 3.0.112 and PHP 7.1+. The codebase is authored with current PHP versions in mind, and while running it on 7.0 may be possible, anything below that definitely won't work. A feature I added just today to the Wireframe module is that in case ProcessWire has write access to your site/templates/ directory, you can use the module settings screen to create the expected directories automatically. Currently that's all, and the module won't – for an example – create Controllers or layouts for you, so you should check out the site profiles for examples on these. (I'm probably going to include some additional helper features in the near future.)
      --
      This project is loosely based on an earlier project called pw-mvc, i.e. the main concepts (such as Controllers and the View layer) are very similar. That being said, Wireframe is a major upgrade in terms of both functionality and architecture: namespaces and autoloader support are now baked in, the codebase requires PHP 7, Controllers are classes extending \Wireframe\Controller (instead of regular "flat" PHP files), implementation based on a module instead of a collection of drop-in files, etc.
      While Wireframe is indeed still in a relatively early stage (0.3.0 was launched today, in case version numbers matter) for the most part I'm happy with the way it works, and likely won't change it too drastically anytime soon – so feel free to give it a try, and if you do, please let me know how it went. I will continue building upon this project, and I am also constantly working on various side projects, such as the site profiles and a few unannounced helper modules.
      I should probably add that while Wireframe is not hard to use, it is more geared towards those interested in "software development" type methodology. With future updates to the module, the site profiles, and the docs I hope to lower the learning curve, but certain level of "developer focus" will remain. Although of course the optimal outcome would be if I could use this project to lure more folks towards that end of the spectrum... 🙂
      --
      Please let me know what you think – and thanks in advance!
×
×
  • Create New...