Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Settings.php
1 <?php
2 
3 /**
4  * This is the model class for table "settings".
5  *
6  * The followings are the available columns in table 'settings':
7  * @property integer $id
8  * @property string $settings
9  */
10 class Settings extends CActiveRecord
11 {
12  private static $_instance;
13 
14  /**
15  * Returns the static model of the specified AR class.
16  *
17  * @param string $className active record class name.
18  *
19  * @return Settings the static model class
20  */
21  public static function model($className = __CLASS__)
22  {
23  return parent::model($className);
24  }
25 
26  /**
27  * Returns model table name
28  *
29  * @return string the associated database table name
30  */
31  public function tableName()
32  {
33  return '{{settings}}';
34  }
35 
36  /**
37  * Sets some rules
38  *
39  * @return array validation rules for model attributes.
40  */
41  public function rules()
42  {
43  // NOTE: you should only define rules for those attributes that
44  // will receive user inputs.
45  return array(
46  array('settings', 'required'),
47  // The following rule is used by search().
48  // Please remove those attributes that should not be searched.
49  array('id, settings', 'safe', 'on' => 'search'),
50  );
51  }
52 
53  /**
54  * Allows model relations definition
55  *
56  * @return array relational rules.
57  */
58  public function relations()
59  {
60  // NOTE: you may need to adjust the relation name and the related
61  // class name for the relations automatically generated below.
62  return array(
63  );
64  }
65 
66  /**
67  * Singleton method
68  *
69  * @return Settings.
70  */
71  public static function instance()
72  {
73  if (!self::$_instance) {
74  $settings = Settings::model()->find();
75  if (is_null($settings)) {
76  $settings = new Settings();
77  $settings->settings = array();
78  $settings->save();
79  }
80  self::$_instance = $settings;
81  }
82  return self::$_instance;
83  }
84 
85  /**
86  * resets model instance
87  *
88  * @return void
89  */
90  public static function reload()
91  {
92  self::$_instance = null;
93  return self::instance();
94  }
95 
96  /**
97  * Returns setting by given name
98  *
99  * @param string name settings parameter name
100  *
101  * @return mixed
102  */
103  public function get($name)
104  {
105  return isset($this->settings[$name]) ? $this->settings[$name] : false;
106  }
107 
108  /**
109  * Settings setter
110  *
111  * @param string $name name
112  * @param mixed $value value
113  *
114  * @return void
115  */
116  public function set($name, $value)
117  {
118  $this->settings = array_merge($this->settings, array($name => $value));
119  $this->save();
120  }
121 
122  /**
123  * Deletes parameter by name
124  *
125  * @param string $name name
126  *
127  * @return void
128  */
129  public function unsetValue($name)
130  {
131  $s = $this->settings;
132  unset($s[$name]);
133  $this->settings = $s;
134  $this->save();
135  }
136 
137  /**
138  * Performes some action before model is saved
139  *
140  * @return mixed
141  */
142  protected function beforeSave()
143  {
144  $this->settings = serialize($this->settings);
145  return parent::beforeSave();
146  }
147 
148  /**
149  * Performes some action after model is saved
150  *
151  * @return mixed
152  */
153  protected function afterSave()
154  {
155  $this->settings = unserialize($this->settings);
157  }
158 
159  /**
160  * Performes some action before model is found
161  *
162  * @return mixed
163  */
164  protected function afterFind()
165  {
166  $this->settings = unserialize($this->settings);
168  }
169 
170  /**
171  * Returns customized attribute labels (name=>label)
172  *
173  * @return array
174  */
175  public function attributeLabels()
176  {
177  return array(
178  'id' => 'ID',
179  'settings' => 'Settings',
180  );
181  }
182 
183 }