attributedef.class.inc.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Typology for the attributes
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('MyHelpers.class.inc.php');
  25. require_once('ormdocument.class.inc.php');
  26. /**
  27. * MissingColumnException - sent if an attribute is being created but the column is missing in the row
  28. *
  29. * @package iTopORM
  30. */
  31. class MissingColumnException extends Exception
  32. {}
  33. /**
  34. * add some description here...
  35. *
  36. * @package iTopORM
  37. */
  38. define('EXTKEY_RELATIVE', 1);
  39. /**
  40. * add some description here...
  41. *
  42. * @package iTopORM
  43. */
  44. define('EXTKEY_ABSOLUTE', 2);
  45. /**
  46. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  47. *
  48. * @package iTopORM
  49. */
  50. define('DEL_MANUAL', 1);
  51. /**
  52. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  53. *
  54. * @package iTopORM
  55. */
  56. define('DEL_AUTO', 2);
  57. /**
  58. * Attribute definition API, implemented in and many flavours (Int, String, Enum, etc.)
  59. *
  60. * @package iTopORM
  61. */
  62. abstract class AttributeDefinition
  63. {
  64. abstract public function GetType();
  65. abstract public function GetTypeDesc();
  66. abstract public function GetEditClass();
  67. protected $m_sCode;
  68. private $m_aParams = array();
  69. protected $m_sHostClass = '!undefined!';
  70. protected function Get($sParamName) {return $this->m_aParams[$sParamName];}
  71. protected function IsParam($sParamName) {return (array_key_exists($sParamName, $this->m_aParams));}
  72. protected function GetOptional($sParamName, $default)
  73. {
  74. if (array_key_exists($sParamName, $this->m_aParams))
  75. {
  76. return $this->m_aParams[$sParamName];
  77. }
  78. else
  79. {
  80. return $default;
  81. }
  82. }
  83. public function __construct($sCode, $aParams)
  84. {
  85. $this->m_sCode = $sCode;
  86. $this->m_aParams = $aParams;
  87. $this->ConsistencyCheck();
  88. }
  89. public function OverloadParams($aParams)
  90. {
  91. foreach ($aParams as $sParam => $value)
  92. {
  93. if (!array_key_exists($sParam, $this->m_aParams))
  94. {
  95. throw new CoreException("Unknown attribute definition parameter '$sParam', please select a value in {".implode(", ", array_keys($this->m_aParams))."}");
  96. }
  97. else
  98. {
  99. $this->m_aParams[$sParam] = $value;
  100. }
  101. }
  102. }
  103. public function SetHostClass($sHostClass)
  104. {
  105. $this->m_sHostClass = $sHostClass;
  106. }
  107. public function GetHostClass()
  108. {
  109. return $this->m_sHostClass;
  110. }
  111. // Note: I could factorize this code with the parameter management made for the AttributeDef class
  112. // to be overloaded
  113. static protected function ListExpectedParams()
  114. {
  115. return array();
  116. }
  117. private function ConsistencyCheck()
  118. {
  119. // Check that any mandatory param has been specified
  120. //
  121. $aExpectedParams = $this->ListExpectedParams();
  122. foreach($aExpectedParams as $sParamName)
  123. {
  124. if (!array_key_exists($sParamName, $this->m_aParams))
  125. {
  126. $aBacktrace = debug_backtrace();
  127. $sTargetClass = $aBacktrace[2]["class"];
  128. $sCodeInfo = $aBacktrace[1]["file"]." - ".$aBacktrace[1]["line"];
  129. throw new Exception("ERROR missing parameter '$sParamName' in ".get_class($this)." declaration for class $sTargetClass ($sCodeInfo)");
  130. }
  131. }
  132. }
  133. // table, key field, name field
  134. public function ListDBJoins()
  135. {
  136. return "";
  137. // e.g: return array("Site", "infrid", "name");
  138. }
  139. public function IsDirectField() {return false;}
  140. public function IsScalar() {return false;}
  141. public function IsLinkSet() {return false;}
  142. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return false;}
  143. public function IsExternalField() {return false;}
  144. public function IsWritable() {return false;}
  145. public function IsNullAllowed() {return true;}
  146. public function GetCode() {return $this->m_sCode;}
  147. public function GetLabel() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $this->m_sCode);}
  148. public function GetLabel_Obsolete()
  149. {
  150. // Written for compatibility with a data model written prior to version 0.9.1
  151. if (array_key_exists('label', $this->m_aParams))
  152. {
  153. return $this->m_aParams['label'];
  154. }
  155. else
  156. {
  157. return $this->GetLabel();
  158. }
  159. }
  160. public function GetDescription() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', '');}
  161. public function GetHelpOnEdition() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'?', '');}
  162. public function GetDescription_Obsolete()
  163. {
  164. // Written for compatibility with a data model written prior to version 0.9.1
  165. if (array_key_exists('description', $this->m_aParams))
  166. {
  167. return $this->m_aParams['description'];
  168. }
  169. else
  170. {
  171. return $this->GetDescription();
  172. }
  173. }
  174. public function GetValuesDef() {return null;}
  175. public function GetPrerequisiteAttributes() {return array();}
  176. public function GetNullValue() {return null;}
  177. public function IsNull($proposedValue) {return is_null($proposedValue);}
  178. public function MakeRealValue($proposedValue) {return $proposedValue;} // force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing!)
  179. public function GetSQLExpressions() {return array();} // returns suffix/expression pairs (1 in most of the cases), for READING (Select)
  180. public function FromSQLToValue($aCols, $sPrefix = '') {return null;} // returns a value out of suffix/value pairs, for SELECT result interpretation
  181. public function GetSQLColumns() {return array();} // returns column/spec pairs (1 in most of the cases), for STRUCTURING (DB creation)
  182. public function GetSQLValues($value) {return array();} // returns column/value pairs (1 in most of the cases), for WRITING (Insert, Update)
  183. public function RequiresIndex() {return false;}
  184. public function GetValidationPattern()
  185. {
  186. return '';
  187. }
  188. public function CheckFormat($value)
  189. {
  190. return true;
  191. }
  192. public function MakeValue()
  193. {
  194. $sComputeFunc = $this->Get("compute_func");
  195. if (empty($sComputeFunc)) return null;
  196. return call_user_func($sComputeFunc);
  197. }
  198. abstract public function GetDefaultValue();
  199. //
  200. // To be overloaded in subclasses
  201. //
  202. abstract public function GetBasicFilterOperators(); // returns an array of "opCode"=>"description"
  203. abstract public function GetBasicFilterLooseOperator(); // returns an "opCode"
  204. //abstract protected GetBasicFilterHTMLInput();
  205. abstract public function GetBasicFilterSQLExpr($sOpCode, $value);
  206. public function GetFilterDefinitions()
  207. {
  208. return array();
  209. }
  210. public function GetEditValue($sValue)
  211. {
  212. return (string)$sValue;
  213. }
  214. public function GetAsHTML($sValue)
  215. {
  216. return Str::pure2html((string)$sValue);
  217. }
  218. public function GetAsXML($sValue)
  219. {
  220. return Str::pure2xml((string)$sValue);
  221. }
  222. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  223. {
  224. return (string)$sValue;
  225. }
  226. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  227. {
  228. $oValSetDef = $this->GetValuesDef();
  229. if (!$oValSetDef) return null;
  230. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  231. }
  232. /**
  233. * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
  234. * Each derived class is reponsible for defining and processing their own smart patterns, the base class
  235. * does nothing special, and just calls the default (loose) operator
  236. * @param string $sSearchText The search string to analyze for smart patterns
  237. * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
  238. * @param Hash $aParams Values of the query parameters
  239. * @return Expression The search condition to be added (AND) to the current search
  240. */
  241. public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
  242. {
  243. $sParamName = $oField->GetParent().'_'.$oField->GetName();
  244. $oRightExpr = new VariableExpression($sParamName);
  245. $sOperator = $this->GetBasicFilterLooseOperator();
  246. switch ($sOperator)
  247. {
  248. case 'Contains':
  249. $aParams[$sParamName] = "%$sSearchText%";
  250. $sSQLOperator = 'LIKE';
  251. break;
  252. default:
  253. $sSQLOperator = $sOperator;
  254. $aParams[$sParamName] = $sSearchText;
  255. }
  256. $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
  257. return $oNewCondition;
  258. }
  259. }
  260. /**
  261. * Set of objects directly linked to an object, and being part of its definition
  262. *
  263. * @package iTopORM
  264. */
  265. class AttributeLinkedSet extends AttributeDefinition
  266. {
  267. static protected function ListExpectedParams()
  268. {
  269. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "linked_class", "ext_key_to_me", "count_min", "count_max"));
  270. }
  271. public function GetType() {return "Array of objects";}
  272. public function GetTypeDesc() {return "Any kind of objects [subclass] of the same class";}
  273. public function GetEditClass() {return "List";}
  274. public function IsWritable() {return true;}
  275. public function IsLinkSet() {return true;}
  276. public function IsIndirect() {return false;}
  277. public function GetValuesDef() {return $this->Get("allowed_values");}
  278. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  279. public function GetDefaultValue($aArgs = array())
  280. {
  281. // Note: so far, this feature is a prototype,
  282. // later, the argument 'this' should always be present in the arguments
  283. //
  284. if (($this->IsParam('default_value')) && array_key_exists('this', $aArgs))
  285. {
  286. $aValues = $this->Get('default_value')->GetValues($aArgs);
  287. $oSet = DBObjectSet::FromArray($this->Get('linked_class'), $aValues);
  288. return $oSet;
  289. }
  290. else
  291. {
  292. return DBObjectSet::FromScratch($this->Get('linked_class'));
  293. }
  294. }
  295. public function GetLinkedClass() {return $this->Get('linked_class');}
  296. public function GetExtKeyToMe() {return $this->Get('ext_key_to_me');}
  297. public function GetBasicFilterOperators() {return array();}
  298. public function GetBasicFilterLooseOperator() {return '';}
  299. public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
  300. public function GetAsHTML($sValue)
  301. {
  302. return "ERROR: LIST OF OBJECTS";
  303. }
  304. public function GetAsXML($sValue)
  305. {
  306. return "ERROR: LIST OF OBJECTS";
  307. }
  308. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  309. {
  310. return "ERROR: LIST OF OBJECTS";
  311. }
  312. }
  313. /**
  314. * Set of objects linked to an object (n-n), and being part of its definition
  315. *
  316. * @package iTopORM
  317. */
  318. class AttributeLinkedSetIndirect extends AttributeLinkedSet
  319. {
  320. static protected function ListExpectedParams()
  321. {
  322. return array_merge(parent::ListExpectedParams(), array("ext_key_to_remote"));
  323. }
  324. public function IsIndirect() {return true;}
  325. public function GetExtKeyToRemote() { return $this->Get('ext_key_to_remote'); }
  326. public function GetEditClass() {return "LinkedSet";}
  327. }
  328. /**
  329. * Abstract class implementing default filters for a DB column
  330. *
  331. * @package iTopORM
  332. */
  333. class AttributeDBFieldVoid extends AttributeDefinition
  334. {
  335. static protected function ListExpectedParams()
  336. {
  337. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "sql"));
  338. }
  339. // To be overriden, used in GetSQLColumns
  340. protected function GetSQLCol() {return "VARCHAR(255)";}
  341. public function GetType() {return "Void";}
  342. public function GetTypeDesc() {return "Any kind of value, from the DB";}
  343. public function GetEditClass() {return "String";}
  344. public function GetValuesDef() {return $this->Get("allowed_values");}
  345. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  346. public function IsDirectField() {return true;}
  347. public function IsScalar() {return true;}
  348. public function IsWritable() {return true;}
  349. public function GetSQLExpr() {return $this->Get("sql");}
  350. public function GetDefaultValue() {return $this->MakeRealValue("");}
  351. public function IsNullAllowed() {return false;}
  352. //
  353. protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  354. public function GetSQLExpressions()
  355. {
  356. $aColumns = array();
  357. // Note: to optimize things, the existence of the attribute is determine by the existence of one column with an empty suffix
  358. $aColumns[''] = $this->Get("sql");
  359. return $aColumns;
  360. }
  361. public function FromSQLToValue($aCols, $sPrefix = '')
  362. {
  363. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  364. return $value;
  365. }
  366. public function GetSQLValues($value)
  367. {
  368. $aValues = array();
  369. $aValues[$this->Get("sql")] = $this->ScalarToSQL($value);
  370. return $aValues;
  371. }
  372. public function GetSQLColumns()
  373. {
  374. $aColumns = array();
  375. $aColumns[$this->Get("sql")] = $this->GetSQLCol();
  376. return $aColumns;
  377. }
  378. public function GetFilterDefinitions()
  379. {
  380. return array($this->GetCode() => new FilterFromAttribute($this));
  381. }
  382. public function GetBasicFilterOperators()
  383. {
  384. return array("="=>"equals", "!="=>"differs from");
  385. }
  386. public function GetBasicFilterLooseOperator()
  387. {
  388. return "=";
  389. }
  390. public function GetBasicFilterSQLExpr($sOpCode, $value)
  391. {
  392. $sQValue = CMDBSource::Quote($value);
  393. switch ($sOpCode)
  394. {
  395. case '!=':
  396. return $this->GetSQLExpr()." != $sQValue";
  397. break;
  398. case '=':
  399. default:
  400. return $this->GetSQLExpr()." = $sQValue";
  401. }
  402. }
  403. }
  404. /**
  405. * Base class for all kind of DB attributes, with the exception of external keys
  406. *
  407. * @package iTopORM
  408. */
  409. class AttributeDBField extends AttributeDBFieldVoid
  410. {
  411. static protected function ListExpectedParams()
  412. {
  413. return array_merge(parent::ListExpectedParams(), array("default_value", "is_null_allowed"));
  414. }
  415. public function GetDefaultValue() {return $this->MakeRealValue($this->Get("default_value"));}
  416. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  417. }
  418. /**
  419. * Map an integer column to an attribute
  420. *
  421. * @package iTopORM
  422. */
  423. class AttributeInteger extends AttributeDBField
  424. {
  425. static protected function ListExpectedParams()
  426. {
  427. return parent::ListExpectedParams();
  428. //return array_merge(parent::ListExpectedParams(), array());
  429. }
  430. public function GetType() {return "Integer";}
  431. public function GetTypeDesc() {return "Numeric value (could be negative)";}
  432. public function GetEditClass() {return "String";}
  433. protected function GetSQLCol() {return "INT(11)";}
  434. public function GetValidationPattern()
  435. {
  436. return "^[0-9]+$";
  437. }
  438. public function GetBasicFilterOperators()
  439. {
  440. return array(
  441. "!="=>"differs from",
  442. "="=>"equals",
  443. ">"=>"greater (strict) than",
  444. ">="=>"greater than",
  445. "<"=>"less (strict) than",
  446. "<="=>"less than",
  447. "in"=>"in"
  448. );
  449. }
  450. public function GetBasicFilterLooseOperator()
  451. {
  452. // Unless we implement an "equals approximately..." or "same order of magnitude"
  453. return "=";
  454. }
  455. public function GetBasicFilterSQLExpr($sOpCode, $value)
  456. {
  457. $sQValue = CMDBSource::Quote($value);
  458. switch ($sOpCode)
  459. {
  460. case '!=':
  461. return $this->GetSQLExpr()." != $sQValue";
  462. break;
  463. case '>':
  464. return $this->GetSQLExpr()." > $sQValue";
  465. break;
  466. case '>=':
  467. return $this->GetSQLExpr()." >= $sQValue";
  468. break;
  469. case '<':
  470. return $this->GetSQLExpr()." < $sQValue";
  471. break;
  472. case '<=':
  473. return $this->GetSQLExpr()." <= $sQValue";
  474. break;
  475. case 'in':
  476. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  477. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  478. break;
  479. case '=':
  480. default:
  481. return $this->GetSQLExpr()." = \"$value\"";
  482. }
  483. }
  484. public function GetNullValue()
  485. {
  486. return null;
  487. }
  488. public function IsNull($proposedValue)
  489. {
  490. return is_null($proposedValue);
  491. }
  492. public function MakeRealValue($proposedValue)
  493. {
  494. if (is_null($proposedValue)) return null;
  495. if ($proposedValue == '') return null;
  496. return (int)$proposedValue;
  497. }
  498. public function ScalarToSQL($value)
  499. {
  500. assert(is_numeric($value) || is_null($value));
  501. return $value; // supposed to be an int
  502. }
  503. }
  504. /**
  505. * Map a boolean column to an attribute
  506. *
  507. * @package iTopORM
  508. */
  509. class AttributeBoolean extends AttributeInteger
  510. {
  511. static protected function ListExpectedParams()
  512. {
  513. return parent::ListExpectedParams();
  514. //return array_merge(parent::ListExpectedParams(), array());
  515. }
  516. public function GetType() {return "Boolean";}
  517. public function GetTypeDesc() {return "Boolean";}
  518. public function GetEditClass() {return "Integer";}
  519. protected function GetSQLCol() {return "TINYINT(1)";}
  520. public function MakeRealValue($proposedValue)
  521. {
  522. if (is_null($proposedValue)) return null;
  523. if ($proposedValue == '') return null;
  524. if ((int)$proposedValue) return true;
  525. return false;
  526. }
  527. public function ScalarToSQL($value)
  528. {
  529. assert(is_bool($value));
  530. if ($value) return 1;
  531. return 0;
  532. }
  533. }
  534. /**
  535. * Map a varchar column (size < ?) to an attribute
  536. *
  537. * @package iTopORM
  538. */
  539. class AttributeString extends AttributeDBField
  540. {
  541. static protected function ListExpectedParams()
  542. {
  543. return parent::ListExpectedParams();
  544. //return array_merge(parent::ListExpectedParams(), array());
  545. }
  546. public function GetType() {return "String";}
  547. public function GetTypeDesc() {return "Alphanumeric string";}
  548. public function GetEditClass() {return "String";}
  549. protected function GetSQLCol() {return "VARCHAR(255)";}
  550. public function CheckFormat($value)
  551. {
  552. $sRegExp = $this->GetValidationPattern();
  553. if (empty($sRegExp))
  554. {
  555. return true;
  556. }
  557. else
  558. {
  559. $sRegExp = str_replace('/', '\\/', $sRegExp);
  560. return preg_match("/$sRegExp/", $value);
  561. }
  562. }
  563. public function GetBasicFilterOperators()
  564. {
  565. return array(
  566. "="=>"equals",
  567. "!="=>"differs from",
  568. "Like"=>"equals (no case)",
  569. "NotLike"=>"differs from (no case)",
  570. "Contains"=>"contains",
  571. "Begins with"=>"begins with",
  572. "Finishes with"=>"finishes with"
  573. );
  574. }
  575. public function GetBasicFilterLooseOperator()
  576. {
  577. return "Contains";
  578. }
  579. public function GetBasicFilterSQLExpr($sOpCode, $value)
  580. {
  581. $sQValue = CMDBSource::Quote($value);
  582. switch ($sOpCode)
  583. {
  584. case '=':
  585. case '!=':
  586. return $this->GetSQLExpr()." $sOpCode $sQValue";
  587. case 'Begins with':
  588. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("$value%");
  589. case 'Finishes with':
  590. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value");
  591. case 'Contains':
  592. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
  593. case 'NotLike':
  594. return $this->GetSQLExpr()." NOT LIKE $sQValue";
  595. case 'Like':
  596. default:
  597. return $this->GetSQLExpr()." LIKE $sQValue";
  598. }
  599. }
  600. public function GetNullValue()
  601. {
  602. return '';
  603. }
  604. public function IsNull($proposedValue)
  605. {
  606. return ($proposedValue == '');
  607. }
  608. public function MakeRealValue($proposedValue)
  609. {
  610. if (is_null($proposedValue)) return '';
  611. return (string)$proposedValue;
  612. }
  613. public function ScalarToSQL($value)
  614. {
  615. if (!is_string($value) && !is_null($value))
  616. {
  617. throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetCode(), 'attribute' => $this->GetHostClass()));
  618. }
  619. return $value;
  620. }
  621. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  622. {
  623. $sFrom = array("\r\n", $sTextQualifier);
  624. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  625. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  626. return '"'.$sEscaped.'"';
  627. }
  628. }
  629. /**
  630. * An attibute that matches an object class
  631. *
  632. * @package iTopORM
  633. */
  634. class AttributeClass extends AttributeString
  635. {
  636. static protected function ListExpectedParams()
  637. {
  638. return array_merge(parent::ListExpectedParams(), array("class_category", "more_values"));
  639. }
  640. public function __construct($sCode, $aParams)
  641. {
  642. $this->m_sCode = $sCode;
  643. $aParams["allowed_values"] = new ValueSetEnumClasses($aParams['class_category'], $aParams['more_values']);
  644. parent::__construct($sCode, $aParams);
  645. }
  646. public function GetDefaultValue()
  647. {
  648. $sDefault = parent::GetDefaultValue();
  649. if (!$this->IsNullAllowed() && is_null($sDefault))
  650. {
  651. // For this kind of attribute specifying null as default value
  652. // is authorized even if null is not allowed
  653. // Pick the first one...
  654. $aClasses = $this->GetAllowedValues();
  655. $sDefault = key($aClasses);
  656. }
  657. return $sDefault;
  658. }
  659. public function GetAsHTML($sValue)
  660. {
  661. return MetaModel::GetName($sValue);
  662. }
  663. public function RequiresIndex()
  664. {
  665. return true;
  666. }
  667. }
  668. /**
  669. * An attibute that matches one of the language codes availables in the dictionnary
  670. *
  671. * @package iTopORM
  672. */
  673. class AttributeApplicationLanguage extends AttributeString
  674. {
  675. static protected function ListExpectedParams()
  676. {
  677. return parent::ListExpectedParams();
  678. }
  679. public function __construct($sCode, $aParams)
  680. {
  681. $this->m_sCode = $sCode;
  682. $aAvailableLanguages = Dict::GetLanguages();
  683. $aLanguageCodes = array();
  684. foreach($aAvailableLanguages as $sLangCode => $aInfo)
  685. {
  686. $aLanguageCodes[$sLangCode] = $aInfo['description'].' ('.$aInfo['localized_description'].')';
  687. }
  688. $aParams["allowed_values"] = new ValueSetEnum($aLanguageCodes);
  689. parent::__construct($sCode, $aParams);
  690. }
  691. public function RequiresIndex()
  692. {
  693. return true;
  694. }
  695. }
  696. /**
  697. * The attribute dedicated to the finalclass automatic attribute
  698. *
  699. * @package iTopORM
  700. */
  701. class AttributeFinalClass extends AttributeString
  702. {
  703. public function __construct($sCode, $aParams)
  704. {
  705. $this->m_sCode = $sCode;
  706. $aParams["allowed_values"] = null;
  707. parent::__construct($sCode, $aParams);
  708. $this->m_sValue = $this->Get("default_value");
  709. }
  710. public function IsWritable()
  711. {
  712. return false;
  713. }
  714. public function RequiresIndex()
  715. {
  716. return true;
  717. }
  718. public function SetFixedValue($sValue)
  719. {
  720. $this->m_sValue = $sValue;
  721. }
  722. public function GetDefaultValue()
  723. {
  724. return $this->m_sValue;
  725. }
  726. public function GetAsHTML($sValue)
  727. {
  728. return MetaModel::GetName($sValue);
  729. }
  730. }
  731. /**
  732. * Map a varchar column (size < ?) to an attribute that must never be shown to the user
  733. *
  734. * @package iTopORM
  735. */
  736. class AttributePassword extends AttributeString
  737. {
  738. static protected function ListExpectedParams()
  739. {
  740. return parent::ListExpectedParams();
  741. //return array_merge(parent::ListExpectedParams(), array());
  742. }
  743. public function GetEditClass() {return "Password";}
  744. protected function GetSQLCol() {return "VARCHAR(64)";}
  745. public function GetFilterDefinitions()
  746. {
  747. // Note: due to this, you will get an error if a password is being declared as a search criteria (see ZLists)
  748. // not allowed to search on passwords!
  749. return array();
  750. }
  751. public function GetAsHTML($sValue)
  752. {
  753. if (strlen($sValue) == 0)
  754. {
  755. return '';
  756. }
  757. else
  758. {
  759. return '******';
  760. }
  761. }
  762. }
  763. /**
  764. * Map a text column (size > ?) to an attribute
  765. *
  766. * @package iTopORM
  767. */
  768. class AttributeText extends AttributeString
  769. {
  770. public function GetType() {return "Text";}
  771. public function GetTypeDesc() {return "Multiline character string";}
  772. public function GetEditClass() {return "Text";}
  773. protected function GetSQLCol() {return "TEXT";}
  774. public function GetAsHTML($sValue)
  775. {
  776. return str_replace("\n", "<br>\n", parent::GetAsHTML($sValue));
  777. }
  778. public function GetAsXML($value)
  779. {
  780. return Str::pure2xml($value);
  781. }
  782. }
  783. /**
  784. * Specialization of a string: email
  785. *
  786. * @package iTopORM
  787. */
  788. class AttributeEmailAddress extends AttributeString
  789. {
  790. public function GetTypeDesc() {return "Email address(es)";}
  791. public function GetValidationPattern()
  792. {
  793. return "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
  794. }
  795. }
  796. /**
  797. * Specialization of a string: IP address
  798. *
  799. * @package iTopORM
  800. */
  801. class AttributeIPAddress extends AttributeString
  802. {
  803. public function GetTypeDesc() {return "IP address";}
  804. public function GetValidationPattern()
  805. {
  806. $sNum = '(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])';
  807. return "^($sNum\\.$sNum\\.$sNum\\.$sNum)$";
  808. }
  809. }
  810. /**
  811. * Specialization of a string: OQL expression
  812. *
  813. * @package iTopORM
  814. */
  815. class AttributeOQL extends AttributeString
  816. {
  817. public function GetTypeDesc() {return "OQL expression";}
  818. }
  819. /**
  820. * Specialization of a string: template
  821. *
  822. * @package iTopORM
  823. */
  824. class AttributeTemplateString extends AttributeString
  825. {
  826. public function GetTypeDesc() {return "Template string";}
  827. }
  828. /**
  829. * Specialization of a text: template
  830. *
  831. * @package iTopORM
  832. */
  833. class AttributeTemplateText extends AttributeText
  834. {
  835. public function GetTypeDesc() {return "Multiline template string";}
  836. }
  837. /**
  838. * Specialization of a text: wiki formatting
  839. *
  840. * @package iTopORM
  841. */
  842. class AttributeWikiText extends AttributeText
  843. {
  844. public function GetTypeDesc() {return "Multiline string with special formatting such as links to objects";}
  845. public function GetAsHTML($value)
  846. {
  847. // [SELECT xxxx.... [label]] => hyperlink to a result list
  848. // {SELECT xxxx.... [label]} => result list displayed inline
  849. // [myclass/nnn [label]] => hyperlink to an object
  850. // {myclass/nnn/attcode} => attribute displayed inline
  851. // etc.
  852. return parent::GetAsHTML($value);
  853. }
  854. }
  855. /**
  856. * Map a enum column to an attribute
  857. *
  858. * @package iTopORM
  859. */
  860. class AttributeEnum extends AttributeString
  861. {
  862. static protected function ListExpectedParams()
  863. {
  864. return parent::ListExpectedParams();
  865. //return array_merge(parent::ListExpectedParams(), array());
  866. }
  867. public function GetType() {return "Enum";}
  868. public function GetTypeDesc() {return "List of predefined alphanumeric strings";}
  869. public function GetEditClass() {return "String";}
  870. protected function GetSQLCol()
  871. {
  872. $oValDef = $this->GetValuesDef();
  873. if ($oValDef)
  874. {
  875. $aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
  876. }
  877. else
  878. {
  879. $aValues = array();
  880. }
  881. if (count($aValues) > 0)
  882. {
  883. // The syntax used here do matters
  884. // In particular, I had to remove unnecessary spaces to
  885. // make sure that this string will match the field type returned by the DB
  886. // (used to perform a comparison between the current DB format and the data model)
  887. return "ENUM(".implode(",", $aValues).")";
  888. }
  889. else
  890. {
  891. return "VARCHAR(255)"; // ENUM() is not an allowed syntax!
  892. }
  893. }
  894. public function RequiresIndex()
  895. {
  896. return false;
  897. }
  898. public function GetBasicFilterOperators()
  899. {
  900. return parent::GetBasicFilterOperators();
  901. }
  902. public function GetBasicFilterLooseOperator()
  903. {
  904. return parent::GetBasicFilterLooseOperator();
  905. }
  906. public function GetBasicFilterSQLExpr($sOpCode, $value)
  907. {
  908. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  909. }
  910. public function GetAsHTML($sValue)
  911. {
  912. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  913. $sDescription = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue.'+', $sValue);
  914. // later, we could imagine a detailed description in the title
  915. return "<span title=\"$sDescription\">".parent::GetAsHtml($sLabel)."</span>";
  916. }
  917. public function GetEditValue($sValue)
  918. {
  919. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  920. return $sLabel;
  921. }
  922. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  923. {
  924. $aRawValues = parent::GetAllowedValues($aArgs, $sBeginsWith);
  925. if (is_null($aRawValues)) return null;
  926. $aLocalizedValues = array();
  927. foreach ($aRawValues as $sKey => $sValue)
  928. {
  929. $aLocalizedValues[$sKey] = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sKey, $sKey);
  930. }
  931. return $aLocalizedValues;
  932. }
  933. }
  934. /**
  935. * Map a date+time column to an attribute
  936. *
  937. * @package iTopORM
  938. */
  939. class AttributeDateTime extends AttributeDBField
  940. {
  941. //const MYDATETIMEZONE = "UTC";
  942. const MYDATETIMEZONE = "Europe/Paris";
  943. static protected $const_TIMEZONE = null; // set once for all upon object construct
  944. static public function InitStatics()
  945. {
  946. // Init static constant once for all (remove when PHP allows real static const)
  947. self::$const_TIMEZONE = new DateTimeZone(self::MYDATETIMEZONE);
  948. // #@# Init default timezone -> do not get a notice... to be improved !!!
  949. // duplicated in the email test page (the mail function does trigger a notice as well)
  950. date_default_timezone_set(self::MYDATETIMEZONE);
  951. }
  952. static protected function GetDateFormat()
  953. {
  954. return "Y-m-d H:i:s";
  955. }
  956. static protected function ListExpectedParams()
  957. {
  958. return parent::ListExpectedParams();
  959. //return array_merge(parent::ListExpectedParams(), array());
  960. }
  961. public function GetType() {return "Date";}
  962. public function GetTypeDesc() {return "Date and time";}
  963. public function GetEditClass() {return "DateTime";}
  964. protected function GetSQLCol() {return "TIMESTAMP";}
  965. public static function GetAsUnixSeconds($value)
  966. {
  967. $oDeadlineDateTime = new DateTime($value, self::$const_TIMEZONE);
  968. $iUnixSeconds = $oDeadlineDateTime->format('U');
  969. return $iUnixSeconds;
  970. }
  971. // #@# THIS HAS TO REVISED
  972. // Having null not allowed was interpreted by mySQL
  973. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  974. // Then, on each update of the record, the field was modified.
  975. // We will have to specify the default value if we want to restore this option
  976. // In fact, we could also have more verbs dedicated to the DB:
  977. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  978. public function IsNullAllowed() {return true;}
  979. public function GetDefaultValue()
  980. {
  981. $default = parent::GetDefaultValue();
  982. if (!parent::IsNullAllowed())
  983. {
  984. if (empty($default))
  985. {
  986. $default = date(self::GetDateFormat());
  987. }
  988. }
  989. return $default;
  990. }
  991. // END OF THE WORKAROUND
  992. ///////////////////////////////////////////////////////////////
  993. public function GetValidationPattern()
  994. {
  995. return "^([0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30))))( (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])){0,1}|0000-00-00 00:00:00|0000-00-00$";
  996. }
  997. public function GetBasicFilterOperators()
  998. {
  999. return array(
  1000. "="=>"equals",
  1001. "!="=>"differs from",
  1002. "<"=>"before",
  1003. "<="=>"before",
  1004. ">"=>"after (strictly)",
  1005. ">="=>"after",
  1006. "SameDay"=>"same day (strip time)",
  1007. "SameMonth"=>"same year/month",
  1008. "SameYear"=>"same year",
  1009. "Today"=>"today",
  1010. ">|"=>"after today + N days",
  1011. "<|"=>"before today + N days",
  1012. "=|"=>"equals today + N days",
  1013. );
  1014. }
  1015. public function GetBasicFilterLooseOperator()
  1016. {
  1017. // Unless we implement a "same xxx, depending on given precision" !
  1018. return "=";
  1019. }
  1020. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1021. {
  1022. $sQValue = CMDBSource::Quote($value);
  1023. switch ($sOpCode)
  1024. {
  1025. case '=':
  1026. case '!=':
  1027. case '<':
  1028. case '<=':
  1029. case '>':
  1030. case '>=':
  1031. return $this->GetSQLExpr()." $sOpCode $sQValue";
  1032. case 'SameDay':
  1033. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  1034. case 'SameMonth':
  1035. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  1036. case 'SameYear':
  1037. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  1038. case 'Today':
  1039. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  1040. case '>|':
  1041. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1042. case '<|':
  1043. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1044. case '=|':
  1045. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1046. default:
  1047. return $this->GetSQLExpr()." = $sQValue";
  1048. }
  1049. }
  1050. public function MakeRealValue($proposedValue)
  1051. {
  1052. if (is_null($proposedValue))
  1053. {
  1054. return null;
  1055. }
  1056. if (is_string($proposedValue) && ($proposedValue == "") && $this->IsNullAllowed())
  1057. {
  1058. return null;
  1059. }
  1060. if (!is_numeric($proposedValue))
  1061. {
  1062. return $proposedValue;
  1063. }
  1064. return date(self::GetDateFormat(), $proposedValue);
  1065. }
  1066. public function ScalarToSQL($value)
  1067. {
  1068. if (is_null($value))
  1069. {
  1070. return null;
  1071. }
  1072. elseif (empty($value))
  1073. {
  1074. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  1075. return '0000-00-00 00:00:00';
  1076. }
  1077. return $value;
  1078. }
  1079. public function GetAsHTML($value)
  1080. {
  1081. return Str::pure2html($value);
  1082. }
  1083. public function GetAsXML($value)
  1084. {
  1085. return Str::pure2xml($value);
  1086. }
  1087. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  1088. {
  1089. $sFrom = array("\r\n", $sTextQualifier);
  1090. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  1091. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  1092. return '"'.$sEscaped.'"';
  1093. }
  1094. /**
  1095. * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
  1096. * Each derived class is reponsible for defining and processing their own smart patterns, the base class
  1097. * does nothing special, and just calls the default (loose) operator
  1098. * @param string $sSearchText The search string to analyze for smart patterns
  1099. * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
  1100. * @param Hash $aParams Values of the query parameters
  1101. * @return Expression The search condition to be added (AND) to the current search
  1102. */
  1103. public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
  1104. {
  1105. // Possible smart patterns
  1106. $aPatterns = array(
  1107. 'between' => array('pattern' => '/^\[(.*),(.*)\]$/', 'operator' => 'n/a'),
  1108. 'greater than or equal' => array('pattern' => '/^>=(.*)$/', 'operator' => '>='),
  1109. 'greater than' => array('pattern' => '/^>(.*)$/', 'operator' => '>'),
  1110. 'less than or equal' => array('pattern' => '/^<=(.*)$/', 'operator' => '<='),
  1111. 'less than' => array('pattern' => '/^<(.*)$/', 'operator' => '<'),
  1112. );
  1113. $sPatternFound = '';
  1114. $aMatches = array();
  1115. foreach($aPatterns as $sPatName => $sPattern)
  1116. {
  1117. if (preg_match($sPattern['pattern'], $sSearchText, $aMatches))
  1118. {
  1119. $sPatternFound = $sPatName;
  1120. break;
  1121. }
  1122. }
  1123. switch($sPatternFound)
  1124. {
  1125. case 'between':
  1126. $sParamName1 = $oField->GetParent().'_'.$oField->GetName().'_1';
  1127. $oRightExpr = new VariableExpression($sParamName1);
  1128. $aParams[$sParamName1] = $aMatches[1];
  1129. $oCondition1 = new BinaryExpression($oField, '>=', $oRightExpr);
  1130. $sParamName2 = $oField->GetParent().'_'.$oField->GetName().'_2';
  1131. $oRightExpr = new VariableExpression($sParamName2);
  1132. $sOperator = $this->GetBasicFilterLooseOperator();
  1133. $aParams[$sParamName2] = $aMatches[2];
  1134. $oCondition2 = new BinaryExpression($oField, '<=', $oRightExpr);
  1135. $oNewCondition = new BinaryExpression($oCondition1, 'AND', $oCondition2);
  1136. break;
  1137. case 'greater than':
  1138. case 'greater than or equal':
  1139. case 'less than':
  1140. case 'less than or equal':
  1141. $sSQLOperator = $aPatterns[$sPatternFound]['operator'];
  1142. $sParamName = $oField->GetParent().'_'.$oField->GetName();
  1143. $oRightExpr = new VariableExpression($sParamName);
  1144. $aParams[$sParamName] = $aMatches[1];
  1145. $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
  1146. break;
  1147. default:
  1148. $oNewCondition = parent::GetSmartConditionExpression($sSearchText, $oField, $aParams);
  1149. }
  1150. return $oNewCondition;
  1151. }
  1152. }
  1153. /**
  1154. * Map a date+time column to an attribute
  1155. *
  1156. * @package iTopORM
  1157. */
  1158. class AttributeDate extends AttributeDateTime
  1159. {
  1160. const MYDATEFORMAT = "Y-m-d";
  1161. static protected function GetDateFormat()
  1162. {
  1163. return "Y-m-d";
  1164. }
  1165. static public function InitStatics()
  1166. {
  1167. // Nothing to do...
  1168. }
  1169. static protected function ListExpectedParams()
  1170. {
  1171. return parent::ListExpectedParams();
  1172. //return array_merge(parent::ListExpectedParams(), array());
  1173. }
  1174. public function GetType() {return "Date";}
  1175. public function GetTypeDesc() {return "Date";}
  1176. public function GetEditClass() {return "Date";}
  1177. protected function GetSQLCol() {return "DATE";}
  1178. public function GetValidationPattern()
  1179. {
  1180. return "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
  1181. }
  1182. }
  1183. // Init static constant once for all (remove when PHP allows real static const)
  1184. AttributeDate::InitStatics();
  1185. /**
  1186. * A dead line stored as a date & time
  1187. * The only difference with the DateTime attribute is the display:
  1188. * relative to the current time
  1189. */
  1190. class AttributeDeadline extends AttributeDateTime
  1191. {
  1192. public function GetAsHTML($value)
  1193. {
  1194. $sResult = '';
  1195. if ($value !== null)
  1196. {
  1197. $value = AttributeDateTime::GetAsUnixSeconds($value);
  1198. $difference = $value - time();
  1199. if ($difference >= 0)
  1200. {
  1201. $sResult = self::FormatDuration($difference);
  1202. }
  1203. else
  1204. {
  1205. $sResult = Dict::Format('UI:DeadlineMissedBy_duration', self::FormatDuration(-$difference));
  1206. }
  1207. }
  1208. return $sResult;
  1209. }
  1210. static function FormatDuration($duration)
  1211. {
  1212. $days = floor($duration / 86400);
  1213. $hours = floor(($duration - (86400*$days)) / 3600);
  1214. $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
  1215. $sResult = '';
  1216. if ($duration < 60)
  1217. {
  1218. // Less than 1 min
  1219. $sResult =Dict::S('UI:Deadline_LessThan1Min');
  1220. }
  1221. else if ($duration < 3600)
  1222. {
  1223. // less than 1 hour, display it in minutes
  1224. $sResult =Dict::Format('UI:Deadline_Minutes', $minutes);
  1225. }
  1226. else if ($duration < 86400)
  1227. {
  1228. // Less that 1 day, display it in hours/minutes
  1229. $sResult =Dict::Format('UI:Deadline_Hours_Minutes', $hours, $minutes);
  1230. }
  1231. else
  1232. {
  1233. // Less that 1 day, display it in hours/minutes
  1234. $sResult =Dict::Format('UI:Deadline_Days_Hours_Minutes', $days, $hours, $minutes);
  1235. }
  1236. return $sResult;
  1237. }
  1238. }
  1239. // Init static constant once for all (remove when PHP allows real static const)
  1240. AttributeDateTime::InitStatics();
  1241. /**
  1242. * Map a foreign key to an attribute
  1243. * AttributeExternalKey and AttributeExternalField may be an external key
  1244. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  1245. * where an AttributeExternalField corresponds to a column into another table (class)
  1246. *
  1247. * @package iTopORM
  1248. */
  1249. class AttributeExternalKey extends AttributeDBFieldVoid
  1250. {
  1251. static protected function ListExpectedParams()
  1252. {
  1253. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed", "on_target_delete"));
  1254. }
  1255. public function GetType() {return "Extkey";}
  1256. public function GetTypeDesc() {return "Link to another object";}
  1257. public function GetEditClass() {return "ExtKey";}
  1258. protected function GetSQLCol() {return "INT(11)";}
  1259. public function RequiresIndex()
  1260. {
  1261. return true;
  1262. }
  1263. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  1264. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  1265. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  1266. public function GetKeyAttCode() {return $this->GetCode();}
  1267. public function GetDefaultValue() {return 0;}
  1268. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  1269. public function GetBasicFilterOperators()
  1270. {
  1271. return parent::GetBasicFilterOperators();
  1272. }
  1273. public function GetBasicFilterLooseOperator()
  1274. {
  1275. return parent::GetBasicFilterLooseOperator();
  1276. }
  1277. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1278. {
  1279. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  1280. }
  1281. // overloaded here so that an ext key always have the answer to
  1282. // "what are your possible values?"
  1283. public function GetValuesDef()
  1284. {
  1285. $oValSetDef = $this->Get("allowed_values");
  1286. if (!$oValSetDef)
  1287. {
  1288. // Let's propose every existing value
  1289. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1290. }
  1291. return $oValSetDef;
  1292. }
  1293. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  1294. {
  1295. try
  1296. {
  1297. return parent::GetAllowedValues($aArgs, $sBeginsWith);
  1298. }
  1299. catch (MissingQueryArgument $e)
  1300. {
  1301. // Some required arguments could not be found, enlarge to any existing value
  1302. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1303. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  1304. }
  1305. }
  1306. public function GetDeletionPropagationOption()
  1307. {
  1308. return $this->Get("on_target_delete");
  1309. }
  1310. public function GetNullValue()
  1311. {
  1312. return 0;
  1313. }
  1314. public function IsNull($proposedValue)
  1315. {
  1316. return ($proposedValue == 0);
  1317. }
  1318. public function MakeRealValue($proposedValue)
  1319. {
  1320. if (is_null($proposedValue)) return 0;
  1321. if (MetaModel::IsValidObject($proposedValue)) return $proposedValue->GetKey();
  1322. return (int)$proposedValue;
  1323. }
  1324. }
  1325. /**
  1326. * An attribute which corresponds to an external key (direct or indirect)
  1327. *
  1328. * @package iTopORM
  1329. */
  1330. class AttributeExternalField extends AttributeDefinition
  1331. {
  1332. static protected function ListExpectedParams()
  1333. {
  1334. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  1335. }
  1336. public function GetType() {return "ExtkeyField";}
  1337. public function GetTypeDesc() {return "Field of an object pointed to by the current object";}
  1338. public function GetEditClass() {return "ExtField";}
  1339. protected function GetSQLCol()
  1340. {
  1341. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  1342. $oExtAttDef = $this->GetExtAttDef();
  1343. return $oExtAttDef->GetSQLCol();
  1344. }
  1345. public function GetLabel()
  1346. {
  1347. $oRemoteAtt = $this->GetExtAttDef();
  1348. $sDefault = $oRemoteAtt->GetLabel();
  1349. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $sDefault);
  1350. }
  1351. public function GetDescription()
  1352. {
  1353. $oRemoteAtt = $this->GetExtAttDef();
  1354. $sDefault = $oRemoteAtt->GetDescription();
  1355. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', $sDefault);
  1356. }
  1357. public function GetHelpOnEdition()
  1358. {
  1359. $oRemoteAtt = $this->GetExtAttDef();
  1360. $sDefault = $oRemoteAtt->GetHelpOnEdition();
  1361. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'?', $sDefault);
  1362. }
  1363. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  1364. {
  1365. switch($iType)
  1366. {
  1367. case EXTKEY_ABSOLUTE:
  1368. // see further
  1369. $oRemoteAtt = $this->GetExtAttDef();
  1370. return $oRemoteAtt->IsExternalKey($iType);
  1371. case EXTKEY_RELATIVE:
  1372. return false;
  1373. default:
  1374. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1375. }
  1376. }
  1377. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  1378. {
  1379. return $this->GetKeyAttDef($iType)->GetTargetClass();
  1380. }
  1381. public function IsExternalField() {return true;}
  1382. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  1383. public function GetExtAttCode() {return $this->Get("target_attcode");}
  1384. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  1385. {
  1386. switch($iType)
  1387. {
  1388. case EXTKEY_ABSOLUTE:
  1389. // see further
  1390. $oRemoteAtt = $this->GetExtAttDef();
  1391. if ($oRemoteAtt->IsExternalField())
  1392. {
  1393. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  1394. }
  1395. else if ($oRemoteAtt->IsExternalKey())
  1396. {
  1397. return $oRemoteAtt;
  1398. }
  1399. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  1400. case EXTKEY_RELATIVE:
  1401. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  1402. default:
  1403. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1404. }
  1405. }
  1406. public function GetExtAttDef()
  1407. {
  1408. $oKeyAttDef = $this->GetKeyAttDef();
  1409. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  1410. if (!is_object($oExtAttDef)) throw new CoreException("Invalid external field ".$this->GetCode()." in class ".$this->GetHostClass().". The class ".$oKeyAttDef->Get("targetclass")." has no attribute ".$this->Get("target_attcode"));
  1411. return $oExtAttDef;
  1412. }
  1413. public function GetSQLExpr()
  1414. {
  1415. $oExtAttDef = $this->GetExtAttDef();
  1416. return $oExtAttDef->GetSQLExpr();
  1417. }
  1418. public function GetDefaultValue()
  1419. {
  1420. $oExtAttDef = $this->GetExtAttDef();
  1421. return $oExtAttDef->GetDefaultValue();
  1422. }
  1423. public function IsNullAllowed()
  1424. {
  1425. $oExtAttDef = $this->GetExtAttDef();
  1426. return $oExtAttDef->IsNullAllowed();
  1427. }
  1428. public function IsScalar()
  1429. {
  1430. $oExtAttDef = $this->GetExtAttDef();
  1431. return $oExtAttDef->IsScalar();
  1432. }
  1433. public function GetFilterDefinitions()
  1434. {
  1435. return array($this->GetCode() => new FilterFromAttribute($this));
  1436. }
  1437. public function GetBasicFilterOperators()
  1438. {
  1439. $oExtAttDef = $this->GetExtAttDef();
  1440. return $oExtAttDef->GetBasicFilterOperators();
  1441. }
  1442. public function GetBasicFilterLooseOperator()
  1443. {
  1444. $oExtAttDef = $this->GetExtAttDef();
  1445. return $oExtAttDef->GetBasicFilterLooseOperator();
  1446. }
  1447. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1448. {
  1449. $oExtAttDef = $this->GetExtAttDef();
  1450. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  1451. }
  1452. public function GetNullValue()
  1453. {
  1454. $oExtAttDef = $this->GetExtAttDef();
  1455. return $oExtAttDef->GetNullValue();
  1456. }
  1457. public function IsNull($proposedValue)
  1458. {
  1459. $oExtAttDef = $this->GetExtAttDef();
  1460. return $oExtAttDef->IsNull($proposedValue);
  1461. }
  1462. public function MakeRealValue($proposedValue)
  1463. {
  1464. $oExtAttDef = $this->GetExtAttDef();
  1465. return $oExtAttDef->MakeRealValue($proposedValue);
  1466. }
  1467. public function ScalarToSQL($value)
  1468. {
  1469. // This one could be used in case of filtering only
  1470. $oExtAttDef = $this->GetExtAttDef();
  1471. return $oExtAttDef->ScalarToSQL($value);
  1472. }
  1473. // Do not overload GetSQLExpression here because this is handled in the joins
  1474. //public function GetSQLExpressions() {return array();}
  1475. // Here, we get the data...
  1476. public function FromSQLToValue($aCols, $sPrefix = '')
  1477. {
  1478. $oExtAttDef = $this->GetExtAttDef();
  1479. return $oExtAttDef->FromSQLToValue($aCols, $sPrefix);
  1480. }
  1481. public function GetAsHTML($value)
  1482. {
  1483. $oExtAttDef = $this->GetExtAttDef();
  1484. return $oExtAttDef->GetAsHTML($value);
  1485. }
  1486. public function GetAsXML($value)
  1487. {
  1488. $oExtAttDef = $this->GetExtAttDef();
  1489. return $oExtAttDef->GetAsXML($value);
  1490. }
  1491. public function GetAsCSV($value, $sSeparator = ',', $sTestQualifier = '"')
  1492. {
  1493. $oExtAttDef = $this->GetExtAttDef();
  1494. return $oExtAttDef->GetAsCSV($value, $sSeparator, $sTestQualifier);
  1495. }
  1496. }
  1497. /**
  1498. * Map a varchar column to an URL (formats the ouput in HMTL)
  1499. *
  1500. * @package iTopORM
  1501. */
  1502. class AttributeURL extends AttributeString
  1503. {
  1504. static protected function ListExpectedParams()
  1505. {
  1506. //return parent::ListExpectedParams();
  1507. return array_merge(parent::ListExpectedParams(), array("target"));
  1508. }
  1509. public function GetType() {return "Url";}
  1510. public function GetTypeDesc() {return "Absolute or relative URL as a text string";}
  1511. public function GetEditClass() {return "String";}
  1512. public function GetAsHTML($sValue)
  1513. {
  1514. $sTarget = $this->Get("target");
  1515. if (empty($sTarget)) $sTarget = "_blank";
  1516. $sLabel = Str::pure2html($sValue);
  1517. if (strlen($sLabel) > 40)
  1518. {
  1519. // Truncate the length to about 40 characters, by removing the middle
  1520. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  1521. }
  1522. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  1523. }
  1524. public function GetValidationPattern()
  1525. {
  1526. return "^(http|https|ftp)\://[a-zA-Z0-9\-\.]+(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$";
  1527. }
  1528. }
  1529. /**
  1530. * A blob is an ormDocument, it is stored as several columns in the database
  1531. *
  1532. * @package iTopORM
  1533. */
  1534. class AttributeBlob extends AttributeDefinition
  1535. {
  1536. static protected function ListExpectedParams()
  1537. {
  1538. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  1539. }
  1540. public function GetType() {return "Blob";}
  1541. public function GetTypeDesc() {return "Document";}
  1542. public function GetEditClass() {return "Document";}
  1543. public function IsDirectField() {return true;}
  1544. public function IsScalar() {return true;}
  1545. public function IsWritable() {return true;}
  1546. public function GetDefaultValue() {return "";}
  1547. public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
  1548. // Facilitate things: allow the user to Set the value from a string
  1549. public function MakeRealValue($proposedValue)
  1550. {
  1551. if (!is_object($proposedValue))
  1552. {
  1553. return new ormDocument($proposedValue, 'text/plain');
  1554. }
  1555. return $proposedValue;
  1556. }
  1557. public function GetSQLExpressions()
  1558. {
  1559. $aColumns = array();
  1560. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  1561. $aColumns[''] = $this->GetCode().'_mimetype';
  1562. $aColumns['_data'] = $this->GetCode().'_data';
  1563. $aColumns['_filename'] = $this->GetCode().'_filename';
  1564. return $aColumns;
  1565. }
  1566. public function FromSQLToValue($aCols, $sPrefix = '')
  1567. {
  1568. if (!isset($aCols[$sPrefix]))
  1569. {
  1570. $sAvailable = implode(', ', array_keys($aCols));
  1571. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  1572. }
  1573. $sMimeType = $aCols[$sPrefix];
  1574. if (!isset($aCols[$sPrefix.'_data']))
  1575. {
  1576. $sAvailable = implode(', ', array_keys($aCols));
  1577. throw new MissingColumnException("Missing column '".$sPrefix."_data' from {$sAvailable}");
  1578. }
  1579. $data = $aCols[$sPrefix.'_data'];
  1580. if (!isset($aCols[$sPrefix.'_filename']))
  1581. {
  1582. $sAvailable = implode(', ', array_keys($aCols));
  1583. throw new MissingColumnException("Missing column '".$sPrefix."_filename' from {$sAvailable}");
  1584. }
  1585. $sFileName = $aCols[$sPrefix.'_filename'];
  1586. $value = new ormDocument($data, $sMimeType, $sFileName);
  1587. return $value;
  1588. }
  1589. public function GetSQLValues($value)
  1590. {
  1591. // #@# Optimization: do not load blobs anytime
  1592. // As per mySQL doc, selecting blob columns will prevent mySQL from
  1593. // using memory in case a temporary table has to be created
  1594. // (temporary tables created on disk)
  1595. // We will have to remove the blobs from the list of attributes when doing the select
  1596. // then the use of Get() should finalize the load
  1597. if ($value instanceOf ormDocument)
  1598. {
  1599. $aValues = array();
  1600. $aValues[$this->GetCode().'_data'] = $value->GetData();
  1601. $aValues[$this->GetCode().'_mimetype'] = $value->GetMimeType();
  1602. $aValues[$this->GetCode().'_filename'] = $value->GetFileName();
  1603. }
  1604. else
  1605. {
  1606. $aValues = array();
  1607. $aValues[$this->GetCode().'_data'] = '';
  1608. $aValues[$this->GetCode().'_mimetype'] = '';
  1609. $aValues[$this->GetCode().'_filename'] = '';
  1610. }
  1611. return $aValues;
  1612. }
  1613. public function GetSQLColumns()
  1614. {
  1615. $aColumns = array();
  1616. $aColumns[$this->GetCode().'_data'] = 'LONGBLOB'; // 2^32 (4 Gb)
  1617. $aColumns[$this->GetCode().'_mimetype'] = 'VARCHAR(255)';
  1618. $aColumns[$this->GetCode().'_filename'] = 'VARCHAR(255)';
  1619. return $aColumns;
  1620. }
  1621. public function GetFilterDefinitions()
  1622. {
  1623. return array();
  1624. // still not working... see later...
  1625. return array(
  1626. $this->GetCode().'->filename' => new FilterFromAttribute($this, '_filename'),
  1627. $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype'),
  1628. $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype')
  1629. );
  1630. }
  1631. public function GetBasicFilterOperators()
  1632. {
  1633. return array();
  1634. }
  1635. public function GetBasicFilterLooseOperator()
  1636. {
  1637. return '=';
  1638. }
  1639. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1640. {
  1641. return 'true';
  1642. }
  1643. public function GetAsHTML($value)
  1644. {
  1645. if (is_object($value))
  1646. {
  1647. return $value->GetAsHTML();
  1648. }
  1649. }
  1650. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  1651. {
  1652. return ''; // Not exportable in CSV !
  1653. }
  1654. public function GetAsXML($value)
  1655. {
  1656. return ''; // Not exportable in XML, or as CDATA + some subtags ??
  1657. }
  1658. }
  1659. // Indexed array having two dimensions
  1660. class AttributeTable extends AttributeText
  1661. {
  1662. public function GetType() {return "Table";}
  1663. public function GetTypeDesc() {return "Array with 2 dimensions";}
  1664. public function GetEditClass() {return "Text";}
  1665. protected function GetSQLCol() {return "TEXT";}
  1666. // Facilitate things: allow the user to Set the value from a string
  1667. public function MakeRealValue($proposedValue)
  1668. {
  1669. if (!is_array($proposedValue))
  1670. {
  1671. return array(0 => array(0 => $proposedValue));
  1672. }
  1673. return $proposedValue;
  1674. }
  1675. public function FromSQLToValue($aCols, $sPrefix = '')
  1676. {
  1677. try
  1678. {
  1679. $value = @unserialize($aCols[$sPrefix.'']);
  1680. if ($value === false)
  1681. {
  1682. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  1683. }
  1684. }
  1685. catch(Exception $e)
  1686. {
  1687. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  1688. }
  1689. return $value;
  1690. }
  1691. public function GetSQLValues($value)
  1692. {
  1693. $aValues = array();
  1694. $aValues[$this->Get("sql")] = serialize($value);
  1695. return $aValues;
  1696. }
  1697. public function GetAsHTML($value)
  1698. {
  1699. if (!is_array($value))
  1700. {
  1701. throw new CoreException('Expecting an array', array('found' => get_class($value)));
  1702. }
  1703. if (count($value) == 0)
  1704. {
  1705. return "";
  1706. }
  1707. $sRes = "<TABLE class=\"listResults\">";
  1708. $sRes .= "<TBODY>";
  1709. foreach($value as $iRow => $aRawData)
  1710. {
  1711. $sRes .= "<TR>";
  1712. foreach ($aRawData as $iCol => $cell)
  1713. {
  1714. $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$cell));
  1715. $sRes .= "<TD>$sCell</TD>";
  1716. }
  1717. $sRes .= "</TR>";
  1718. }
  1719. $sRes .= "</TBODY>";
  1720. $sRes .= "</TABLE>";
  1721. return $sRes;
  1722. }
  1723. }
  1724. // The PHP value is a hash array, it is stored as a TEXT column
  1725. class AttributePropertySet extends AttributeTable
  1726. {
  1727. public function GetType() {return "PropertySet";}
  1728. public function GetTypeDesc() {return "List of properties (name and value)";}
  1729. public function GetEditClass() {return "Text";}
  1730. protected function GetSQLCol() {return "TEXT";}
  1731. // Facilitate things: allow the user to Set the value from a string
  1732. public function MakeRealValue($proposedValue)
  1733. {
  1734. if (!is_array($proposedValue))
  1735. {
  1736. return array('?' => (string)$proposedValue);
  1737. }
  1738. return $proposedValue;
  1739. }
  1740. public function GetAsHTML($value)
  1741. {
  1742. if (!is_array($value))
  1743. {
  1744. throw new CoreException('Expecting an array', array('found' => get_class($value)));
  1745. }
  1746. if (count($value) == 0)
  1747. {
  1748. return "";
  1749. }
  1750. $sRes = "<TABLE class=\"listResults\">";
  1751. $sRes .= "<TBODY>";
  1752. foreach($value as $sProperty => $sValue)
  1753. {
  1754. $sRes .= "<TR>";
  1755. $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$sValue));
  1756. $sRes .= "<TD class=\"label\">$sProperty</TD><TD>$sCell</TD>";
  1757. $sRes .= "</TR>";
  1758. }
  1759. $sRes .= "</TBODY>";
  1760. $sRes .= "</TABLE>";
  1761. return $sRes;
  1762. }
  1763. }
  1764. ?>