Merge "Make it possible to sort on simple custom columns"
[ninja.git] / modules / lsfilter / widgets / gridstat / gridstat.php
blob79f04269316228853c804df0be050383fb86b341
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * ORM gridstat widget
6 * @author op5 AB
7 */
8 class gridstat_Widget extends widget_Base {
9 /**
10 * Set if this widget is duplicatable
12 protected $duplicatable = true;
14 /**
15 * An array containing the settings for this widget; which filters to use
17 protected $settings = false;
19 /**
20 * Constructor. This should be overloaded, to upadte the settings-attribute
21 * when making a custom widget of this type
23 public function __construct($widget_model) {
24 parent::__construct($widget_model);
25 $this->settings = array();
28 /**
29 * Disable everything configurable. This is useful when including the widget with generetated parameters from a controller.
31 public function set_fixed() {
32 $this->movable = false;
33 $this->removable = false;
34 $this->closeconfirm = false;
35 $this->editable = false;
36 $this->duplicatable = false;
39 /**
40 * Load the options for this widget.
42 public function options() {
43 $options = parent::options();
44 return $options;
47 private function grab_filters( $arr ) {
48 $filters = array();
49 foreach( $arr as $key => $value ) {
50 if( is_array( $value ) ) {
51 $filters = array_merge($filters, $this->grab_filters($value));
52 } else if( $key === 'filter' ) {
53 $filters[] = $value;
56 return $filters;
59 private function apply_filters( $arr, $results ) {
60 $filters = array();
61 foreach( $arr as $key => $value ) {
62 if( is_array( $value ) ) {
63 $res = $this->apply_filters($value, $results);
65 if( $res !== false ) {
66 if( $key === 'fields' ) {
67 if( count($res) == 0 ) {
68 return false;
71 $filters[$key] = $res;
73 } else {
74 $filters[$key] = $value;
76 if( $key === 'filter' ) {
77 $filters['count'] = $results[$value];
78 if( $results[$value] == 0 ) {
79 return false;
84 return $filters;
87 /**
88 * Fetch the data and show the widget
90 public function index() {
91 try {
92 $filters = $this->grab_filters($this->settings);
93 $results = array();
94 foreach( $filters as $filter ) {
95 $results[$filter] = count(ObjectPool_Model::get_by_query($filter));
97 $this->data = $this->apply_filters($this->settings, $results);
98 if(empty($this->data)) {
99 $this->data = array(
100 array(
101 'icon' => 'shield-not-disabled',
102 'title' => _('N/A'),
103 'fields' => array()
107 require('view.php');
108 } catch( ORMException $e ) {
109 require('view_error.php');
110 } catch( op5LivestatusException $e ) {
111 require('view_error.php');
112 } catch( Exception $e ) {
113 print '<pre>';
114 print_r($e);
115 print '</pre>';