| 1 |
<?php |
| 2 |
|
| 3 |
require_once 'WhereClause.php'; |
| 4 |
class SmartFormsQuery |
| 5 |
{ |
| 6 |
private $formId; |
| 7 |
public $selectColumns; |
| 8 |
public $fieldDictionary; |
| 9 |
|
| 10 |
/** |
| 11 |
* @var WhereClause[] |
| 12 |
*/ |
| 13 |
private $conditions; |
| 14 |
private $orderByColumns; |
| 15 |
|
| 16 |
public function __construct($formId) |
| 17 |
{ |
| 18 |
$this->formId=$formId; |
| 19 |
$this->selectColumns=array(); |
| 20 |
$this->conditions=array(); |
| 21 |
$this->fieldDictionary=$this->GetFormDictionary(); |
| 22 |
} |
| 23 |
|
| 24 |
public function StoresInformation($formelement) |
| 25 |
{ |
| 26 |
switch($formelement["ClassName"]) |
| 27 |
{ |
| 28 |
case "rednaotextinput": |
| 29 |
case 'rednaorating': |
| 30 |
case "rednaoprependedtext": |
| 31 |
case "rednaoappendedtext": |
| 32 |
case "rednaoemail": |
| 33 |
case "rednaoprependedcheckbox": |
| 34 |
case "rednaoappendedcheckbox": |
| 35 |
case "rednaotextarea": |
| 36 |
case "rednaomultipleradios": |
| 37 |
case 'rednaomultiplecheckboxes': |
| 38 |
case "rednaosearchablelist": |
| 39 |
case "rednaoimagepicker": |
| 40 |
case "rednaoselectbasic": |
| 41 |
case "rednaoname": |
| 42 |
case "rednaoaddress": |
| 43 |
case "rednaophone": |
| 44 |
case "rednaodonationrecurrence": |
| 45 |
case "sfFileUpload": |
| 46 |
case "rednaoimageupload": |
| 47 |
case "rednaodatepicker": |
| 48 |
case "rednaosurveytable": |
| 49 |
case "rednaosignature": |
| 50 |
case "rednaorepeater": |
| 51 |
case "rednaonumber": |
| 52 |
case 'rednaocurrency': |
| 53 |
case 'rednaotimepicker': |
| 54 |
return true; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
public function AddField($fieldId) |
| 62 |
{ |
| 63 |
if(!isset($this->fieldDictionary[$fieldId])) |
| 64 |
{ |
| 65 |
echo "Invalid field ".$fieldId; |
| 66 |
return; |
| 67 |
} |
| 68 |
array_push($this->selectColumns,$fieldId); |
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param $fieldList array |
| 74 |
*/ |
| 75 |
public function AddFields($fieldList){ |
| 76 |
|
| 77 |
foreach($fieldList as $field) |
| 78 |
$this->AddField($field); |
| 79 |
} |
| 80 |
|
| 81 |
public function AddCondition($field,$comparison,$value,$join='and'){ |
| 82 |
$whereCondition=''; |
| 83 |
if(isset($this->fieldDictionary[$field])) |
| 84 |
{ |
| 85 |
$whereCondition=new WhereClause($this->fieldDictionary[$field],$comparison,$value,$join); |
| 86 |
|
| 87 |
}else |
| 88 |
|
| 89 |
if($field=='_FormId'||$field=='_SFTimeStamp'||$field=='_UserId'||$field=='_Date') |
| 90 |
{ |
| 91 |
$whereCondition=new WhereClause(array('ClassName'=>'','Id'=>$field),$comparison,$value,$join); |
| 92 |
}else{ |
| 93 |
echo "Invalid field ".$field; |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
$this->conditions[]=$whereCondition; |
| 98 |
return $whereCondition; |
| 99 |
|
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
public function AddConditions($conditionList){ |
| 104 |
for($i=0;$i<count($conditionList);$i++) |
| 105 |
{ |
| 106 |
$whereClause=$this->AddCondition($conditionList[$i]['field'],$conditionList[$i]['comparison'],$conditionList[$i]['value']); |
| 107 |
if($i==0) |
| 108 |
$whereClause->openParentheses=true; |
| 109 |
if($i==count($conditionList)-1) |
| 110 |
$whereClause->closeParentheses=true; |
| 111 |
$this->conditions[]=$whereClause; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function GetResults($limit='',$skip='',$format='html'){ |
| 116 |
if(count($this->selectColumns)==0) |
| 117 |
{ |
| 118 |
$this->selectColumns[]='_Date'; |
| 119 |
foreach($this->fieldDictionary as $field) |
| 120 |
{ |
| 121 |
if($this->StoresInformation($field)) |
| 122 |
{ |
| 123 |
$this->selectColumns[]=$field['Id']; |
| 124 |
} |
| 125 |
} |
| 126 |
$this->selectColumns[]='_UserName'; |
| 127 |
} |
| 128 |
return $this->Execute('',$limit,$skip,$format); |
| 129 |
} |
| 130 |
|
| 131 |
public function GetRaw($limit='',$skip=''){ |
| 132 |
return $this->Execute('parent.*',$limit,$skip); |
| 133 |
} |
| 134 |
|
| 135 |
public function GetCount(){ |
| 136 |
$result=$this->Execute("count(*) count"); |
| 137 |
if(count($result)==0) |
| 138 |
return null; |
| 139 |
return $result[0]['count']; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function GetScalar(){ |
| 145 |
$result=$this->Execute(); |
| 146 |
if(count($result)==0) |
| 147 |
return null; |
| 148 |
return array_pop($result[0]); |
| 149 |
} |
| 150 |
|
| 151 |
private function Execute($columns='',$limit='',$skip='',$format='html'){ |
| 152 |
$useDefault=false; |
| 153 |
if($columns=='') |
| 154 |
{ |
| 155 |
$useDefault=true; |
| 156 |
$columns='data,date,entry_id,form_id'; |
| 157 |
} |
| 158 |
global $wpdb; |
| 159 |
$params=array(); |
| 160 |
$params[]=$this->formId; |
| 161 |
$conditionText="SELECT $columns,parent.ip user_ip, user.display_name user_name,user.ID user_id FROM ".SMART_FORMS_ENTRY." parent |
| 162 |
left join ".$wpdb->users ." user on user.ID=parent.user_id where form_id=%d "; |
| 163 |
|
| 164 |
|
| 165 |
if(count($this->conditions)>0) |
| 166 |
{ |
| 167 |
|
| 168 |
foreach ($this->conditions as $condition) |
| 169 |
{ |
| 170 |
$conditionText .=$condition->GetText(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$conditionText.=' order by parent.date desc '; |
| 175 |
if($limit!=''&&$limit!=0) |
| 176 |
{ |
| 177 |
$conditionText.=' limit '.$wpdb->prepare('%d',$limit); |
| 178 |
} |
| 179 |
|
| 180 |
if($skip!='') |
| 181 |
{ |
| 182 |
$conditionText.=' offset '.$wpdb->prepare('%d',$skip); |
| 183 |
} |
| 184 |
|
| 185 |
$conditionText=$wpdb->prepare($conditionText,$params); |
| 186 |
$result=$wpdb->get_results($conditionText,'ARRAY_A'); |
| 187 |
|
| 188 |
if($useDefault) |
| 189 |
{ |
| 190 |
return $this->ProcessResult($result,$format); |
| 191 |
}else{ |
| 192 |
return $result; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
protected function GetFormDictionary() |
| 200 |
{ |
| 201 |
global $wpdb; |
| 202 |
|
| 203 |
/** @noinspection PhpUndefinedMethodInspection */ |
| 204 |
$result=$wpdb->get_results($wpdb->prepare("select element_options from ".SMART_FORMS_TABLE_NAME." where form_id=%d",$this->formId)); |
| 205 |
if($result===false||count($result)==0) |
| 206 |
throw new Exception("Couldn't get form information"); |
| 207 |
|
| 208 |
$formInfo=json_decode($result[0]->element_options,true); |
| 209 |
|
| 210 |
if($formInfo==null) |
| 211 |
throw new Exception("Couldn't get form information"); |
| 212 |
|
| 213 |
/** @noinspection PhpIncludeInspection */ |
| 214 |
include_once(SMART_FORMS_DIR.'string_renderer/rednao_string_builder.php'); |
| 215 |
$StringBuilder=new rednao_string_builder(); |
| 216 |
|
| 217 |
$formElementsDictionary =array(); |
| 218 |
foreach($formInfo as $element) |
| 219 |
{ |
| 220 |
$element["__renderer"]=$StringBuilder->GetElementRenderer($element); |
| 221 |
$formElementsDictionary[$element["Id"]]=$element; |
| 222 |
} |
| 223 |
|
| 224 |
return $formElementsDictionary; |
| 225 |
} |
| 226 |
|
| 227 |
private function ProcessResult($rows,$format) |
| 228 |
{ |
| 229 |
$result=array(); |
| 230 |
foreach($rows as $row) |
| 231 |
{ |
| 232 |
$entryData=array(); |
| 233 |
|
| 234 |
$values = json_decode($row['data'],true); |
| 235 |
foreach ($this->selectColumns as $fieldId) |
| 236 |
{ |
| 237 |
include_once(SMART_FORMS_DIR . 'string_renderer/rednao_string_builder.php'); |
| 238 |
$stringBuilder = new rednao_string_builder(); |
| 239 |
if($fieldId=='_Date') |
| 240 |
{ |
| 241 |
$entryData['_Date']=$row['date']; |
| 242 |
continue; |
| 243 |
} |
| 244 |
|
| 245 |
if($fieldId=='_UserName') |
| 246 |
{ |
| 247 |
$entryData['_UserName']=$row['user_name']; |
| 248 |
continue; |
| 249 |
} |
| 250 |
$htmlValue=''; |
| 251 |
$rawValue=null; |
| 252 |
$plainValue=''; |
| 253 |
if(isset($values[$fieldId])) |
| 254 |
{ |
| 255 |
$htmlValue = $stringBuilder->GetStringFromColumn($this->fieldDictionary[$fieldId], $values[$fieldId]); |
| 256 |
$plainValue=$stringBuilder->GetPlainFromColumn($this->fieldDictionary[$fieldId], $values[$fieldId]); |
| 257 |
$rawValue=$values[$fieldId]; |
| 258 |
} |
| 259 |
|
| 260 |
if($format=='html') |
| 261 |
$entryData[$fieldId]=$htmlValue; |
| 262 |
else{ |
| 263 |
$entryData[$fieldId]=array( |
| 264 |
'value'=>$htmlValue, |
| 265 |
'raw'=>$rawValue, |
| 266 |
'plain'=>$plainValue |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
$result[]=$entryData; |
| 272 |
} |
| 273 |
|
| 274 |
return $result; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|