PlugincsSettingForm.class.php 5.02 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

/**
 * PlugincsSetting form.
 *
 * @package    form
 * @subpackage csSetting
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
abstract class PlugincsSettingForm extends BasecsSettingForm
{
  public function getSettingWidget()
  {
    $type = $this->getObject()->getType();
    $name = $this->getObject()->getName();
    
    // See if there is a widget specific to this setting
    $method = 'get'.sfInflector::camelize($name).'SettingWidget';
    if (method_exists($this, $method))
    {
      return $this->$method();
    }
    // Else, see if there is a widget specific to this setting's type
    $method = 'get'.sfInflector::camelize($type).'SettingWidget';
    if (method_exists($this, $method))
    {
      return $this->$method();
    }
    // Return a generic Widget
      
    return new sfWidgetFormInput(array('type' => $type), $this->getObject()->getOptionsArray());
  }

  public function getSettingValidator()
  {
    $type = $this->getObject()->getType();
    $name = $this->getObject()->getName();
    // See if there is a validator specific to this setting
    $method = 'get'.sfInflector::camelize($name).'SettingValidator';
    if (method_exists($this, $method))
    {
      return $this->$method();
    }
    // Else, see if there is a validator specific to this setting's type
    $method = 'get'.sfInflector::camelize($type).'SettingValidator';
    if (method_exists($this, $method))
    {
      return $this->$method();
    }
    // Return a generic Validator    
    return new sfValidatorString(array('required' => false));
  }

  public function getRichTextSettingWidget()
  {
    if(class_exists('sfWidgetFormCKEditor'))
     return new sfWidgetFormCKEditor(array(), $this->getObject()->getOptionsArray());
    else
      return new sfWidgetFormTextarea(array(), $this->getObject()->getOptionsArray());
  }
  
  //Type Textarea
  public function getTextareaSettingWidget()
  {
    return new sfWidgetFormTextarea(array(), $this->getObject()->getOptionsArray());
  }
  
  // Type Checkbox
  public function getCheckboxSettingWidget()
  {
    return new sfWidgetFormInputCheckbox(array(), $this->getObject()->getOptionsArray());
  }

  // Type Date
  public function getDateTimeSettingWidget()
  {
    return new sfWidgetFormDateTime($this->getObject()->getOptionsArray());
  }
  public function getDateTimeSettingValidator()
  {
    return new sfValidatorDateTime(array('required' => false));
  }

  // Type Time
  public function getTimeSettingWidget()
  {
      $options = $this->getObject()->getOptionsArray();

      if($default = $this->getObject()->getSettingDefault()){
          $default_arr = explode(':', $default);
          if(count($default_arr) == 3){
              $options['empty_values'] = array('hour' => $default_arr[0], 'minute' => $default_arr[1], 'second' => $default_arr[2]);
          }
      }

    return new sfWidgetFormTime($options);
  }
  public function getTimeSettingValidator()
  {
    return new sfValidatorTime(array('required' => true));
  }
  
  // Type Yesno
  public function getYesnoSettingWidget()
  {
    return new sfWidgetFormSelectRadio(array('choices' => array('1' => 'Yes', '0' => 'No')), $this->getObject()->getOptionsArray());
  }
  public function getYesnoSettingValidator()
  {
    return new sfValidatorChoice(array('choices' => array('1', '0'), 'required' => false));
  }
  
  //Type Select List
  public function getSelectSettingWidget()
  {
    return new sfWidgetFormSelect(array('choices' => $this->getObject()->getOptionsArray(), 'required' => false));
  }
  public function getSelectSettingValidator()
  {
    return new sfValidatorChoice(array('choices' => $this->getObject()->getOptionsArray(), 'required' => false));
  }
  
  //Type Model
  public function getModelSettingWidget()
  {
    return new sfWidgetFormDoctrineChoice($this->getObject()->getOptionsArray());
  }
  public function getModelSettingValidator()
  {
    return new sfValidatorDoctrineChoice($this->getObject()->getOptionsArray());
  }
  
  //Type Upload
  public function getUploadSettingWidget()
  {
    $path = $this->getObject()->getUploadPath() . '/' . $this->getObject()->getValue();
    $options = array(
          'file_src' => $this->getObject()->getValue(),
          'template' => "<a href='/$path'>%file%</a><br />%input%<br />%delete% %delete_label%",
      );
    
    // If you want to pass the widget custom settings, you can override in your setting's options  
    $options = array_merge($options, $this->getObject()->getOptionsArray());
    
    return new sfWidgetFormInputFileEditable($options);
  }
  
  // Overriding Bind in this case allows us to have the form field "setting_group_new" for usability
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    $taintedValues['setting_group'] = (isset($taintedValues['setting_group_new']) && $taintedValues['setting_group_new']) ?  $taintedValues['setting_group_new'] : $taintedValues['setting_group'];
    unset($taintedValues['setting_group_new']);
    $ret = parent::bind($taintedValues, $taintedFiles);
    return $ret;
  }
}