1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
 * Toggle Model Behavior
 *
 * Allows you to toggle default fields or a specific field/fields
 * Can also toggle a field on or off explicitly
 *
 * @package app.models.behaviors
 * @author Jose Diaz-Gonzalez
 * @version 1.2.2
 * @copyright Savant Dev
 **/
class ToggleBehavior extends ModelBehavior {
/**
 * Default settings for a model that has this behavior attached.
 *
 * @var array
 * @access protected
 */
    protected $_settings = array(
        'fields' => array('active')
    );
/**
 * Initiate behaviour for the model using settings.
 *
 * @param object $Model Model using the behaviour
 * @param array $settings Settings to override for model.
 * @access public
 */
    function setup(&$model, $config = array()) {
        $this->settings[$model->alias] = $this->__settings;
 
        //merge custom config with default settings
        $this->settings[$model->alias] = array_merge($this->settings[$model->alias], (array)$config);
    }
 
/**
 * Toggles the fields
 *
 * @return void
 * @author Jose Diaz-Gonzalez
 **/
    function toggle(&$model, $fieldNames = array(), $id) {
        $id = ($id != null) ? $id : $this->id;
        $id = (is_array($this->id)) ? $this->id[0] : $id;
 
        $errors = array();
        $fields = (!empty($fields)) ? $fieldNames : $this->settings[$model->alias]['fields'];
        $model->id = $id;
 
        foreach ($fields as $key => $field) {
            if(!$model->hasField($field)) {
                unset($fields[$key]);
                $errors[] = $field;
            }
        }
 
        foreach ($fields as $field) {
            $field = $this->escapeField($field);
            if(!$this->updateAll(array($field => 'NOT ' . $field), array($this->escapeField() => $id)) {
                $errors[] = $field;
            }
        }
        return $errors;
    }
 
/**
 * Toggles the fields on
 *
 * @return void
 * @author Jose Diaz-Gonzalez
 **/
    function turnOn(&$model, $fieldNames = array(), $id = null) {
        $id = ($id != null) ? $id : $this->id;
        $id = (is_array($this->id)) ? $this->id[0] : $id;
 
        $errors = array();
        $fields = (!empty($fields)) ? $fieldNames : $this->settings[$model->alias]['fields'];
        $model->id = $id;
 
        foreach ($fields as $field) {
            if ($model->hasField($field)) {
                $model->saveField($field, 1);
            } else {
                $errors[] = $field;
            }
        }
 
        return $errors;
    }
 
/**
 * Toggles the fields off
 *
 * @return array Errors on saving
 * @author Jose Diaz-Gonzalez
 **/
    function turnOff(&$model, $fieldNames = array(), $id = null) {
        $id = ($id != null) ? $id : $this->id;
        $id = (is_array($this->id)) ? $this->id[0] : $id;
 
        $errors = array();
        $fields = (!empty($fields)) ? $fieldNames : $this->settings[$model->alias]['fields'];
        $model->id = $id;
 
        foreach ($fields as $field) {
            if ($model->hasField($field)) {
                $model->saveField($field, 0);
            } else {
                $errors[] = field;
            }
        }
 
        return $errors;
    }
}
?>