1<?php 2 3class ohrmReportWidgetServicePeriod extends sfWidgetForm implements ohrmEnhancedEmbeddableWidget { 4 use ohrmWidgetTrait; 5 6 private $whereClauseCondition; 7 private $id; 8 private $conditionMap = array(1 => '<', 2 => '>', 3 => 'BETWEEN'); 9 10 public function configure($options = array(), $attributes = array()) { 11 12 $this->id = $attributes['id']; 13 14 $choices = array( 15 '' => '-- ' . __('Select') . ' --', 16 '1' => __('Less Than'), 17 '2' => __('Greater Than'), 18 '3' => __('Range') 19 ); 20 21 $this->addOption($this->id . '_' . 'comparision', new sfWidgetFormChoice(array('choices' => $choices))); 22 $this->addOption($this->id . '_' . 'value1', new sfWidgetFormInputText($options, array('size' => 10))); 23 $this->addOption($this->id . '_' . 'value2', new sfWidgetFormInputText($options, array('size' => 10))); 24 25 26 $this->addOption('template', '%comparision%        %value1%        %value2%'); 27 } 28 29 public function render($name, $value = null, $attributes = array(), $errors = array()) { 30 $values = array_merge(array('value1' => '', 'value2' => '', 'comparision' => ''), is_array($value) ? $value : array()); 31 32 $html = strtr($this->translate($this->getOption('template')), array( 33 '%comparision%' => $this->getOption($this->attributes['id'] . '_' . 'comparision')->render($name . '[comparision]', $values['comparision'], array('id' => $this->attributes['id'] . '_' . 'comparision')), 34 '%value1%' => $this->getOption($this->attributes['id'] . '_' . 'value1')->render($name . '[value1]', $values['value1'], array('id' => $this->attributes['id'] . '_' . 'value1')), 35 '%value2%' => $this->getOption($this->attributes['id'] . '_' . 'value2')->render($name . '[value2]', $values['value2'], array('id' => $this->attributes['id'] . '_' . 'value2')), 36 )); 37 38 $javaScript = $javaScript = sprintf(<<<EOF 39 <script type="text/javascript"> 40 41$(document).ready(function() { 42 43 if($('#%s' + '_comparision').val() == ''){ 44 $('#%s' + '_value1').hide().val(''); 45 $('#%s' + '_value2').hide().val(''); 46 } 47 48 $('#%s' + '_comparision').change(function(){ 49 if($('#%s' + '_comparision').val() == ''){ 50 $('#%s' + '_value1').hide().val(''); 51 $('#%s' + '_value2').hide().val(''); 52 }else if($('#%s' + '_comparision').val() == '1'){ 53 getHints('#%s' + '_value1','%s'); 54 $('#%s' + '_value1').show(); 55 $('#%s' + '_value2').hide().val(''); 56 }else if($('#%s' + '_comparision').val() == '2'){ 57 getHints('#%s' + '_value1','%s'); 58 $('#%s' + '_value1').show(); 59 $('#%s' + '_value2').hide().val(''); 60 }else if($('#%s' + '_comparision').val() == '3'){ 61 getHints('#%s' + '_value1','%s'); 62 getHints('#%s' + '_value2','%s'); 63 $('#%s' + '_value1').show(); 64 $('#%s' + '_value2').show(); 65 } 66 }); 67 68 $('#%s' + '_comparision').trigger('change'); 69 }); 70 71function getHints( id , message ){ 72 if ($(id).val() == ''){ 73 $(id).val(message) 74 .addClass("inputFormatHint"); 75 } 76 77 $(id).one('focus', function() { 78 if ($(this).hasClass("inputFormatHint")) { 79 $(this).val(""); 80 $(this).removeClass("inputFormatHint"); 81 } 82 }); 83} 84 85 86 </script> 87EOF 88 , 89 $this->attributes['id'], 90 $this->attributes['id'], 91 $this->attributes['id'], 92 $this->attributes['id'], 93 $this->attributes['id'], 94 $this->attributes['id'], 95 $this->attributes['id'], 96 $this->attributes['id'], 97 $this->attributes['id'], 98 __js('Type in years') . '..', 99 $this->attributes['id'], 100 $this->attributes['id'], 101 $this->attributes['id'], 102 $this->attributes['id'], 103 __js('Type in years') . '..', 104 $this->attributes['id'], 105 $this->attributes['id'], 106 $this->attributes['id'], 107 $this->attributes['id'], 108 __js('Type in years') . '..', 109 $this->attributes['id'], 110 __js('Type in years') . '..', 111 $this->attributes['id'], 112 $this->attributes['id'], 113 $this->attributes['id']); 114 115 return $html . $javaScript; 116 } 117 118 /** 119 * Embeds this widget into the form. Sets label and validator for this widget. 120 * @param sfForm $form 121 */ 122 public function embedWidgetIntoForm(sfForm &$form) { 123 124 125 $widgetSchema = $form->getWidgetSchema(); 126 $validatorSchema = $form->getValidatorSchema(); 127 128 $widgetSchema[$this->attributes['id']] = $this; 129 $widgetSchema[$this->attributes['id']]->setLabel(ucwords(str_replace("_", " ", $this->attributes['id']))); 130 131 $requiredMessage = __(ValidationMessages::REQUIRED); 132 $validatorSchema[$this->attributes['id']] = new ohrmValidatorConditionalFilter(array(), array('required' => $requiredMessage)); 133 134//$form->setValidator('date_period', new sfValidatorString()); 135// $validatorSchema[$this->attributes['id']] = new ohrmValidatorDateRange(array(), array("invalid" => "Insert a correct date")); 136// $validatorSchema[$this->attributes['id']] = new sfValidatorPass(); 137// $validatorSchema->setPostValidator(new ohrmValidatorSchemaDateRange($this->attributes['id'], ohrmValidatorSchemaDateRange::LESS_THAN_EQUAL, $this->attributes['id'], 138// array('throw_global_error' => true), 139// array('invalid' => 'The from date ("%left_field%") must be before the to date ("%right_field%")') 140// )); 141 } 142 143 /** 144 * Sets whereClauseCondition. 145 * @param string $condition 146 */ 147 public function setWhereClauseCondition($condition) { 148 149 if (isset($this->conditionMap[$condition])) { 150 $this->whereClauseCondition = $this->conditionMap[$condition]; 151 } 152 } 153 154 /** 155 * Gets whereClauseCondition. ( if whereClauseCondition is set returns that, else returns default condition ) 156 * @return string ( a condition ) 157 */ 158 public function getWhereClauseCondition() { 159 160 if (isset($this->whereClauseCondition)) { 161 $setCondition = $this->whereClauseCondition; 162 return $setCondition; 163 } else { 164 $defaultCondition = "="; 165 return $defaultCondition; 166 } 167 } 168 169 /** 170 * This method generates the where clause part. 171 * @param string $fieldName 172 * @param string $value 173 * @return string 174 */ 175 public function generateWhereClausePart($fieldName, $value) { 176 $condition = $this->getWhereClauseCondition(); 177 178 if($condition == '<'){ 179 return $whereClausePart = $fieldName . " " . $this->getWhereClauseCondition() . " " . $this->getEscapedString($value['value1']); 180 }else if($condition == '>'){ 181 return $whereClausePart = $fieldName . " " . $this->getWhereClauseCondition() . " " . $this->getEscapedString($value['value1']); 182 }else if($condition == 'BETWEEN'){ 183 return "( " . $fieldName . " " . $this->getWhereClauseCondition() . " " . $this->getEscapedString($value['value1']) . " AND " . $this->getEscapedString($value['value2']) . " )"; 184 } 185 186 return null; 187 } 188 189 public function getDefaultValue(SelectedFilterField $selectedFilterField) { 190 191 $condition = ''; 192 193 if (!empty($selectedFilterField->whereCondition)) { 194 $condition = array_search($selectedFilterField->whereCondition, $this->conditionMap); 195 } 196 197 $values = array('value1' => $selectedFilterField->value1, 198 'value2' => $selectedFilterField->value2, 199 'comparision' => $condition); 200 201 return $values; 202 } 203} 204 205