[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /*+******************************************************************************* 3 * The contents of this file are subject to the vtiger CRM Public License Version 1.0 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 ******************************************************************************/ 10 require_once 'includes/runtime/Cache.php'; 11 class WebserviceField{ 12 private $fieldId; 13 private $uitype; 14 private $blockId; 15 private $blockName; 16 private $nullable; 17 private $default; 18 private $tableName; 19 private $columnName; 20 private $fieldName; 21 private $fieldLabel; 22 private $editable; 23 private $fieldType; 24 private $displayType; 25 private $mandatory; 26 private $massEditable; 27 private $tabid; 28 private $presence; 29 /** 30 * 31 * @var PearDatabase 32 */ 33 private $pearDB; 34 private $typeOfData; 35 private $fieldDataType; 36 private $dataFromMeta; 37 private static $tableMeta = array(); 38 private static $fieldTypeMapping = array(); 39 private $referenceList; 40 private $defaultValuePresent; 41 private $explicitDefaultValue; 42 43 private $genericUIType = 10; 44 45 private $readOnly = 0; 46 47 private function __construct($adb,$row){ 48 $this->uitype = $row['uitype']; 49 $this->blockId = $row['block']; 50 $this->blockName = null; 51 $this->tableName = $row['tablename']; 52 $this->columnName = $row['columnname']; 53 $this->fieldName = $row['fieldname']; 54 $this->fieldLabel = $row['fieldlabel']; 55 $this->displayType = $row['displaytype']; 56 $this->massEditable = ($row['masseditable'] === '1')? true: false; 57 $typeOfData = $row['typeofdata']; 58 $this->presence = $row['presence']; 59 $this->typeOfData = $typeOfData; 60 $typeOfData = explode("~",$typeOfData); 61 $this->mandatory = ($typeOfData[1] == 'M')? true: false; 62 if($this->uitype == 4){ 63 $this->mandatory = false; 64 } 65 $this->fieldType = $typeOfData[0]; 66 $this->tabid = $row['tabid']; 67 $this->fieldId = $row['fieldid']; 68 $this->pearDB = $adb; 69 $this->fieldDataType = null; 70 $this->dataFromMeta = false; 71 $this->defaultValuePresent = false; 72 $this->referenceList = null; 73 $this->explicitDefaultValue = false; 74 75 $this->readOnly = (isset($row['readonly']))? $row['readonly'] : 0; 76 77 if(array_key_exists('defaultvalue', $row)) { 78 $this->setDefault($row['defaultvalue']); 79 } 80 } 81 82 public static function fromQueryResult($adb,$result,$rowNumber){ 83 return new WebserviceField($adb,$adb->query_result_rowdata($result,$rowNumber)); 84 } 85 86 public static function fromArray($adb,$row){ 87 return new WebserviceField($adb,$row); 88 } 89 90 public function getTableName(){ 91 return $this->tableName; 92 } 93 94 public function getFieldName(){ 95 return $this->fieldName; 96 } 97 98 public function getFieldLabelKey(){ 99 return $this->fieldLabel; 100 } 101 102 public function getFieldType(){ 103 return $this->fieldType; 104 } 105 106 public function isMandatory(){ 107 return $this->mandatory; 108 } 109 110 public function getTypeOfData(){ 111 return $this->typeOfData; 112 } 113 114 public function getDisplayType(){ 115 return $this->displayType; 116 } 117 118 public function getMassEditable(){ 119 return $this->massEditable; 120 } 121 122 public function getFieldId(){ 123 return $this->fieldId; 124 } 125 126 public function getDefault(){ 127 if($this->dataFromMeta !== true && $this->explicitDefaultValue !== true){ 128 $this->fillColumnMeta(); 129 } 130 return $this->default; 131 } 132 133 public function getColumnName(){ 134 return $this->columnName; 135 } 136 137 public function getBlockId(){ 138 return $this->blockId; 139 } 140 141 public function getBlockName(){ 142 if(empty($this->blockName)) { 143 $this->blockName = getBlockName($this->blockId); 144 } 145 return $this->blockName; 146 } 147 148 public function getTabId(){ 149 return $this->tabid; 150 } 151 152 public function isNullable(){ 153 if($this->dataFromMeta !== true){ 154 $this->fillColumnMeta(); 155 } 156 return $this->nullable; 157 } 158 159 public function hasDefault(){ 160 if($this->dataFromMeta !== true && $this->explicitDefaultValue !== true){ 161 $this->fillColumnMeta(); 162 } 163 return $this->defaultValuePresent; 164 } 165 166 public function getUIType(){ 167 return $this->uitype; 168 } 169 170 public function isReadOnly() { 171 if($this->readOnly == 1) return true; 172 return false; 173 } 174 175 private function setNullable($nullable){ 176 $this->nullable = $nullable; 177 } 178 179 public function setDefault($value){ 180 $this->default = $value; 181 $this->explicitDefaultValue = true; 182 $this->defaultValuePresent = true; 183 } 184 185 public function setFieldDataType($dataType){ 186 $this->fieldDataType = $dataType; 187 } 188 189 public function setReferenceList($referenceList){ 190 $this->referenceList = $referenceList; 191 } 192 193 public function getTableFields(){ 194 $tableFields = null; 195 if(isset(WebserviceField::$tableMeta[$this->getTableName()])){ 196 $tableFields = WebserviceField::$tableMeta[$this->getTableName()]; 197 }else{ 198 $dbMetaColumns = $this->pearDB->database->MetaColumns($this->getTableName()); 199 $tableFields = array(); 200 foreach ($dbMetaColumns as $key => $dbField) { 201 $tableFields[$dbField->name] = $dbField; 202 } 203 WebserviceField::$tableMeta[$this->getTableName()] = $tableFields; 204 } 205 return $tableFields; 206 } 207 public function fillColumnMeta(){ 208 $tableFields = $this->getTableFields(); 209 foreach ($tableFields as $fieldName => $dbField) { 210 if(strcmp($fieldName,$this->getColumnName())===0){ 211 $this->setNullable(!$dbField->not_null); 212 if($dbField->has_default === true && !$this->explicitDefaultValue){ 213 $this->defaultValuePresent = $dbField->has_default; 214 $this->setDefault($dbField->default_value); 215 } 216 } 217 } 218 $this->dataFromMeta = true; 219 } 220 221 public function getFieldDataType(){ 222 if($this->fieldDataType === null){ 223 $fieldDataType = $this->getFieldTypeFromUIType(); 224 if($fieldDataType === null){ 225 $fieldDataType = $this->getFieldTypeFromTypeOfData(); 226 } 227 if($fieldDataType == 'date' || $fieldDataType == 'datetime' || $fieldDataType == 'time') { 228 $tableFieldDataType = $this->getFieldTypeFromTable(); 229 if($tableFieldDataType == 'datetime'){ 230 $fieldDataType = $tableFieldDataType; 231 } 232 } 233 $this->fieldDataType = $fieldDataType; 234 } 235 return $this->fieldDataType; 236 } 237 238 public function getReferenceList(){ 239 static $referenceList = array(); 240 if($this->referenceList === null){ 241 if(isset($referenceList[$this->getFieldId()])){ 242 $this->referenceList = $referenceList[$this->getFieldId()]; 243 return $referenceList[$this->getFieldId()]; 244 } 245 if(!isset(WebserviceField::$fieldTypeMapping[$this->getUIType()])){ 246 $this->getFieldTypeFromUIType(); 247 } 248 $fieldTypeData = WebserviceField::$fieldTypeMapping[$this->getUIType()]; 249 $referenceTypes = array(); 250 if($this->getUIType() != $this->genericUIType){ 251 $sql = "select * from vtiger_ws_referencetype where fieldtypeid=?"; 252 $params = array($fieldTypeData['fieldtypeid']); 253 }else{ 254 $sql = 'select relmodule as type from vtiger_fieldmodulerel where fieldid=?'; 255 $params = array($this->getFieldId()); 256 } 257 $result = $this->pearDB->pquery($sql,$params); 258 $numRows = $this->pearDB->num_rows($result); 259 for($i=0;$i<$numRows;++$i){ 260 array_push($referenceTypes,$this->pearDB->query_result($result,$i,"type")); 261 } 262 263 //to handle hardcoding done for Calendar module todo activities. 264 if($this->tabid == 9 && $this->fieldName =='parent_id'){ 265 $referenceTypes[] = 'Invoice'; 266 $referenceTypes[] = 'Quotes'; 267 $referenceTypes[] = 'PurchaseOrder'; 268 $referenceTypes[] = 'SalesOrder'; 269 $referenceTypes[] = 'Campaigns'; 270 } 271 272 global $current_user; 273 $types = vtws_listtypes(null, $current_user); 274 $accessibleTypes = $types['types']; 275 //If it is non admin user or the edit and view is there for profile then users module will be accessible 276 if(!is_admin($current_user)&& !in_array("Users",$accessibleTypes)) { 277 array_push($accessibleTypes, 'Users'); 278 } 279 $referenceTypes = array_values(array_intersect($accessibleTypes,$referenceTypes)); 280 $referenceList[$this->getFieldId()] = $referenceTypes; 281 $this->referenceList = $referenceTypes; 282 return $referenceTypes; 283 } 284 return $this->referenceList; 285 } 286 287 private function getFieldTypeFromTable(){ 288 $tableFields = $this->getTableFields(); 289 foreach ($tableFields as $fieldName => $dbField) { 290 if(strcmp($fieldName,$this->getColumnName())===0){ 291 return $dbField->type; 292 } 293 } 294 //This should not be returned if entries in DB are correct. 295 return null; 296 } 297 298 private function getFieldTypeFromTypeOfData(){ 299 switch($this->fieldType){ 300 case 'T': return "time"; 301 case 'D': 302 case 'DT': return "date"; 303 case 'E': return "email"; 304 case 'N': 305 case 'NN': return "double"; 306 case 'P': return "password"; 307 case 'I': return "integer"; 308 case 'V': 309 default: return "string"; 310 } 311 } 312 313 private function getFieldTypeFromUIType(){ 314 315 // Cache all the information for futher re-use 316 if(empty(self::$fieldTypeMapping)) { 317 $result = $this->pearDB->pquery("select * from vtiger_ws_fieldtype", array()); 318 while($resultrow = $this->pearDB->fetch_array($result)) { 319 self::$fieldTypeMapping[$resultrow['uitype']] = $resultrow; 320 } 321 } 322 323 if(isset(WebserviceField::$fieldTypeMapping[$this->getUIType()])){ 324 if(WebserviceField::$fieldTypeMapping[$this->getUIType()] === false){ 325 return null; 326 } 327 $row = WebserviceField::$fieldTypeMapping[$this->getUIType()]; 328 return $row['fieldtype']; 329 } else { 330 WebserviceField::$fieldTypeMapping[$this->getUIType()] = false; 331 return null; 332 } 333 } 334 335 function getPicklistDetails(){ 336 $cache = Vtiger_Cache::getInstance(); 337 if($cache->getPicklistDetails($this->getTabId(),$this->getFieldName())){ 338 return $cache->getPicklistDetails($this->getTabId(),$this->getFieldName()); 339 } else { 340 $hardCodedPickListNames = array("hdntaxtype","email_flag"); 341 $hardCodedPickListValues = array( 342 "hdntaxtype"=>array( 343 array("label"=>"Individual","value"=>"individual"), 344 array("label"=>"Group","value"=>"group") 345 ), 346 "email_flag" => array( 347 array('label'=>'SAVED','value'=>'SAVED'), 348 array('label'=>'SENT','value' => 'SENT'), 349 array('label'=>'MAILSCANNER','value' => 'MAILSCANNER') 350 ) 351 ); 352 if(in_array(strtolower($this->getFieldName()),$hardCodedPickListNames)){ 353 return $hardCodedPickListValues[strtolower($this->getFieldName())]; 354 } 355 $picklistDetails = $this->getPickListOptions($this->getFieldName()); 356 $cache->setPicklistDetails($this->getTabId(),$this->getFieldName(),$picklistDetails); 357 return $picklistDetails; 358 } 359 } 360 361 function getPickListOptions(){ 362 $fieldName = $this->getFieldName(); 363 364 $default_charset = VTWS_PreserveGlobal::getGlobal('default_charset'); 365 $options = array(); 366 $sql = "select * from vtiger_picklist where name=?"; 367 $result = $this->pearDB->pquery($sql,array($fieldName)); 368 $numRows = $this->pearDB->num_rows($result); 369 if($numRows == 0){ 370 $sql = "select * from vtiger_$fieldName"; 371 $result = $this->pearDB->pquery($sql,array()); 372 $numRows = $this->pearDB->num_rows($result); 373 for($i=0;$i<$numRows;++$i){ 374 $elem = array(); 375 $picklistValue = $this->pearDB->query_result($result,$i,$fieldName); 376 $picklistValue = decode_html($picklistValue); 377 $moduleName = getTabModuleName($this->getTabId()); 378 if($moduleName == 'Events') $moduleName = 'Calendar'; 379 $elem["label"] = getTranslatedString($picklistValue,$moduleName); 380 $elem["value"] = $picklistValue; 381 array_push($options,$elem); 382 } 383 }else{ 384 $user = VTWS_PreserveGlobal::getGlobal('current_user'); 385 $details = getPickListValues($fieldName,$user->roleid); 386 for($i=0;$i<sizeof($details);++$i){ 387 $elem = array(); 388 $picklistValue = decode_html($details[$i]); 389 $moduleName = getTabModuleName($this->getTabId()); 390 if($moduleName == 'Events') $moduleName = 'Calendar'; 391 $elem["label"] = getTranslatedString($picklistValue,$moduleName); 392 $elem["value"] = $picklistValue; 393 array_push($options,$elem); 394 } 395 } 396 return $options; 397 } 398 399 function getPresence() { 400 return $this->presence; 401 } 402 403 } 404 405 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |