PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/output/classes/controls/ViewLookupWizardField.php

https://gitlab.com/Lidbary/PHPRunner
PHP | 363 lines | 252 code | 58 blank | 53 comment | 47 complexity | 07558cd28d1fa66fc54ce3bbb5322f61 MD5 | raw file
  1. <?php
  2. class ViewLookupWizardField extends ViewControl
  3. {
  4. public $nLookupType;
  5. public $lookupTable;
  6. public $displayFieldName;
  7. public $linkFieldName;
  8. public $linkAndDisplaySame;
  9. public $pSet;
  10. public $lookupPSet;
  11. public $cipherer;
  12. public $lookupQueryObj;
  13. public $displayFieldIndex;
  14. public $LookupSQL;
  15. public $resolvedLookupValues = array();
  16. public $resolvedLinkLookupValues = array();
  17. public $linkFieldIndex;
  18. public $localControlsContainer;
  19. /**
  20. * @type Connection
  21. */
  22. protected $lookupConnection;
  23. public function ViewLookupWizardField($field, $container, $pageObject)
  24. {
  25. parent::ViewControl($field, $container, $pageObject);
  26. $this->lookupPSet = null;
  27. $this->cipherer = null;
  28. $this->lookupQueryObj = null;
  29. $this->displayFieldIndex = 0;
  30. $this->linkFieldIndex = 1;
  31. $this->LookupSQL = "";
  32. if($this->container->pSet->getEditFormat($field) != EDIT_FORMAT_LOOKUP_WIZARD)
  33. {
  34. $this->pSet = new ProjectSettings( $this->container->pSet->_table );
  35. // set view page
  36. $this->pSet->setPage( $this->container->pageType );
  37. // set edit page
  38. $this->pSet->setPage( $this->container->pSet->getPageTypeByFieldEditFormat($field, EDIT_FORMAT_LOOKUP_WIZARD) );
  39. }
  40. else
  41. $this->pSet = $this->container->pSet;
  42. $this->nLookupType = $this->pSet->getLookupType($this->field);
  43. $this->lookupTable = $this->pSet->getLookupTable($this->field);
  44. $this->setLookupConnection();
  45. $this->displayFieldName = $this->pSet->getDisplayField($this->field);
  46. $this->linkFieldName = $this->pSet->getLinkField($this->field);
  47. $this->linkAndDisplaySame = $this->displayFieldName == $this->linkFieldName;
  48. if($this->nLookupType == LT_QUERY)
  49. {
  50. $this->lookupPSet = new ProjectSettings($this->lookupTable, $this->container->pageType);
  51. $this->cipherer = new RunnerCipherer($this->lookupTable);
  52. $this->lookupQueryObj = $this->lookupPSet->getSQLQuery()->CloneObject();
  53. if($this->pSet->getCustomDisplay($this->field))
  54. $this->lookupQueryObj->AddCustomExpression($this->displayFieldName, $this->lookupPSet, $this->pSet->_table, $this->field);
  55. $this->lookupQueryObj->ReplaceFieldsWithDummies($this->lookupPSet->getBinaryFieldsIndices());
  56. $lookupIndexes = GetLookupFieldsIndexes($this->pSet, $this->field);
  57. $this->displayFieldIndex = $lookupIndexes["displayFieldIndex"];
  58. $this->linkFieldIndex = $lookupIndexes["linkFieldIndex"];
  59. }
  60. else
  61. {
  62. $this->cipherer = new RunnerCipherer($this->pSet->_table);
  63. $this->LookupSQL = "SELECT ";
  64. $this->LookupSQL.= RunnerPage::sqlFormattedDisplayField($this->field, $this->lookupConnection, $this->pSet);
  65. $this->LookupSQL.= ", ".$this->lookupConnection->addFieldWrappers($this->pSet->getLinkField($this->field));
  66. $this->LookupSQL.= " FROM ".$this->lookupConnection->addTableWrappers( $this->lookupTable )." WHERE ";
  67. }
  68. $this->localControlsContainer = new ViewControlsContainer($this->pSet, $this->container->pageType, $pageObject);
  69. $this->localControlsContainer->isLocal = true;
  70. }
  71. /**
  72. * Set the lookupConnection property
  73. */
  74. protected function setLookupConnection()
  75. {
  76. global $cman;
  77. if( $this->nLookupType == LT_QUERY )
  78. {
  79. $this->lookupConnection = $cman->byTable( $this->lookupTable );
  80. return;
  81. }
  82. $connId = $this->pSet->getNotProjectLookupTableConnId( $this->field );
  83. $this->lookupConnection = strlen( $connId ) ? $cman->byId( $connId ) : $cman->getDefault();
  84. }
  85. /**
  86. * @param String value
  87. * @return String
  88. */
  89. protected function getDbPreparedValuesList( $value )
  90. {
  91. if( !$this->pSet->multiSelect($this->field) )
  92. return "";
  93. $values = splitvalues($value);
  94. $type = $this->pSet->getLWLinkFieldType($this->field);
  95. $numeric = true;
  96. if(!$type)
  97. {
  98. foreach($values as $val)
  99. {
  100. if( strlen($val) && !is_numeric($val) )
  101. {
  102. $numeric = false;
  103. break;
  104. }
  105. }
  106. }
  107. else
  108. $numeric = !NeedQuotes($type);
  109. $listValues = array();
  110. foreach($values as $val)
  111. {
  112. if( $numeric && !strlen($val) )
  113. continue;
  114. if( $numeric )
  115. $listValues[] = ($val + 0);
  116. else
  117. {
  118. $fName = $this->nLookupType == LT_QUERY ? $this->linkFieldName : $this->field;
  119. $listValues[] = $this->lookupConnection->prepareString( $this->cipherer->EncryptField($fName, $val) );
  120. }
  121. }
  122. return implode(",", $listValues);
  123. }
  124. /**
  125. * @param String value
  126. * @param String in
  127. * @return String
  128. */
  129. protected function getMultiselectLookupResolvingSQL( $value, $in )
  130. {
  131. if( !$this->pSet->multiSelect($this->field) )
  132. return "";
  133. $where = GetLWWhere($this->field, $this->pSet->getEditPageType());
  134. if( $this->nLookupType == LT_QUERY )
  135. {
  136. $inWhere = RunnerPage::_getFieldSQLDecrypt( $this->linkFieldName, $this->lookupConnection, $this->lookupPSet, $this->cipherer )
  137. ." in (".$in.")";
  138. if( strlen($where) )
  139. $inWhere.=" and (".$where.")";
  140. $LookupSQL = $this->lookupQueryObj->toSql(whereAdd($this->lookupQueryObj->m_where->toSql($this->lookupQueryObj), $inWhere));
  141. }
  142. else
  143. {
  144. $LookupSQL = $this->LookupSQL.$this->lookupConnection->addFieldWrappers($this->pSet->getLinkField($this->field))." in (".$in.")";
  145. if( strlen($where) )
  146. $LookupSQL.=" and (".$where.")";
  147. }
  148. return $LookupSQL;
  149. }
  150. /**
  151. * @param String value
  152. * @return String
  153. */
  154. protected function getNotMultiselectLookupResolvingSQL( $value )
  155. {
  156. if( $this->pSet->multiSelect($this->field) )
  157. return "";
  158. $where = GetLWWhere($this->field, $this->pSet->getEditPageType());
  159. $strdata = $this->cipherer->MakeDBValue($this->nLookupType == LT_QUERY ? $this->linkFieldName : $this->field, $value, "", true);
  160. if( $this->nLookupType == LT_QUERY )
  161. {
  162. $strWhere = GetFullFieldName($this->linkFieldName, $this->lookupTable, false)." = " . $strdata;
  163. if( strlen($where) )
  164. $strWhere.= " and (".$where.")";
  165. $LookupSQL = $this->lookupQueryObj->toSql(whereAdd($this->lookupQueryObj->m_where->toSql($this->lookupQueryObj), $strWhere));
  166. }
  167. else
  168. {
  169. $strWhere = $this->lookupConnection->addFieldWrappers($this->pSet->getLinkField($this->field))." = " . $strdata;
  170. if( strlen($where) )
  171. $strWhere.= " and (".$where.")";
  172. $LookupSQL = $this->LookupSQL.$strWhere;
  173. }
  174. return $LookupSQL;
  175. }
  176. /**
  177. * @param String lookupValue
  178. * @return String
  179. */
  180. protected function getDecryptLookupValue( $lookupValue )
  181. {
  182. if( $this->nLookupType == LT_QUERY || $this->linkAndDisplaySame )
  183. return $this->cipherer->DecryptField($this->nLookupType == LT_QUERY ? $this->displayFieldName : $this->field, $lookupValue);
  184. return $lookupValue;
  185. }
  186. /**
  187. * @param String value
  188. * return Array
  189. */
  190. protected function getMultiselectLookupValues( $value )
  191. {
  192. $in = $this->getDbPreparedValuesList( $value );
  193. if( !strlen($in) )
  194. return array( $value );
  195. if( count($this->resolvedLookupValues[ $value ]) )
  196. return $this->resolvedLookupValues[ $value ];
  197. $LookupSQL = $this->getMultiselectLookupResolvingSQL($value, $in);
  198. LogInfo($LookupSQL);
  199. $lookupArr = array();
  200. $qResult = $this->lookupConnection->query( $LookupSQL );
  201. while( $lookuprow = $qResult->fetchNumeric() )
  202. {
  203. $displayValue = $lookuprow[ $this->displayFieldIndex ];
  204. $lookupArr[] = $displayValue;
  205. $this->resolvedLinkLookupValues[ $value ][ $displayValue ] = $lookuprow[ $this->linkFieldIndex ];
  206. }
  207. $lookupValues = array();
  208. $lookupArr = array_unique( $lookupArr );
  209. foreach($lookupArr as $lookupvalue)
  210. {
  211. $lookupValues[] = $this->getDecryptLookupValue( $lookupvalue );
  212. }
  213. if( count($lookupValues) )
  214. $this->resolvedLookupValues[ $value ] = $lookupValues;
  215. return $lookupValues;
  216. }
  217. /**
  218. * @param String value
  219. * return Array
  220. */
  221. protected function getNotMultiselectLookupValues( $value )
  222. {
  223. if( isset( $this->resolvedLookupValues[ $value ] ) )
  224. return array( $this->resolvedLookupValues[ $value ] );
  225. $lookupvalue = $value;
  226. $LookupSQL = $this->getNotMultiselectLookupResolvingSQL( $value );
  227. LogInfo($LookupSQL);
  228. $qResult = $this->lookupConnection->query( $LookupSQL );
  229. if( $lookuprow = $qResult->fetchNumeric() )
  230. {
  231. $lookupvalue = $this->getDecryptLookupValue( $lookuprow[ $this->displayFieldIndex ] );
  232. $this->resolvedLookupValues[ $value ] = $lookupvalue;
  233. }
  234. return array( $lookupvalue );
  235. }
  236. /**
  237. * @param String value
  238. * return Array
  239. */
  240. protected function getLookupValues( $value )
  241. {
  242. if( $this->pSet->multiSelect($this->field) )
  243. return $this->getMultiselectLookupValues( $value );
  244. return $this->getNotMultiselectLookupValues( $value );
  245. }
  246. /**
  247. * @param &Array data
  248. * @param String keylink
  249. */
  250. public function showDBValue(&$data, $keylink)
  251. {
  252. $value = $data[ $this->field ];
  253. if( !strlen($value) )
  254. return "";
  255. $outValues = array();
  256. $localData = $data;
  257. $lookupValues = $this->getLookupValues( $value );
  258. foreach( $lookupValues as $lookupvalue )
  259. {
  260. $this->localControlsContainer->linkFieldValues[ $this->field ] = $data[ $this->field ];
  261. // to highlight lookup search result correctly
  262. if( isset( $this->resolvedLinkLookupValues[ $value ] ) && isset( $this->resolvedLinkLookupValues[ $value ][ $lookupvalue ] ) )
  263. $this->localControlsContainer->originlinkValues[ $this->field ] = $this->resolvedLinkLookupValues[ $value ][ $lookupvalue ];
  264. if( $this->pSet->getViewFormat($this->field) != "Custom" )
  265. $localData[ $this->field ] = $lookupvalue;
  266. $outValues[] = $this->localControlsContainer->showDBValue($this->field, $localData, $keylink, $lookupvalue);
  267. }
  268. return implode(",", $outValues);
  269. }
  270. /**
  271. * User API function
  272. * @param &Array data
  273. * @return String
  274. */
  275. public function getTextValue(&$data)
  276. {
  277. $value = $data[ $this->field ];
  278. if( !strlen($value) )
  279. return "";
  280. $textValues = array();
  281. $localData = $data;
  282. $lookupValues = $this->getLookupValues( $value );
  283. foreach( $lookupValues as $lookupvalue )
  284. {
  285. if( $this->pSet->getViewFormat($this->field) != "Custom" )
  286. $localData[ $this->field ] = $lookupvalue;
  287. $textValues[] = $this->localControlsContainer->getControl( $this->field )->getTextValue( $localData );
  288. }
  289. return implode(",", $textValues);
  290. }
  291. /**
  292. * Get the field's content that will be exported
  293. * @prarm &Array data
  294. * @prarm String keylink
  295. * @return String
  296. */
  297. public function getExportValue(&$data, $keylink = "")
  298. {
  299. $this->localControlsContainer->setForExportVar( $this->container->forExport );
  300. if( $this->container->forExport == "csv" )
  301. return $data[ $this->field ];
  302. return $this->showDBValue($data, $keylink);
  303. }
  304. }
  305. ?>