Skip to content
Snippets Groups Projects
class.search.php 42.1 KiB
Newer Older
  • Learn to ignore specific revisions
  •     function __construct($fields, $state) {
            parent::__construct($fields);
            $this->state = $state;
        }
    }
    
    
    // Advanced search special fields
    
    class HelpTopicChoiceField extends ChoiceField {
        function hasIdValue() {
            return true;
        }
    
    
        function getChoices($verbose=false) {
    
            return Topic::getHelpTopics(false, Topic::DISPLAY_DISABLED);
        }
    }
    
    require_once INCLUDE_DIR . 'class.dept.php';
    class DepartmentChoiceField extends ChoiceField {
    
        function getChoices($verbose=false) {
    
            return Dept::getDepartments();
        }
    
        function getSearchMethods() {
            return array(
    
                'includes' =>   __('is'),
                '!includes' =>  __('is not'),
    
            );
        }
    }
    
    class AssigneeChoiceField extends ChoiceField {
    
        function getChoices($verbose=false) {
    
                'M' => __('Me'),
                'T' => __('One of my teams'),
    
            );
            foreach (Staff::getStaffMembers() as $id=>$name) {
    
                // Don't include $thisstaff (since that's 'Me')
                if ($thisstaff && $thisstaff->getId() == $id)
                    continue;
    
                $items['s' . $id] = $name;
            }
            foreach (Team::getTeams() as $id=>$name) {
                $items['t' . $id] = $name;
            }
            return $items;
        }
    
        function getSearchMethods() {
            return array(
    
                'assigned' =>   __('assigned'),
                '!assigned' =>  __('unassigned'),
                'includes' =>   __('includes'),
                '!includes' =>  __('does not include'),
    
            );
        }
    
        function getSearchMethodWidgets() {
            return array(
                'assigned' => null,
    
                '!assigned' => null,
                'includes' => array('ChoiceField', array(
    
                    'choices' => $this->getChoices(),
                    'configuration' => array('multiselect' => true),
                )),
    
                '!includes' => array('ChoiceField', array(
    
                    'choices' => $this->getChoices(),
                    'configuration' => array('multiselect' => true),
                )),
            );
        }
    
    
        function getSearchQ($method, $value, $name=false) {
            global $thisstaff;
    
            $Q = new Q();
            switch ($method) {
            case 'assigned':
    
                $Q->negate();
            case '!assigned':
                $Q->add(array('team_id' => 0,
                    'staff_id' => 0));
    
                break;
            case '!includes':
                $Q->negate();
            case 'includes':
                $teams = $agents = array();
                foreach ($value as $id => $ST) {
                    switch ($id[0]) {
                    case 'M':
                        $agents[] = $thisstaff->getId();
                        break;
                    case 's':
                        $agents[] = (int) substr($id, 1);
                        break;
                    case 'T':
                        $teams = array_merge($thisstaff->getTeams());
                        break;
                    case 't':
                        $teams[] = (int) substr($id, 1);
                        break;
                    }
                }
                $constraints = array();
                if ($teams)
                    $constraints['team_id__in'] = $teams;
                if ($agents)
                    $constraints['staff_id__in'] = $agents;
                $Q->add(Q::any($constraints));
            }
            return $Q;
        }
    
    
        function describeSearchMethod($method) {
            switch ($method) {
            case 'assigned':
                return __('assigned');
            case '!assigned':
                return __('unassigned');
            default:
                return parent::describeSearchMethod($method);
            }
        }
    
    }
    
    class TicketStateChoiceField extends ChoiceField {
    
        function getChoices($verbose=false) {
    
            return array(
                'open' => __('Open'),
                'closed' => __('Closed'),
    
                'archived' => _P('ticket state name', 'Archived'),
    
                'deleted' => _P('ticket state name','Deleted'),
    
            );
        }
    
        function getSearchMethods() {
            return array(
                'includes' =>   __('is'),
                '!includes' =>  __('is not'),
            );
        }
    
        function getSearchQ($method, $value, $name=false) {
            return parent::getSearchQ($method, $value, 'status__state');
        }
    }
    
    class TicketFlagChoiceField extends ChoiceField {
    
        function getChoices($verbose=false) {
    
            return array(
                'isanswered' =>   __('Answered'),
                'isoverdue' =>    __('Overdue'),
            );
        }
    
        function getSearchMethods() {
            return array(
                'includes' =>   __('is'),
                '!includes' =>  __('is not'),
            );
        }
    
        function getSearchQ($method, $value, $name=false) {
            $Q = new Q();
            if (isset($value['isanswered']))
                $Q->add(array('isanswered' => 1));
            if (isset($value['isoverdue']))
                $Q->add(array('isoverdue' => 1));
            if ($method == '!includes')
                $Q->negate();
            if ($Q->constraints)
                return $Q;
        }
    
    class TicketSourceChoiceField extends ChoiceField {
    
        function getChoices($verbose=false) {
    
                'web' => __('Web'),
                'email' => __('Email'),
                'phone' => __('Phone'),
                'api' => __('API'),
                'other' => __('Other'),
    
            );
        }
    
        function getSearchMethods() {
            return array(
                'includes' =>   __('is'),
                '!includes' =>  __('is not'),
            );
        }
    
        function getSearchQ($method, $value, $name=false) {
            return parent::getSearchQ($method, $value, 'source');
        }
    }
    
    
    class OpenClosedTicketStatusList extends TicketStatusList {
        function getItems($criteria=array()) {
            $rv = array();
            $base = parent::getItems($criteria);
            foreach ($base as $idx=>$S) {
                if (in_array($S->state, array('open', 'closed')))
                    $rv[$idx] = $S;
            }
            return $rv;
        }
    }
    
    class TicketStatusChoiceField extends SelectionField {
        static $widget = 'ChoicesWidget';
    
        function getList() {
    
            return new OpenClosedTicketStatusList(
    
                DynamicList::lookup(
                    array('type' => 'ticket-status'))
            );
        }
    
        function getSearchMethods() {
            return array(
    
                'includes' =>   __('is'),
                '!includes' =>  __('is not'),
    
    
        function getSearchQ($method, $value, $name=false) {
            $name = $name ?: $this->get('name');
            switch ($method) {
            case '!includes':
                return Q::not(array("{$name}__in" => array_keys($value)));
            case 'includes':
                return new Q(array("{$name}__in" => array_keys($value)));
            default:
                return parent::getSearchQ($method, $value, $name);
            }
        }