Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
<?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.0
 * @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, $id, $fieldNames = array()) {
        if (!empty($fields)){
            $fields = $fieldNames;
        } else {
            $fields = $this->settings[$model->alias]['fields'];
        }
       
        foreach ($fields as $field) {
            $model->id = $id;
            $value = $model->field($field);
 
            $model->id = $id;
            if($value === 1) {
                $model->saveField($field, 0);
            } else {
                $model->saveField($field, 1);
            }
        }
    }
 
/**
 * Toggles the fields on
 *
 * @return void
 * @author Jose Diaz-Gonzalez
 **/
    function toggleOn(&$model, $id, $fieldNames = array()) {
        if (!empty($fields)){
            $fields = $fields;
        } else {
            $fields = $this->settings[$model->alias]['fields'];
        }
 
        foreach ($fields as $field) {
            $model->id = $id;
            $model->saveField($field, 1);
        }
    }
 
/**
 * Toggles the fields off
 *
 * @return void
 * @author Jose Diaz-Gonzalez
 **/
    function toggleOn(&$model, $id, $fieldNames = array()) {
        if (!empty($fields)){
            $fields = $fields;
        } else {
            $fields = $this->settings[$model->alias]['fields'];
        }
 
        foreach ($fields as $field) {
            $model->id = $id;
            $model->saveField($field, 0);
        }
    }
}
?>