PlugincsSetting.class.php 1.99 KB
Newer Older
Яков's avatar
first  
Яков committed
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
<?php
/*
 * Plugin class
 *
 * auto-generated by the sfDoctrine plugin
 * creation date: 2007-05-02 13:45:58
 */
abstract class PlugincsSetting extends BasecsSetting
{
  // Get a value from the options array
  public function getOption($name, $required = false)
  {
    $config = $this->getOptionsArray();
    
    if ($required && !isset($config[$name])) 
    {
      throw new sfException(sprintf('Missing required option "%s" in setting %s', $name, $this['name']));
    }
    
    return isset($config[$name]) ? $config[$name] : false;
  }
  
  // convert the options text area to an array
  public function getOptionsArray()
  {
    return sfToolkit::stringToArray($this->getWidgetOptions());
  }
  
  // path to uploaded files
  public function getUploadPath()
  {
    if ($this['type'] != 'upload') 
    {
      throw new sfException(sprintf('Cannot get Upload Path for setting of type "%s"', $this['type']));
    }
    
    $default_path = csSettings::getDefaultUploadPath();
        
    $target_path = $this->getOption('upload_path');
    
    return $target_path ? $target_path : $default_path;
  }
  
  // shortcut for getting the setting group (field name 'group' is reserved)
  public function getGroup()
  {
    return $this['setting_group'];
  }
  
  // remove cache when an item is updated
  public function postSave($event)
  {
    csSettings::clearSettingsCache();
  }
  
  public function postDelete($event)
  {
    csSettings::clearSettingsCache();
  }
  
  // Ensures default value is set when object is saved without a value
  public function preInsert($event)
  {
    if ($this['setting_default'] && !$this['value']) 
    {
      $this['value'] = $this['setting_default'];
    }
  }
  
  public function getValue()
  {
    $value = $this->_get('value');
    switch ($this['type']) 
    {
      case 'checkbox':
        if ($value == '') 
        {
          // Cast as boolean
          return false;
        }
        break;
      case 'yesno':
        return (bool) $value;
    }
    
    return $value;
  }
}