History: PluginCustomSearch Complex Example With Nested List Plugins
Source of version: 4 (current)
Copy to clipboard
This is a starter page to help document complex use of ((PluginCustomSearch)) and ((PluginList)). It uses Smarty templates and JavaScript/jQuery to achieve date searches on Tracker data in related Trackers, and depends on Elasticsearch for the facet/aggregation generation and works in Tiki 18.x. Initially a non-working dump of the (slightly simplified) files and pages: !! Pages !!! The top level wiki page {CODE(colors="tiki")}! Monthly Statistics {DIV(class="monthly-stats")} {CUSTOMSEARCH(tpl="themes/templates/monthly_statistics_search_form.tpl" searchfadediv="customsearch_0_results" recalllastsearch="0" searchonload="1" requireinput="0" forcesortmode="0" searchfadediv="customsearch_0_form" customsearchjs="1")} {filter field=tracker_id content="3"} {facet name="tracker_field_projectCompany" operator="or" count="200" order="_term_asc" min="0"} {pagination max="300"} {tablesorter server="n" sortable="y" sortList="[0,0]" tsortcolumns="type:n|type:digit|type:digit|type:digit|type:digit|type:digit|type:digit|type:digit" tsfilters="y" tstotals="type:none|type:col;formula:sum;filter:visible;label:Totals|type:col;formula:sum;filter:visible;label:Totals|type:col;formula:sum;filter:visible;label:Totals|type:col;formula:sum;filter:visible;label:Totals|type:col;formula:sum;filter:visible;label:Totals|type:col;formula:sum;filter:visible;label:Totals|type:col;formula:sum;filter:visible;label:Totals"} {output template="themes/templates/monthly_statistics_list.tpl"} {CUSTOMSEARCH} {DIV} {CODE} !!! monthly_statistics_search_form.tpl {CODE(colors="smarty")} {literal} <div class="clearfix stats-search"> {* add dropdown for months and year from May 1st*} <select id="daterange-presets" class="ignore"> <option value=""></option> <option value="year">{tr}Financial Year To Date{/tr}</option> <option value="1">{tr}January{/tr}</option> <option value="2">{tr}February{/tr}</option> <option value="3">{tr}March{/tr}</option> <option value="4">{tr}April{/tr}</option> <option value="5">{tr}May{/tr}</option> <option value="6">{tr}June{/tr}</option> <option value="7">{tr}July{/tr}</option> <option value="8">{tr}August{/tr}</option> <option value="9">{tr}September{/tr}</option> <option value="10">{tr}October{/tr}</option> <option value="11">{tr}November{/tr}</option> <option value="12">{tr}December{/tr}</option> </select> {daterange _field="tracker_field_projectDate" id="date" _to="now" _from="first day of this month"} {input type="Submit" value="Search" id="button" class="btn btn-default btn-sm"} </div> {/literal} {CODE} !!! monthly_statistics_list.tpl {CODE(colors="smarty")} {* PluginCusrtomSearch send the request data in the AJAX call in a JSON encoded parameter called "adddata" *} {$adddata = $smarty.request.adddata|json_decode:true} {* a couple of handy debug blocks to see what's being send trom the server *} <pre class="add-data" style="display: none">{$adddata|var_dump}</pre> <pre class="facets" style="display: none">{$facets|var_dump}</pre> {if isset($adddata.date.value)} {* date range is sent as two unix timestamps "xxxxxx,yyyyyy" *} {$projectDate = ','|explode:$smarty.request.adddata.date.value} {$projectDate = '{filter range="tracker_field_projectDate" from="'|cat:$projectDate[0]|cat:'" to="'|cat:$projectDate[1]|cat:'"}'} {/if} <table class="table" id="wpcs-0"> <thead> <tr> <th>{tr}Company Name{/tr}</th> <th>{tr}New Projects{/tr}</th> <th>{tr}Live Projects{/tr}</th> <th>{tr}High Priority Status{/tr}</th> <th>{tr}Medium Priority Status{/tr}</th> <th>{tr}Low Priority Status{/tr}</th> <th>{tr}Ordered PP{/tr}</th> <th>{tr}Ordered MP{/tr}</th> </tr> </thead> <tbody> {foreach $facets['tracker_field_projectCompanyName'].options as $id => $name} <pre class="results-row" style="display: none">{$row|var_dump}</pre> {* facet labels get sent in the form "Label Text (count)" so use a regular expression here to split it into an array *} {$r = preg_match('/(.*)\((\d+)\)/', $name, $m)} {wikiplugin _name='list'}{literal} {filter field="tracker_id" content="3"} {filter field="tracker_field_projectCompanyName" content="{/literal}{$id}{literal}"} {pagination max="1"} {facet name="tracker_field_projectCurrentStatus" operator="or"} {facet name="tracker_field_projectPriorityLevel" operator="or"} {/literal}{$projectDate}{literal} {output template="themes/templates/monthly_statistics_inner_row.tpl"} {ALTERNATE()}<tr class="result-row"><td>{/literal}{$m[1]}{literal}</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>{ALTERNATE} {FORMAT(name="distributorName")}{/literal}{$m[1]}{literal}{FORMAT} {FORMAT(name="projectCount")}{/literal}{$m[2]}{literal}{FORMAT} {/literal}{/wikiplugin} {/foreach} </tbody> <tfoot> <tr> <th>{tr}Company Name{/tr}</th> <th>{tr}New Projects{/tr}</th> <th>{tr}Live Projects{/tr}</th> <th>{tr}High Priority Status{/tr}</th> <th>{tr}Medium Priority Status{/tr}</th> <th>{tr}Low Priority Status{/tr}</th> <th>{tr}Ordered PP{/tr}</th> <th>{tr}Ordered MP{/tr}</th> </tr> </tfoot> </table> {CODE} !!! monthly_statistics_inner_row.tpl {CODE(colors="smarty")} {$row = $results[0]} <tr class="result-row"> <pre class="disty-facets" style="display: none">{$facets|var_dump}</pre> <pre class="disty-results" style="display: none">{$results|var_dump}</pre> <td>{$row.distributorName}</td> <td>{$row.projectCount}</td> {$c = preg_replace('/[^\(]*\((\d+)\)/', '$1', $facets.tracker_field_projectCurrentStatus.options.live)} <td>{($c) ? $c : 0}</td> {$c = preg_replace('/[^\(]*\((\d+)\)/', '$1', $facets.tracker_field_projectPriorityLevel.options.High)} <td>{($c) ? $c : 0}</td> {$c = preg_replace('/[^\(]*\((\d+)\)/', '$1', $facets.tracker_field_projectPriorityLevel.options.Medium)} <td>{($c) ? $c : 0}</td> {$c = preg_replace('/[^\(]*\((\d+)\)/', '$1', $facets.tracker_field_projectPriorityLevel.options.Low)} <td>{($c) ? $c : 0}</td> {$c = preg_replace('/[^\(]*\((\d+)\)/', '$1', $facets.tracker_field_projectCurrentStatus.options.orderedforpreproduction)} <td>{($c) ? $c : 0}</td> {$c = preg_replace('/[^\(]*\((\d+)\)/', '$1', $facets.tracker_field_projectCurrentStatus.options.orderedmassproduction)} <td>{($c) ? $c : 0}</td> </tr> {CODE} !!! Finally the JavaScript Bit {CODE(colors="javascript")} $(document).ready( function() { // set up month selector dropdown $("#daterange-presets").change(function () { var val = $(this).val(), $from = $("#date_from_dptxt"), $to = $("#date_to_dptxt"), now = new Date(), year = now.getFullYear(); if (val === "year") { $from.datepicker("setDate", "1/05/" + year).change(); $to.datepicker("setDate", now).change(); } else { val = parseInt(val); $from.datepicker("setDate", "1/" + val + "/" + year).change(); if (val === 12) { val = 0; year++; } $to.datepicker("setDate", "1/" + (val + 1) + "/" + year).change(); } $("table#wpcs-0").find("th:first-child").trigger("sort"); $csForm.submit(); }); }); {CODE}