OSDN Git Service

Collectionableプラグイン導入
[trpgtools-onweb/cake-frame.git] / app / plugins / collectionable / models / behaviors / options.php
1 <?php
2
3 class OptionsBehavior extends ModelBehavior {
4         
5         var $settings = array();
6         var $defaultSettings = array(
7                 'setupProperty' => true,
8                 'defaultOption' => false,
9                 'optionName' => 'options',
10         );
11
12         var $defaultQuery = array(
13                 'conditions' => null, 'fields' => null, 'joins' => array(), 'limit' => null,
14                 'offset' => null, 'order' => null, 'page' => null, 'group' => null, 'callbacks' => true
15         );
16
17         function setup(&$Model, $settings = array()) {
18                 $this->settings = array_merge($this->defaultSettings, (array)$settings);
19                 $optionName = $this->settings['optionName'];
20                 if ($this->settings['setupProperty']) {
21                         if (empty($Model->{$optionName})) {
22                                 $Model->{$optionName} = array();
23                         }
24                         if (empty($Model->defaultOption)) {
25                                 $Model->defaultOption = $this->settings['defaultOption'];
26                         }
27                 }
28                 return true;
29         }
30
31         function beforeFind(&$Model, $query = array()) {
32                 if (isset($query['options'])) {
33                         $options = $query['options'];
34                         unset($query['options']);
35
36                         $query = Set::merge($this->defaultQuery, $this->options($Model, $options), Set::filter($query));
37                 }
38                 return $query;
39         }
40
41         function options(&$Model, $type = null){
42                 $args = func_get_args();
43                 if (func_num_args() > 2) {
44                         array_shift($args);
45                         $type = $args;
46                 }
47
48                 $option = array();
49                 if (is_array($type)) {
50                         foreach ($type as $t) {
51                                 $option = Set::merge($option, $this->options($Model, $t));
52                         }
53                 } else {
54                         $optionName = $this->settings['optionName'];
55                         $option = isset($Model->{$optionName}[$type]) ? $Model->{$optionName}[$type] : array();
56                         $default = array();
57                         if ($Model->defaultOption) {
58                                 $default = $this->_getDefault($Model->defaultOption, $Model->{$optionName});
59                         }
60                         $options = array();
61                         if (isset($option[$optionName]) && !empty($option[$optionName])) {
62                                 $options = $this->_intelligentlyMerge(array(), $option[$optionName], $Model->{$optionName});
63                                 unset($option['options']);
64                         }
65                         $option = Set::merge($default, $options, $option);
66                 }
67                 return $option;
68         }
69
70         function _getDefault($defaultOption, $options) {
71                 $default = array();
72                 if ($defaultOption === true && !empty($options['default'])) {
73                         $default = $options['default'];
74                 } elseif (is_array($defaultOption)) {
75                         $default = $this->_intelligentlyMerge($default, $defaultOption, $options);
76                 } elseif (!empty($options[$defaultOption])) {
77                         $default = $this->_intelligentlyMerge($default, $options[$defaultOption], $options);
78                 }
79                 return $default;
80         }
81
82         function _intelligentlyMerge($data, $merges, $options) {
83                 $merges = (array)$merges;
84                 if (Set::numeric(array_keys($merges))) {
85                         foreach($merges as $merge) {
86                                 if (!empty($options[$merge])) {
87                                         $data = $this->_intelligentlyMerge($data, $options[$merge], $options);
88                                 }
89                         }
90                 } else {
91                         $optionName = $this->settings['optionName'];
92                         if (array_key_exists($optionName, $merges)) {
93                                 $data = $this->_intelligentlyMerge($data, $merges[$optionName], $options);
94                                 unset($merges[$optionName]);
95                         }
96                         $data = Set::merge($data, $merges);
97                 }
98                 return $data;
99         }
100 }