deal with modelconfig
[tine20] / tine20 / Tinebase / Model / Filter / ExplicitRelatedRecord.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Tinebase
6  * @subpackage  Filter
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Alexander Stintzing <a.stintzing@metaways.de>
9  * @copyright   Copyright (c) 2012 Metaways Infosystems GmbH (http://www.metaways.de)
10  */
11
12 /**
13  * explicit related record filter definition
14  * 
15  * filtergroup definition:
16  * 
17  * 'contract' => array('filter' => 'Tinebase_Model_Filter_ExplicitRelatedRecord', 'options' => array(
18  *     'controller' => 'Sales_Controller_Contract',
19  *     'filtergroup' => 'Sales_Model_ContractFilter',
20  *     'own_filtergroup' => 'Timetracker_Model_TimeaccountFilter',
21  *     'own_controller' => 'Timetracker_Controller_Timeaccount',
22  *     'related_model' => 'Sales_Model_Contract',
23  * ))
24  * 
25  * @package     Tinebase
26  * @subpackage  Filter
27  */
28 class Tinebase_Model_Filter_ExplicitRelatedRecord extends Tinebase_Model_Filter_Relation
29 {
30     /**
31      * returns own ids defined by relation filter
32      *
33      * @param string $_modelName
34      * @return array
35      */
36     protected function _getOwnIds($_modelName)
37     {
38         if (! $this->_options['own_filtergroup']) {
39             throw new Tinebase_Exception_InvalidArgument('own filter group has to be defined!');
40         }
41         if (! $this->_options['own_controller']) {
42             throw new Tinebase_Exception_InvalidArgument('own controller has to be defined!');
43         }
44
45         $idProperty = 'id';
46         $filtergroup = $this->_options['own_filtergroup'];
47         $controller = $this->_options['own_controller'];
48
49         if (!($this->_value[0]['value'])) {
50             $relationFilter = new Tinebase_Model_RelationFilter(array(
51                 array('field' => 'own_model',     'operator' => 'equals', 'value' => $_modelName),
52                 array('field' => 'related_model', 'operator' => 'equals', 'value' => $this->_options['related_model']),
53             ));
54
55             $notInIds = Tinebase_Relations::getInstance()->search($relationFilter, NULL)->own_id;
56             $filter = new $filtergroup(array(
57
58             ),'AND');
59
60             // Deal with generic filtermodel!
61             if ($this->_options['own_filtergroup'] === Tinebase_Model_Filter_FilterGroup::class) {
62                 $filter->setConfiguredModel($_modelName);
63             }
64
65             $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => $idProperty, 'operator' => 'notin', 'value' => $notInIds)));
66
67             return $controller::getInstance()->search($filter, null, false, true);
68         }
69
70         return parent::_getOwnIds($_modelName);
71     }
72
73     /**
74      * (non-PHPdoc)
75      * @see Tinebase_Model_Filter_Relation::toArray()
76      */
77     public function toArray($_valueToJson = false)
78     {
79         $ret = parent::toArray($_valueToJson);
80         foreach($ret['value'] as &$filter) {
81             if ($filter['field'] == ':id' && $filter['operator'] == 'equals' && is_string($filter['value']) && strlen($filter['value']) == 40) {
82                 $split = explode('_Model_', $this->_options['related_model']);
83                 $cname = $split[0] . '_Controller_' . $split[1];
84                 $fr = $cname::getInstance()->get($filter['value'], /* $_containerId = */ null, /* $_getRelatedData = */ false);
85                 $fr->relations = null;
86                 $filter['value'] = $fr->toArray();
87             }
88         }
89         return $ret;
90     }
91 }