attributedef.class.inc.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  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. require_once('ormpassword.class.inc.php');
  27. /**
  28. * MissingColumnException - sent if an attribute is being created but the column is missing in the row
  29. *
  30. * @package iTopORM
  31. */
  32. class MissingColumnException extends Exception
  33. {}
  34. /**
  35. * add some description here...
  36. *
  37. * @package iTopORM
  38. */
  39. define('EXTKEY_RELATIVE', 1);
  40. /**
  41. * add some description here...
  42. *
  43. * @package iTopORM
  44. */
  45. define('EXTKEY_ABSOLUTE', 2);
  46. /**
  47. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  48. *
  49. * @package iTopORM
  50. */
  51. define('DEL_MANUAL', 1);
  52. /**
  53. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  54. *
  55. * @package iTopORM
  56. */
  57. define('DEL_AUTO', 2);
  58. /**
  59. * Attribute definition API, implemented in and many flavours (Int, String, Enum, etc.)
  60. *
  61. * @package iTopORM
  62. */
  63. abstract class AttributeDefinition
  64. {
  65. public function GetType()
  66. {
  67. return Dict::S('Core:'.get_class($this));
  68. }
  69. public function GetTypeDesc()
  70. {
  71. return Dict::S('Core:'.get_class($this).'+');
  72. }
  73. abstract public function GetEditClass();
  74. protected $m_sCode;
  75. private $m_aParams = array();
  76. protected $m_sHostClass = '!undefined!';
  77. protected function Get($sParamName) {return $this->m_aParams[$sParamName];}
  78. protected function IsParam($sParamName) {return (array_key_exists($sParamName, $this->m_aParams));}
  79. protected function GetOptional($sParamName, $default)
  80. {
  81. if (array_key_exists($sParamName, $this->m_aParams))
  82. {
  83. return $this->m_aParams[$sParamName];
  84. }
  85. else
  86. {
  87. return $default;
  88. }
  89. }
  90. public function __construct($sCode, $aParams)
  91. {
  92. $this->m_sCode = $sCode;
  93. $this->m_aParams = $aParams;
  94. $this->ConsistencyCheck();
  95. }
  96. public function OverloadParams($aParams)
  97. {
  98. foreach ($aParams as $sParam => $value)
  99. {
  100. if (!array_key_exists($sParam, $this->m_aParams))
  101. {
  102. throw new CoreException("Unknown attribute definition parameter '$sParam', please select a value in {".implode(", ", array_keys($this->m_aParams))."}");
  103. }
  104. else
  105. {
  106. $this->m_aParams[$sParam] = $value;
  107. }
  108. }
  109. }
  110. public function SetHostClass($sHostClass)
  111. {
  112. $this->m_sHostClass = $sHostClass;
  113. }
  114. public function GetHostClass()
  115. {
  116. return $this->m_sHostClass;
  117. }
  118. // Note: I could factorize this code with the parameter management made for the AttributeDef class
  119. // to be overloaded
  120. static protected function ListExpectedParams()
  121. {
  122. return array();
  123. }
  124. private function ConsistencyCheck()
  125. {
  126. // Check that any mandatory param has been specified
  127. //
  128. $aExpectedParams = $this->ListExpectedParams();
  129. foreach($aExpectedParams as $sParamName)
  130. {
  131. if (!array_key_exists($sParamName, $this->m_aParams))
  132. {
  133. $aBacktrace = debug_backtrace();
  134. $sTargetClass = $aBacktrace[2]["class"];
  135. $sCodeInfo = $aBacktrace[1]["file"]." - ".$aBacktrace[1]["line"];
  136. throw new Exception("ERROR missing parameter '$sParamName' in ".get_class($this)." declaration for class $sTargetClass ($sCodeInfo)");
  137. }
  138. }
  139. }
  140. // table, key field, name field
  141. public function ListDBJoins()
  142. {
  143. return "";
  144. // e.g: return array("Site", "infrid", "name");
  145. }
  146. public function IsDirectField() {return false;}
  147. public function IsScalar() {return false;}
  148. public function IsLinkSet() {return false;}
  149. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return false;}
  150. public function IsExternalField() {return false;}
  151. public function IsWritable() {return false;}
  152. public function IsNullAllowed() {return true;}
  153. public function GetCode() {return $this->m_sCode;}
  154. public function GetLabel() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $this->m_sCode);}
  155. public function GetLabel_Obsolete()
  156. {
  157. // Written for compatibility with a data model written prior to version 0.9.1
  158. if (array_key_exists('label', $this->m_aParams))
  159. {
  160. return $this->m_aParams['label'];
  161. }
  162. else
  163. {
  164. return $this->GetLabel();
  165. }
  166. }
  167. public function GetDescription() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', '');}
  168. public function GetHelpOnEdition() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'?', '');}
  169. public function GetDescription_Obsolete()
  170. {
  171. // Written for compatibility with a data model written prior to version 0.9.1
  172. if (array_key_exists('description', $this->m_aParams))
  173. {
  174. return $this->m_aParams['description'];
  175. }
  176. else
  177. {
  178. return $this->GetDescription();
  179. }
  180. }
  181. public function GetValuesDef() {return null;}
  182. public function GetPrerequisiteAttributes() {return array();}
  183. public function GetNullValue() {return null;}
  184. public function IsNull($proposedValue) {return is_null($proposedValue);}
  185. public function MakeRealValue($proposedValue) {return $proposedValue;} // force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing!)
  186. public function GetSQLExpressions() {return array();} // returns suffix/expression pairs (1 in most of the cases), for READING (Select)
  187. public function FromSQLToValue($aCols, $sPrefix = '') {return null;} // returns a value out of suffix/value pairs, for SELECT result interpretation
  188. public function GetSQLColumns() {return array();} // returns column/spec pairs (1 in most of the cases), for STRUCTURING (DB creation)
  189. public function GetSQLValues($value) {return array();} // returns column/value pairs (1 in most of the cases), for WRITING (Insert, Update)
  190. public function RequiresIndex() {return false;}
  191. public function GetValidationPattern()
  192. {
  193. return '';
  194. }
  195. public function CheckFormat($value)
  196. {
  197. return true;
  198. }
  199. public function GetMaxSize()
  200. {
  201. return null;
  202. }
  203. public function MakeValue()
  204. {
  205. $sComputeFunc = $this->Get("compute_func");
  206. if (empty($sComputeFunc)) return null;
  207. return call_user_func($sComputeFunc);
  208. }
  209. abstract public function GetDefaultValue();
  210. //
  211. // To be overloaded in subclasses
  212. //
  213. abstract public function GetBasicFilterOperators(); // returns an array of "opCode"=>"description"
  214. abstract public function GetBasicFilterLooseOperator(); // returns an "opCode"
  215. //abstract protected GetBasicFilterHTMLInput();
  216. abstract public function GetBasicFilterSQLExpr($sOpCode, $value);
  217. public function GetFilterDefinitions()
  218. {
  219. return array();
  220. }
  221. public function GetEditValue($sValue)
  222. {
  223. return (string)$sValue;
  224. }
  225. public function GetAsHTML($sValue)
  226. {
  227. return Str::pure2html((string)$sValue);
  228. }
  229. public function GetAsXML($sValue)
  230. {
  231. return Str::pure2xml((string)$sValue);
  232. }
  233. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  234. {
  235. return (string)$sValue;
  236. }
  237. public function GetAllowedValues($aArgs = array(), $sContains = '')
  238. {
  239. $oValSetDef = $this->GetValuesDef();
  240. if (!$oValSetDef) return null;
  241. return $oValSetDef->GetValues($aArgs, $sContains);
  242. }
  243. /**
  244. * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
  245. * Each derived class is reponsible for defining and processing their own smart patterns, the base class
  246. * does nothing special, and just calls the default (loose) operator
  247. * @param string $sSearchText The search string to analyze for smart patterns
  248. * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
  249. * @param Hash $aParams Values of the query parameters
  250. * @return Expression The search condition to be added (AND) to the current search
  251. */
  252. public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
  253. {
  254. $sParamName = $oField->GetParent().'_'.$oField->GetName();
  255. $oRightExpr = new VariableExpression($sParamName);
  256. $sOperator = $this->GetBasicFilterLooseOperator();
  257. switch ($sOperator)
  258. {
  259. case 'Contains':
  260. $aParams[$sParamName] = "%$sSearchText%";
  261. $sSQLOperator = 'LIKE';
  262. break;
  263. default:
  264. $sSQLOperator = $sOperator;
  265. $aParams[$sParamName] = $sSearchText;
  266. }
  267. $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
  268. return $oNewCondition;
  269. }
  270. }
  271. /**
  272. * Set of objects directly linked to an object, and being part of its definition
  273. *
  274. * @package iTopORM
  275. */
  276. class AttributeLinkedSet extends AttributeDefinition
  277. {
  278. static protected function ListExpectedParams()
  279. {
  280. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "linked_class", "ext_key_to_me", "count_min", "count_max"));
  281. }
  282. public function GetEditClass() {return "List";}
  283. public function IsWritable() {return true;}
  284. public function IsLinkSet() {return true;}
  285. public function IsIndirect() {return false;}
  286. public function GetValuesDef() {return $this->Get("allowed_values");}
  287. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  288. public function GetDefaultValue($aArgs = array())
  289. {
  290. // Note: so far, this feature is a prototype,
  291. // later, the argument 'this' should always be present in the arguments
  292. //
  293. if (($this->IsParam('default_value')) && array_key_exists('this', $aArgs))
  294. {
  295. $aValues = $this->Get('default_value')->GetValues($aArgs);
  296. $oSet = DBObjectSet::FromArray($this->Get('linked_class'), $aValues);
  297. return $oSet;
  298. }
  299. else
  300. {
  301. return DBObjectSet::FromScratch($this->Get('linked_class'));
  302. }
  303. }
  304. public function GetLinkedClass() {return $this->Get('linked_class');}
  305. public function GetExtKeyToMe() {return $this->Get('ext_key_to_me');}
  306. public function GetBasicFilterOperators() {return array();}
  307. public function GetBasicFilterLooseOperator() {return '';}
  308. public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
  309. public function GetAsHTML($sValue)
  310. {
  311. return "ERROR: LIST OF OBJECTS";
  312. }
  313. public function GetAsXML($sValue)
  314. {
  315. return "ERROR: LIST OF OBJECTS";
  316. }
  317. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  318. {
  319. return "ERROR: LIST OF OBJECTS";
  320. }
  321. public function DuplicatesAllowed() {return false;} // No duplicates for 1:n links, never
  322. }
  323. /**
  324. * Set of objects linked to an object (n-n), and being part of its definition
  325. *
  326. * @package iTopORM
  327. */
  328. class AttributeLinkedSetIndirect extends AttributeLinkedSet
  329. {
  330. static protected function ListExpectedParams()
  331. {
  332. return array_merge(parent::ListExpectedParams(), array("ext_key_to_remote"));
  333. }
  334. public function IsIndirect() {return true;}
  335. public function GetExtKeyToRemote() { return $this->Get('ext_key_to_remote'); }
  336. public function GetEditClass() {return "LinkedSet";}
  337. public function DuplicatesAllowed() {return $this->GetOptional("duplicates", false);} // The same object may be linked several times... or not...
  338. }
  339. /**
  340. * Abstract class implementing default filters for a DB column
  341. *
  342. * @package iTopORM
  343. */
  344. class AttributeDBFieldVoid extends AttributeDefinition
  345. {
  346. static protected function ListExpectedParams()
  347. {
  348. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "sql"));
  349. }
  350. // To be overriden, used in GetSQLColumns
  351. protected function GetSQLCol() {return "VARCHAR(255)";}
  352. public function GetEditClass() {return "String";}
  353. public function GetValuesDef() {return $this->Get("allowed_values");}
  354. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  355. public function IsDirectField() {return true;}
  356. public function IsScalar() {return true;}
  357. public function IsWritable() {return true;}
  358. public function GetSQLExpr() {return $this->Get("sql");}
  359. public function GetDefaultValue() {return $this->MakeRealValue("");}
  360. public function IsNullAllowed() {return false;}
  361. //
  362. protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  363. public function GetSQLExpressions()
  364. {
  365. $aColumns = array();
  366. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  367. $aColumns[''] = $this->Get("sql");
  368. return $aColumns;
  369. }
  370. public function FromSQLToValue($aCols, $sPrefix = '')
  371. {
  372. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  373. return $value;
  374. }
  375. public function GetSQLValues($value)
  376. {
  377. $aValues = array();
  378. $aValues[$this->Get("sql")] = $this->ScalarToSQL($value);
  379. return $aValues;
  380. }
  381. public function GetSQLColumns()
  382. {
  383. $aColumns = array();
  384. $aColumns[$this->Get("sql")] = $this->GetSQLCol();
  385. return $aColumns;
  386. }
  387. public function GetFilterDefinitions()
  388. {
  389. return array($this->GetCode() => new FilterFromAttribute($this));
  390. }
  391. public function GetBasicFilterOperators()
  392. {
  393. return array("="=>"equals", "!="=>"differs from");
  394. }
  395. public function GetBasicFilterLooseOperator()
  396. {
  397. return "=";
  398. }
  399. public function GetBasicFilterSQLExpr($sOpCode, $value)
  400. {
  401. $sQValue = CMDBSource::Quote($value);
  402. switch ($sOpCode)
  403. {
  404. case '!=':
  405. return $this->GetSQLExpr()." != $sQValue";
  406. break;
  407. case '=':
  408. default:
  409. return $this->GetSQLExpr()." = $sQValue";
  410. }
  411. }
  412. }
  413. /**
  414. * Base class for all kind of DB attributes, with the exception of external keys
  415. *
  416. * @package iTopORM
  417. */
  418. class AttributeDBField extends AttributeDBFieldVoid
  419. {
  420. static protected function ListExpectedParams()
  421. {
  422. return array_merge(parent::ListExpectedParams(), array("default_value", "is_null_allowed"));
  423. }
  424. public function GetDefaultValue() {return $this->MakeRealValue($this->Get("default_value"));}
  425. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  426. }
  427. /**
  428. * Map an integer column to an attribute
  429. *
  430. * @package iTopORM
  431. */
  432. class AttributeInteger extends AttributeDBField
  433. {
  434. static protected function ListExpectedParams()
  435. {
  436. return parent::ListExpectedParams();
  437. //return array_merge(parent::ListExpectedParams(), array());
  438. }
  439. public function GetEditClass() {return "String";}
  440. protected function GetSQLCol() {return "INT(11)";}
  441. public function GetValidationPattern()
  442. {
  443. return "^[0-9]+$";
  444. }
  445. public function GetBasicFilterOperators()
  446. {
  447. return array(
  448. "!="=>"differs from",
  449. "="=>"equals",
  450. ">"=>"greater (strict) than",
  451. ">="=>"greater than",
  452. "<"=>"less (strict) than",
  453. "<="=>"less than",
  454. "in"=>"in"
  455. );
  456. }
  457. public function GetBasicFilterLooseOperator()
  458. {
  459. // Unless we implement an "equals approximately..." or "same order of magnitude"
  460. return "=";
  461. }
  462. public function GetBasicFilterSQLExpr($sOpCode, $value)
  463. {
  464. $sQValue = CMDBSource::Quote($value);
  465. switch ($sOpCode)
  466. {
  467. case '!=':
  468. return $this->GetSQLExpr()." != $sQValue";
  469. break;
  470. case '>':
  471. return $this->GetSQLExpr()." > $sQValue";
  472. break;
  473. case '>=':
  474. return $this->GetSQLExpr()." >= $sQValue";
  475. break;
  476. case '<':
  477. return $this->GetSQLExpr()." < $sQValue";
  478. break;
  479. case '<=':
  480. return $this->GetSQLExpr()." <= $sQValue";
  481. break;
  482. case 'in':
  483. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  484. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  485. break;
  486. case '=':
  487. default:
  488. return $this->GetSQLExpr()." = \"$value\"";
  489. }
  490. }
  491. public function GetNullValue()
  492. {
  493. return null;
  494. }
  495. public function IsNull($proposedValue)
  496. {
  497. return is_null($proposedValue);
  498. }
  499. public function MakeRealValue($proposedValue)
  500. {
  501. if (is_null($proposedValue)) return null;
  502. if ($proposedValue === '') return null; // 0 is transformed into '' !
  503. return (int)$proposedValue;
  504. }
  505. public function ScalarToSQL($value)
  506. {
  507. assert(is_numeric($value) || is_null($value));
  508. return $value; // supposed to be an int
  509. }
  510. }
  511. /**
  512. * Map a decimal value column (suitable for financial computations) to an attribute
  513. * internally in PHP such numbers are represented as string. Should you want to perform
  514. * a calculation on them, it is recommended to use the BC Math functions in order to
  515. * retain the precision
  516. *
  517. * @package iTopORM
  518. */
  519. class AttributeDecimal extends AttributeDBField
  520. {
  521. static protected function ListExpectedParams()
  522. {
  523. return array_merge(parent::ListExpectedParams(), array('digits', 'decimals' /* including precision */));
  524. }
  525. public function GetEditClass() {return "String";}
  526. protected function GetSQLCol() {return "DECIMAL(".$this->Get('digits').",".$this->Get('decimals').")";}
  527. public function GetValidationPattern()
  528. {
  529. $iNbDigits = $this->Get('digits');
  530. $iPrecision = $this->Get('decimals');
  531. $iNbIntegerDigits = $iNbDigits - $iPrecision - 1; // -1 because the first digit is treated separately in the pattern below
  532. return "^[-+]?[0-9]\d{0,$iNbIntegerDigits}(\.\d{0,$iPrecision})?$";
  533. }
  534. public function GetBasicFilterOperators()
  535. {
  536. return array(
  537. "!="=>"differs from",
  538. "="=>"equals",
  539. ">"=>"greater (strict) than",
  540. ">="=>"greater than",
  541. "<"=>"less (strict) than",
  542. "<="=>"less than",
  543. "in"=>"in"
  544. );
  545. }
  546. public function GetBasicFilterLooseOperator()
  547. {
  548. // Unless we implement an "equals approximately..." or "same order of magnitude"
  549. return "=";
  550. }
  551. public function GetBasicFilterSQLExpr($sOpCode, $value)
  552. {
  553. $sQValue = CMDBSource::Quote($value);
  554. switch ($sOpCode)
  555. {
  556. case '!=':
  557. return $this->GetSQLExpr()." != $sQValue";
  558. break;
  559. case '>':
  560. return $this->GetSQLExpr()." > $sQValue";
  561. break;
  562. case '>=':
  563. return $this->GetSQLExpr()." >= $sQValue";
  564. break;
  565. case '<':
  566. return $this->GetSQLExpr()." < $sQValue";
  567. break;
  568. case '<=':
  569. return $this->GetSQLExpr()." <= $sQValue";
  570. break;
  571. case 'in':
  572. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  573. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  574. break;
  575. case '=':
  576. default:
  577. return $this->GetSQLExpr()." = \"$value\"";
  578. }
  579. }
  580. public function GetNullValue()
  581. {
  582. return null;
  583. }
  584. public function IsNull($proposedValue)
  585. {
  586. return is_null($proposedValue);
  587. }
  588. public function MakeRealValue($proposedValue)
  589. {
  590. if (is_null($proposedValue)) return null;
  591. if ($proposedValue == '') return null;
  592. return (string)$proposedValue;
  593. }
  594. public function ScalarToSQL($value)
  595. {
  596. assert(is_null($value) || preg_match('/'.$this->GetValidationPattern().'/', $value));
  597. return (string)$value; // treated as a string
  598. }
  599. }
  600. /**
  601. * Map a boolean column to an attribute
  602. *
  603. * @package iTopORM
  604. */
  605. class AttributeBoolean extends AttributeInteger
  606. {
  607. static protected function ListExpectedParams()
  608. {
  609. return parent::ListExpectedParams();
  610. //return array_merge(parent::ListExpectedParams(), array());
  611. }
  612. public function GetEditClass() {return "Integer";}
  613. protected function GetSQLCol() {return "TINYINT(1)";}
  614. public function MakeRealValue($proposedValue)
  615. {
  616. if (is_null($proposedValue)) return null;
  617. if ($proposedValue === '') return null;
  618. if ((int)$proposedValue) return true;
  619. return false;
  620. }
  621. public function ScalarToSQL($value)
  622. {
  623. if ($value) return 1;
  624. return 0;
  625. }
  626. }
  627. /**
  628. * Map a varchar column (size < ?) to an attribute
  629. *
  630. * @package iTopORM
  631. */
  632. class AttributeString extends AttributeDBField
  633. {
  634. static protected function ListExpectedParams()
  635. {
  636. return parent::ListExpectedParams();
  637. //return array_merge(parent::ListExpectedParams(), array());
  638. }
  639. public function GetEditClass() {return "String";}
  640. protected function GetSQLCol() {return "VARCHAR(255)";}
  641. public function CheckFormat($value)
  642. {
  643. $sRegExp = $this->GetValidationPattern();
  644. if (empty($sRegExp))
  645. {
  646. return true;
  647. }
  648. else
  649. {
  650. $sRegExp = str_replace('/', '\\/', $sRegExp);
  651. return preg_match("/$sRegExp/", $value);
  652. }
  653. }
  654. public function GetMaxSize()
  655. {
  656. return 255;
  657. }
  658. public function GetBasicFilterOperators()
  659. {
  660. return array(
  661. "="=>"equals",
  662. "!="=>"differs from",
  663. "Like"=>"equals (no case)",
  664. "NotLike"=>"differs from (no case)",
  665. "Contains"=>"contains",
  666. "Begins with"=>"begins with",
  667. "Finishes with"=>"finishes with"
  668. );
  669. }
  670. public function GetBasicFilterLooseOperator()
  671. {
  672. return "Contains";
  673. }
  674. public function GetBasicFilterSQLExpr($sOpCode, $value)
  675. {
  676. $sQValue = CMDBSource::Quote($value);
  677. switch ($sOpCode)
  678. {
  679. case '=':
  680. case '!=':
  681. return $this->GetSQLExpr()." $sOpCode $sQValue";
  682. case 'Begins with':
  683. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("$value%");
  684. case 'Finishes with':
  685. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value");
  686. case 'Contains':
  687. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
  688. case 'NotLike':
  689. return $this->GetSQLExpr()." NOT LIKE $sQValue";
  690. case 'Like':
  691. default:
  692. return $this->GetSQLExpr()." LIKE $sQValue";
  693. }
  694. }
  695. public function GetNullValue()
  696. {
  697. return '';
  698. }
  699. public function IsNull($proposedValue)
  700. {
  701. return ($proposedValue == '');
  702. }
  703. public function MakeRealValue($proposedValue)
  704. {
  705. if (is_null($proposedValue)) return '';
  706. return (string)$proposedValue;
  707. }
  708. public function ScalarToSQL($value)
  709. {
  710. if (!is_string($value) && !is_null($value))
  711. {
  712. throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetCode(), 'attribute' => $this->GetHostClass()));
  713. }
  714. return $value;
  715. }
  716. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  717. {
  718. $sFrom = array("\r\n", $sTextQualifier);
  719. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  720. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  721. return '"'.$sEscaped.'"';
  722. }
  723. }
  724. /**
  725. * An attibute that matches an object class
  726. *
  727. * @package iTopORM
  728. */
  729. class AttributeClass extends AttributeString
  730. {
  731. static protected function ListExpectedParams()
  732. {
  733. return array_merge(parent::ListExpectedParams(), array("class_category", "more_values"));
  734. }
  735. public function __construct($sCode, $aParams)
  736. {
  737. $this->m_sCode = $sCode;
  738. $aParams["allowed_values"] = new ValueSetEnumClasses($aParams['class_category'], $aParams['more_values']);
  739. parent::__construct($sCode, $aParams);
  740. }
  741. public function GetDefaultValue()
  742. {
  743. $sDefault = parent::GetDefaultValue();
  744. if (!$this->IsNullAllowed() && $this->IsNull($sDefault))
  745. {
  746. // For this kind of attribute specifying null as default value
  747. // is authorized even if null is not allowed
  748. // Pick the first one...
  749. $aClasses = $this->GetAllowedValues();
  750. $sDefault = key($aClasses);
  751. }
  752. return $sDefault;
  753. }
  754. public function GetAsHTML($sValue)
  755. {
  756. if (empty($sValue)) return '';
  757. return MetaModel::GetName($sValue);
  758. }
  759. public function RequiresIndex()
  760. {
  761. return true;
  762. }
  763. }
  764. /**
  765. * An attibute that matches one of the language codes availables in the dictionnary
  766. *
  767. * @package iTopORM
  768. */
  769. class AttributeApplicationLanguage extends AttributeString
  770. {
  771. static protected function ListExpectedParams()
  772. {
  773. return parent::ListExpectedParams();
  774. }
  775. public function __construct($sCode, $aParams)
  776. {
  777. $this->m_sCode = $sCode;
  778. $aAvailableLanguages = Dict::GetLanguages();
  779. $aLanguageCodes = array();
  780. foreach($aAvailableLanguages as $sLangCode => $aInfo)
  781. {
  782. $aLanguageCodes[$sLangCode] = $aInfo['description'].' ('.$aInfo['localized_description'].')';
  783. }
  784. $aParams["allowed_values"] = new ValueSetEnum($aLanguageCodes);
  785. parent::__construct($sCode, $aParams);
  786. }
  787. public function RequiresIndex()
  788. {
  789. return true;
  790. }
  791. }
  792. /**
  793. * The attribute dedicated to the finalclass automatic attribute
  794. *
  795. * @package iTopORM
  796. */
  797. class AttributeFinalClass extends AttributeString
  798. {
  799. public function __construct($sCode, $aParams)
  800. {
  801. $this->m_sCode = $sCode;
  802. $aParams["allowed_values"] = null;
  803. parent::__construct($sCode, $aParams);
  804. $this->m_sValue = $this->Get("default_value");
  805. }
  806. public function IsWritable()
  807. {
  808. return false;
  809. }
  810. public function RequiresIndex()
  811. {
  812. return true;
  813. }
  814. public function SetFixedValue($sValue)
  815. {
  816. $this->m_sValue = $sValue;
  817. }
  818. public function GetDefaultValue()
  819. {
  820. return $this->m_sValue;
  821. }
  822. public function GetAsHTML($sValue)
  823. {
  824. if (empty($sValue)) return '';
  825. return MetaModel::GetName($sValue);
  826. }
  827. }
  828. /**
  829. * Map a varchar column (size < ?) to an attribute that must never be shown to the user
  830. *
  831. * @package iTopORM
  832. */
  833. class AttributePassword extends AttributeString
  834. {
  835. static protected function ListExpectedParams()
  836. {
  837. return parent::ListExpectedParams();
  838. //return array_merge(parent::ListExpectedParams(), array());
  839. }
  840. public function GetEditClass() {return "Password";}
  841. protected function GetSQLCol() {return "VARCHAR(64)";}
  842. public function GetMaxSize()
  843. {
  844. return 64;
  845. }
  846. public function GetFilterDefinitions()
  847. {
  848. // Note: due to this, you will get an error if a password is being declared as a search criteria (see ZLists)
  849. // not allowed to search on passwords!
  850. return array();
  851. }
  852. public function GetAsHTML($sValue)
  853. {
  854. if (strlen($sValue) == 0)
  855. {
  856. return '';
  857. }
  858. else
  859. {
  860. return '******';
  861. }
  862. }
  863. }
  864. /**
  865. * Map a text column (size < 255) to an attribute that is encrypted in the database
  866. * The encryption is based on a key set per iTop instance. Thus if you export your
  867. * database (in SQL) to someone else without providing the key at the same time
  868. * the encrypted fields will remain encrypted
  869. *
  870. * @package iTopORM
  871. */
  872. class AttributeEncryptedString extends AttributeString
  873. {
  874. static $sKey = null; // Encryption key used for all encrypted fields
  875. public function __construct($sCode, $aParams)
  876. {
  877. parent::__construct($sCode, $aParams);
  878. if (self::$sKey == null)
  879. {
  880. self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
  881. }
  882. }
  883. protected function GetSQLCol() {return "TINYBLOB";}
  884. public function GetMaxSize()
  885. {
  886. return 255;
  887. }
  888. public function GetFilterDefinitions()
  889. {
  890. // Note: due to this, you will get an error if a an encrypted field is declared as a search criteria (see ZLists)
  891. // not allowed to search on encrypted fields !
  892. return array();
  893. }
  894. public function MakeRealValue($proposedValue)
  895. {
  896. if (is_null($proposedValue)) return null;
  897. return (string)$proposedValue;
  898. }
  899. /**
  900. * Decrypt the value when reading from the database
  901. */
  902. public function FromSQLToValue($aCols, $sPrefix = '')
  903. {
  904. $oSimpleCrypt = new SimpleCrypt();
  905. $sValue = $oSimpleCrypt->Decrypt(self::$sKey, $aCols[$sPrefix]);
  906. return $sValue;
  907. }
  908. /**
  909. * Encrypt the value before storing it in the database
  910. */
  911. public function GetSQLValues($value)
  912. {
  913. $oSimpleCrypt = new SimpleCrypt();
  914. $encryptedValue = $oSimpleCrypt->Encrypt(self::$sKey, $value);
  915. $aValues = array();
  916. $aValues[$this->Get("sql")] = $encryptedValue;
  917. return $aValues;
  918. }
  919. }
  920. /**
  921. * Map a text column (size > ?) to an attribute
  922. *
  923. * @package iTopORM
  924. */
  925. define('WIKI_OBJECT_REGEXP', '/\[\[(.+):(.+)\]\]/U');
  926. class AttributeText extends AttributeString
  927. {
  928. public function GetEditClass() {return "Text";}
  929. protected function GetSQLCol() {return "TEXT";}
  930. public function GetMaxSize()
  931. {
  932. // Is there a way to know the current limitation for mysql?
  933. // See mysql_field_len()
  934. return 65535;
  935. }
  936. public function GetAsHTML($sValue)
  937. {
  938. $sValue = parent::GetAsHTML($sValue);
  939. if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
  940. {
  941. foreach($aAllMatches as $iPos => $aMatches)
  942. {
  943. $sClass = $aMatches[1];
  944. $sName = $aMatches[2];
  945. if (MetaModel::IsValidClass($sClass))
  946. {
  947. $oObj = MetaModel::GetObjectByName($sClass, $sName, false /* MustBeFound */);
  948. if (is_object($oObj))
  949. {
  950. // Propose a std link to the object
  951. $sValue = str_replace($aMatches[0], $oObj->GetHyperlink(), $sValue);
  952. }
  953. else
  954. {
  955. // Propose a std link to the object
  956. $sClassLabel = MetaModel::GetName($sClass);
  957. $sValue = str_replace($aMatches[0], "<span class=\"wiki_broken_link\">$sClassLabel:$sName</span>", $sValue);
  958. // Later: propose a link to create a new object
  959. // Anyhow... there is no easy way to suggest default values based on the given FRIENDLY name
  960. //$sValue = preg_replace('/\[\[(.+):(.+)\]\]/', '<a href="./UI.php?operation=new&class='.$sClass.'&default[att1]=xxx&default[att2]=yyy">'.$sName.'</a>', $sValue);
  961. }
  962. }
  963. }
  964. }
  965. return str_replace("\n", "<br>\n", $sValue);
  966. }
  967. public function GetEditValue($sValue)
  968. {
  969. if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
  970. {
  971. foreach($aAllMatches as $iPos => $aMatches)
  972. {
  973. $sClass = $aMatches[1];
  974. $sName = $aMatches[2];
  975. if (MetaModel::IsValidClass($sClass))
  976. {
  977. $sClassLabel = MetaModel::GetName($sClass);
  978. $sValue = str_replace($aMatches[0], "[[$sClassLabel:$sName]]", $sValue);
  979. }
  980. }
  981. }
  982. return $sValue;
  983. }
  984. public function MakeRealValue($proposedValue)
  985. {
  986. $sValue = $proposedValue;
  987. if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
  988. {
  989. foreach($aAllMatches as $iPos => $aMatches)
  990. {
  991. $sClassLabel = $aMatches[1];
  992. $sName = $aMatches[2];
  993. if (!MetaModel::IsValidClass($sClassLabel))
  994. {
  995. $sClass = MetaModel::GetClassFromLabel($sClassLabel);
  996. if ($sClass)
  997. {
  998. $sValue = str_replace($aMatches[0], "[[$sClass:$sName]]", $sValue);
  999. }
  1000. }
  1001. }
  1002. }
  1003. return $sValue;
  1004. }
  1005. public function GetAsXML($value)
  1006. {
  1007. return Str::pure2xml($value);
  1008. }
  1009. }
  1010. /**
  1011. * Map a log to an attribute
  1012. *
  1013. * @package iTopORM
  1014. */
  1015. class AttributeLongText extends AttributeText
  1016. {
  1017. protected function GetSQLCol() {return "LONGTEXT";}
  1018. public function GetMaxSize()
  1019. {
  1020. // Is there a way to know the current limitation for mysql?
  1021. // See mysql_field_len()
  1022. return 65535*1024; // Limited... still 64 Mb!
  1023. }
  1024. }
  1025. /**
  1026. * Map a text column (size > ?), containing HTML code, to an attribute
  1027. *
  1028. * @package iTopORM
  1029. */
  1030. class AttributeHTML extends AttributeText
  1031. {
  1032. public function GetEditClass() {return "HTML";}
  1033. public function GetAsHTML($sValue)
  1034. {
  1035. return $sValue;
  1036. }
  1037. }
  1038. /**
  1039. * Specialization of a string: email
  1040. *
  1041. * @package iTopORM
  1042. */
  1043. class AttributeEmailAddress extends AttributeString
  1044. {
  1045. public function GetValidationPattern()
  1046. {
  1047. // return "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
  1048. return "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$";
  1049. }
  1050. public function GetAsHTML($sValue)
  1051. {
  1052. if (empty($sValue)) return '';
  1053. return '<a class="mailto" href="mailto:'.$sValue.'">'.parent::GetAsHTML($sValue).'</a>';
  1054. }
  1055. }
  1056. /**
  1057. * Specialization of a string: IP address
  1058. *
  1059. * @package iTopORM
  1060. */
  1061. class AttributeIPAddress extends AttributeString
  1062. {
  1063. public function GetValidationPattern()
  1064. {
  1065. $sNum = '(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])';
  1066. return "^($sNum\\.$sNum\\.$sNum\\.$sNum)$";
  1067. }
  1068. }
  1069. /**
  1070. * Specialization of a string: OQL expression
  1071. *
  1072. * @package iTopORM
  1073. */
  1074. class AttributeOQL extends AttributeText
  1075. {
  1076. }
  1077. /**
  1078. * Specialization of a string: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
  1079. *
  1080. * @package iTopORM
  1081. */
  1082. class AttributeTemplateString extends AttributeString
  1083. {
  1084. }
  1085. /**
  1086. * Specialization of a text: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
  1087. *
  1088. * @package iTopORM
  1089. */
  1090. class AttributeTemplateText extends AttributeText
  1091. {
  1092. }
  1093. /**
  1094. * Specialization of a HTML: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
  1095. *
  1096. * @package iTopORM
  1097. */
  1098. class AttributeTemplateHTML extends AttributeText
  1099. {
  1100. public function GetEditClass() {return "HTML";}
  1101. public function GetAsHTML($sValue)
  1102. {
  1103. return $sValue;
  1104. }
  1105. }
  1106. /**
  1107. * Specialization of a text: wiki formatting
  1108. *
  1109. * @package iTopORM
  1110. */
  1111. class AttributeWikiText extends AttributeText
  1112. {
  1113. public function GetAsHTML($value)
  1114. {
  1115. // [SELECT xxxx.... [label]] => hyperlink to a result list
  1116. // {SELECT xxxx.... [label]} => result list displayed inline
  1117. // [myclass/nnn [label]] => hyperlink to an object
  1118. // {myclass/nnn/attcode} => attribute displayed inline
  1119. // etc.
  1120. return parent::GetAsHTML($value);
  1121. }
  1122. }
  1123. /**
  1124. * Map a enum column to an attribute
  1125. *
  1126. * @package iTopORM
  1127. */
  1128. class AttributeEnum extends AttributeString
  1129. {
  1130. static protected function ListExpectedParams()
  1131. {
  1132. return parent::ListExpectedParams();
  1133. //return array_merge(parent::ListExpectedParams(), array());
  1134. }
  1135. public function GetEditClass() {return "String";}
  1136. protected function GetSQLCol()
  1137. {
  1138. $oValDef = $this->GetValuesDef();
  1139. if ($oValDef)
  1140. {
  1141. $aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
  1142. }
  1143. else
  1144. {
  1145. $aValues = array();
  1146. }
  1147. if (count($aValues) > 0)
  1148. {
  1149. // The syntax used here do matters
  1150. // In particular, I had to remove unnecessary spaces to
  1151. // make sure that this string will match the field type returned by the DB
  1152. // (used to perform a comparison between the current DB format and the data model)
  1153. return "ENUM(".implode(",", $aValues).")";
  1154. }
  1155. else
  1156. {
  1157. return "VARCHAR(255)"; // ENUM() is not an allowed syntax!
  1158. }
  1159. }
  1160. public function ScalarToSQL($value)
  1161. {
  1162. // Note: for strings, the null value is an empty string and it is recorded as such in the DB
  1163. // but that wasn't working for enums, because '' is NOT one of the allowed values
  1164. // that's why a null value must be forced to a real null
  1165. $value = parent::ScalarToSQL($value);
  1166. if ($this->IsNull($value))
  1167. {
  1168. return null;
  1169. }
  1170. else
  1171. {
  1172. return $value;
  1173. }
  1174. }
  1175. public function RequiresIndex()
  1176. {
  1177. return false;
  1178. }
  1179. public function GetBasicFilterOperators()
  1180. {
  1181. return parent::GetBasicFilterOperators();
  1182. }
  1183. public function GetBasicFilterLooseOperator()
  1184. {
  1185. return parent::GetBasicFilterLooseOperator();
  1186. }
  1187. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1188. {
  1189. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  1190. }
  1191. public function GetAsHTML($sValue)
  1192. {
  1193. if (is_null($sValue))
  1194. {
  1195. // Unless a specific label is defined for the null value of this enum, use a generic "undefined" label
  1196. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, Dict::S('Enum:Undefined'));
  1197. }
  1198. else
  1199. {
  1200. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  1201. }
  1202. $sDescription = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue.'+', $sValue);
  1203. // later, we could imagine a detailed description in the title
  1204. return "<span title=\"$sDescription\">".parent::GetAsHtml($sLabel)."</span>";
  1205. }
  1206. public function GetEditValue($sValue)
  1207. {
  1208. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  1209. return $sLabel;
  1210. }
  1211. public function GetAllowedValues($aArgs = array(), $sContains = '')
  1212. {
  1213. $aRawValues = parent::GetAllowedValues($aArgs, $sContains);
  1214. if (is_null($aRawValues)) return null;
  1215. $aLocalizedValues = array();
  1216. foreach ($aRawValues as $sKey => $sValue)
  1217. {
  1218. $aLocalizedValues[$sKey] = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sKey, $sKey);
  1219. }
  1220. return $aLocalizedValues;
  1221. }
  1222. /**
  1223. * Processes the input value to align it with the values supported
  1224. * by this type of attribute. In this case: turns empty strings into nulls
  1225. * @param mixed $proposedValue The value to be set for the attribute
  1226. * @return mixed The actual value that will be set
  1227. */
  1228. public function MakeRealValue($proposedValue)
  1229. {
  1230. if ($proposedValue == '') return null;
  1231. return parent::MakeRealValue($proposedValue);
  1232. }
  1233. }
  1234. /**
  1235. * Map a date+time column to an attribute
  1236. *
  1237. * @package iTopORM
  1238. */
  1239. class AttributeDateTime extends AttributeDBField
  1240. {
  1241. //const MYDATETIMEZONE = "UTC";
  1242. const MYDATETIMEZONE = "Europe/Paris";
  1243. static protected $const_TIMEZONE = null; // set once for all upon object construct
  1244. static public function InitStatics()
  1245. {
  1246. // Init static constant once for all (remove when PHP allows real static const)
  1247. self::$const_TIMEZONE = new DateTimeZone(self::MYDATETIMEZONE);
  1248. // #@# Init default timezone -> do not get a notice... to be improved !!!
  1249. // duplicated in the email test page (the mail function does trigger a notice as well)
  1250. date_default_timezone_set(self::MYDATETIMEZONE);
  1251. }
  1252. static protected function GetDateFormat()
  1253. {
  1254. return "Y-m-d H:i:s";
  1255. }
  1256. static protected function ListExpectedParams()
  1257. {
  1258. return parent::ListExpectedParams();
  1259. //return array_merge(parent::ListExpectedParams(), array());
  1260. }
  1261. public function GetEditClass() {return "DateTime";}
  1262. protected function GetSQLCol() {return "TIMESTAMP";}
  1263. public static function GetAsUnixSeconds($value)
  1264. {
  1265. $oDeadlineDateTime = new DateTime($value, self::$const_TIMEZONE);
  1266. $iUnixSeconds = $oDeadlineDateTime->format('U');
  1267. return $iUnixSeconds;
  1268. }
  1269. // #@# THIS HAS TO REVISED
  1270. // Having null not allowed was interpreted by mySQL
  1271. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  1272. // Then, on each update of the record, the field was modified.
  1273. // We will have to specify the default value if we want to restore this option
  1274. // In fact, we could also have more verbs dedicated to the DB:
  1275. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  1276. public function IsNullAllowed() {return true;}
  1277. public function GetDefaultValue()
  1278. {
  1279. $default = parent::GetDefaultValue();
  1280. if (!parent::IsNullAllowed())
  1281. {
  1282. if (empty($default))
  1283. {
  1284. $default = date(self::GetDateFormat());
  1285. }
  1286. }
  1287. return $default;
  1288. }
  1289. // END OF THE WORKAROUND
  1290. ///////////////////////////////////////////////////////////////
  1291. public function GetValidationPattern()
  1292. {
  1293. 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$";
  1294. }
  1295. public function GetBasicFilterOperators()
  1296. {
  1297. return array(
  1298. "="=>"equals",
  1299. "!="=>"differs from",
  1300. "<"=>"before",
  1301. "<="=>"before",
  1302. ">"=>"after (strictly)",
  1303. ">="=>"after",
  1304. "SameDay"=>"same day (strip time)",
  1305. "SameMonth"=>"same year/month",
  1306. "SameYear"=>"same year",
  1307. "Today"=>"today",
  1308. ">|"=>"after today + N days",
  1309. "<|"=>"before today + N days",
  1310. "=|"=>"equals today + N days",
  1311. );
  1312. }
  1313. public function GetBasicFilterLooseOperator()
  1314. {
  1315. // Unless we implement a "same xxx, depending on given precision" !
  1316. return "=";
  1317. }
  1318. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1319. {
  1320. $sQValue = CMDBSource::Quote($value);
  1321. switch ($sOpCode)
  1322. {
  1323. case '=':
  1324. case '!=':
  1325. case '<':
  1326. case '<=':
  1327. case '>':
  1328. case '>=':
  1329. return $this->GetSQLExpr()." $sOpCode $sQValue";
  1330. case 'SameDay':
  1331. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  1332. case 'SameMonth':
  1333. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  1334. case 'SameYear':
  1335. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  1336. case 'Today':
  1337. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  1338. case '>|':
  1339. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1340. case '<|':
  1341. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1342. case '=|':
  1343. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1344. default:
  1345. return $this->GetSQLExpr()." = $sQValue";
  1346. }
  1347. }
  1348. public function MakeRealValue($proposedValue)
  1349. {
  1350. if (is_null($proposedValue))
  1351. {
  1352. return null;
  1353. }
  1354. if (is_string($proposedValue) && ($proposedValue == "") && $this->IsNullAllowed())
  1355. {
  1356. return null;
  1357. }
  1358. if (!is_numeric($proposedValue))
  1359. {
  1360. return $proposedValue;
  1361. }
  1362. return date(self::GetDateFormat(), $proposedValue);
  1363. }
  1364. public function ScalarToSQL($value)
  1365. {
  1366. if (is_null($value))
  1367. {
  1368. return null;
  1369. }
  1370. elseif (empty($value))
  1371. {
  1372. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  1373. return '0000-00-00 00:00:00';
  1374. }
  1375. return $value;
  1376. }
  1377. public function GetAsHTML($value)
  1378. {
  1379. return Str::pure2html($value);
  1380. }
  1381. public function GetAsXML($value)
  1382. {
  1383. return Str::pure2xml($value);
  1384. }
  1385. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  1386. {
  1387. $sFrom = array("\r\n", $sTextQualifier);
  1388. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  1389. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  1390. return '"'.$sEscaped.'"';
  1391. }
  1392. /**
  1393. * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
  1394. * Each derived class is reponsible for defining and processing their own smart patterns, the base class
  1395. * does nothing special, and just calls the default (loose) operator
  1396. * @param string $sSearchText The search string to analyze for smart patterns
  1397. * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
  1398. * @param Hash $aParams Values of the query parameters
  1399. * @return Expression The search condition to be added (AND) to the current search
  1400. */
  1401. public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
  1402. {
  1403. // Possible smart patterns
  1404. $aPatterns = array(
  1405. 'between' => array('pattern' => '/^\[(.*),(.*)\]$/', 'operator' => 'n/a'),
  1406. 'greater than or equal' => array('pattern' => '/^>=(.*)$/', 'operator' => '>='),
  1407. 'greater than' => array('pattern' => '/^>(.*)$/', 'operator' => '>'),
  1408. 'less than or equal' => array('pattern' => '/^<=(.*)$/', 'operator' => '<='),
  1409. 'less than' => array('pattern' => '/^<(.*)$/', 'operator' => '<'),
  1410. );
  1411. $sPatternFound = '';
  1412. $aMatches = array();
  1413. foreach($aPatterns as $sPatName => $sPattern)
  1414. {
  1415. if (preg_match($sPattern['pattern'], $sSearchText, $aMatches))
  1416. {
  1417. $sPatternFound = $sPatName;
  1418. break;
  1419. }
  1420. }
  1421. switch($sPatternFound)
  1422. {
  1423. case 'between':
  1424. $sParamName1 = $oField->GetParent().'_'.$oField->GetName().'_1';
  1425. $oRightExpr = new VariableExpression($sParamName1);
  1426. $aParams[$sParamName1] = $aMatches[1];
  1427. $oCondition1 = new BinaryExpression($oField, '>=', $oRightExpr);
  1428. $sParamName2 = $oField->GetParent().'_'.$oField->GetName().'_2';
  1429. $oRightExpr = new VariableExpression($sParamName2);
  1430. $sOperator = $this->GetBasicFilterLooseOperator();
  1431. $aParams[$sParamName2] = $aMatches[2];
  1432. $oCondition2 = new BinaryExpression($oField, '<=', $oRightExpr);
  1433. $oNewCondition = new BinaryExpression($oCondition1, 'AND', $oCondition2);
  1434. break;
  1435. case 'greater than':
  1436. case 'greater than or equal':
  1437. case 'less than':
  1438. case 'less than or equal':
  1439. $sSQLOperator = $aPatterns[$sPatternFound]['operator'];
  1440. $sParamName = $oField->GetParent().'_'.$oField->GetName();
  1441. $oRightExpr = new VariableExpression($sParamName);
  1442. $aParams[$sParamName] = $aMatches[1];
  1443. $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
  1444. break;
  1445. default:
  1446. $oNewCondition = parent::GetSmartConditionExpression($sSearchText, $oField, $aParams);
  1447. }
  1448. return $oNewCondition;
  1449. }
  1450. }
  1451. /**
  1452. * Store a duration as a number of seconds
  1453. *
  1454. * @package iTopORM
  1455. */
  1456. class AttributeDuration extends AttributeInteger
  1457. {
  1458. public function GetEditClass() {return "Duration";}
  1459. protected function GetSQLCol() {return "INT(11) UNSIGNED";}
  1460. public function GetNullValue() {return '0';}
  1461. public function GetDefaultValue()
  1462. {
  1463. return 0;
  1464. }
  1465. public function MakeRealValue($proposedValue)
  1466. {
  1467. if (is_null($proposedValue)) return null;
  1468. if (!is_numeric($proposedValue)) return null;
  1469. if ( ((int)$proposedValue) < 0) return null;
  1470. return (int)$proposedValue;
  1471. }
  1472. public function ScalarToSQL($value)
  1473. {
  1474. if (is_null($value))
  1475. {
  1476. return null;
  1477. }
  1478. return $value;
  1479. }
  1480. public function GetAsHTML($value)
  1481. {
  1482. return Str::pure2html(self::FormatDuration($value));
  1483. }
  1484. static function FormatDuration($duration)
  1485. {
  1486. $aDuration = self::SplitDuration($duration);
  1487. $sResult = '';
  1488. if ($duration < 60)
  1489. {
  1490. // Less than 1 min
  1491. $sResult = Dict::Format('Core:Duration_Seconds', $aDuration['seconds']);
  1492. }
  1493. else if ($duration < 3600)
  1494. {
  1495. // less than 1 hour, display it in minutes/seconds
  1496. $sResult = Dict::Format('Core:Duration_Minutes_Seconds', $aDuration['minutes'], $aDuration['seconds']);
  1497. }
  1498. else if ($duration < 86400)
  1499. {
  1500. // Less than 1 day, display it in hours/minutes/seconds
  1501. $sResult = Dict::Format('Core:Duration_Hours_Minutes_Seconds', $aDuration['hours'], $aDuration['minutes'], $aDuration['seconds']);
  1502. }
  1503. else
  1504. {
  1505. // more than 1 day, display it in days/hours/minutes/seconds
  1506. $sResult = Dict::Format('Core:Duration_Days_Hours_Minutes_Seconds', $aDuration['days'], $aDuration['hours'], $aDuration['minutes'], $aDuration['seconds']);
  1507. }
  1508. return $sResult;
  1509. }
  1510. static function SplitDuration($duration)
  1511. {
  1512. $days = floor($duration / 86400);
  1513. $hours = floor(($duration - (86400*$days)) / 3600);
  1514. $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
  1515. $seconds = ($duration % 60); // modulo
  1516. return array( 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds );
  1517. }
  1518. }
  1519. /**
  1520. * Map a date+time column to an attribute
  1521. *
  1522. * @package iTopORM
  1523. */
  1524. class AttributeDate extends AttributeDateTime
  1525. {
  1526. const MYDATEFORMAT = "Y-m-d";
  1527. static protected function GetDateFormat()
  1528. {
  1529. return "Y-m-d";
  1530. }
  1531. static public function InitStatics()
  1532. {
  1533. // Nothing to do...
  1534. }
  1535. static protected function ListExpectedParams()
  1536. {
  1537. return parent::ListExpectedParams();
  1538. //return array_merge(parent::ListExpectedParams(), array());
  1539. }
  1540. public function GetEditClass() {return "Date";}
  1541. protected function GetSQLCol() {return "DATE";}
  1542. public function GetValidationPattern()
  1543. {
  1544. 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)))$";
  1545. }
  1546. }
  1547. // Init static constant once for all (remove when PHP allows real static const)
  1548. AttributeDate::InitStatics();
  1549. /**
  1550. * A dead line stored as a date & time
  1551. * The only difference with the DateTime attribute is the display:
  1552. * relative to the current time
  1553. */
  1554. class AttributeDeadline extends AttributeDateTime
  1555. {
  1556. public function GetAsHTML($value)
  1557. {
  1558. $sResult = '';
  1559. if ($value !== null)
  1560. {
  1561. $value = AttributeDateTime::GetAsUnixSeconds($value);
  1562. $difference = $value - time();
  1563. if ($difference >= 0)
  1564. {
  1565. $sResult = self::FormatDuration($difference);
  1566. }
  1567. else
  1568. {
  1569. $sResult = Dict::Format('UI:DeadlineMissedBy_duration', self::FormatDuration(-$difference));
  1570. }
  1571. }
  1572. return $sResult;
  1573. }
  1574. static function FormatDuration($duration)
  1575. {
  1576. $days = floor($duration / 86400);
  1577. $hours = floor(($duration - (86400*$days)) / 3600);
  1578. $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
  1579. $sResult = '';
  1580. if ($duration < 60)
  1581. {
  1582. // Less than 1 min
  1583. $sResult =Dict::S('UI:Deadline_LessThan1Min');
  1584. }
  1585. else if ($duration < 3600)
  1586. {
  1587. // less than 1 hour, display it in minutes
  1588. $sResult =Dict::Format('UI:Deadline_Minutes', $minutes);
  1589. }
  1590. else if ($duration < 86400)
  1591. {
  1592. // Less that 1 day, display it in hours/minutes
  1593. $sResult =Dict::Format('UI:Deadline_Hours_Minutes', $hours, $minutes);
  1594. }
  1595. else
  1596. {
  1597. // Less that 1 day, display it in hours/minutes
  1598. $sResult =Dict::Format('UI:Deadline_Days_Hours_Minutes', $days, $hours, $minutes);
  1599. }
  1600. return $sResult;
  1601. }
  1602. }
  1603. // Init static constant once for all (remove when PHP allows real static const)
  1604. AttributeDateTime::InitStatics();
  1605. /**
  1606. * Map a foreign key to an attribute
  1607. * AttributeExternalKey and AttributeExternalField may be an external key
  1608. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  1609. * where an AttributeExternalField corresponds to a column into another table (class)
  1610. *
  1611. * @package iTopORM
  1612. */
  1613. class AttributeExternalKey extends AttributeDBFieldVoid
  1614. {
  1615. static protected function ListExpectedParams()
  1616. {
  1617. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed", "on_target_delete"));
  1618. }
  1619. public function GetEditClass() {return "ExtKey";}
  1620. protected function GetSQLCol() {return "INT(11)";}
  1621. public function RequiresIndex()
  1622. {
  1623. return true;
  1624. }
  1625. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  1626. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  1627. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  1628. public function GetKeyAttCode() {return $this->GetCode();}
  1629. public function GetDefaultValue() {return 0;}
  1630. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  1631. public function GetBasicFilterOperators()
  1632. {
  1633. return parent::GetBasicFilterOperators();
  1634. }
  1635. public function GetBasicFilterLooseOperator()
  1636. {
  1637. return parent::GetBasicFilterLooseOperator();
  1638. }
  1639. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1640. {
  1641. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  1642. }
  1643. // overloaded here so that an ext key always have the answer to
  1644. // "what are your possible values?"
  1645. public function GetValuesDef()
  1646. {
  1647. $oValSetDef = $this->Get("allowed_values");
  1648. if (!$oValSetDef)
  1649. {
  1650. // Let's propose every existing value
  1651. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1652. }
  1653. return $oValSetDef;
  1654. }
  1655. public function GetAllowedValues($aArgs = array(), $sContains = '')
  1656. {
  1657. try
  1658. {
  1659. return parent::GetAllowedValues($aArgs, $sContains);
  1660. }
  1661. catch (MissingQueryArgument $e)
  1662. {
  1663. // Some required arguments could not be found, enlarge to any existing value
  1664. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1665. return $oValSetDef->GetValues($aArgs, $sContains);
  1666. }
  1667. }
  1668. public function GetDeletionPropagationOption()
  1669. {
  1670. return $this->Get("on_target_delete");
  1671. }
  1672. public function GetNullValue()
  1673. {
  1674. return 0;
  1675. }
  1676. public function IsNull($proposedValue)
  1677. {
  1678. return ($proposedValue == 0);
  1679. }
  1680. public function MakeRealValue($proposedValue)
  1681. {
  1682. if (is_null($proposedValue)) return 0;
  1683. if ($proposedValue === '') return 0;
  1684. if (MetaModel::IsValidObject($proposedValue)) return $proposedValue->GetKey();
  1685. return (int)$proposedValue;
  1686. }
  1687. public function GetMaximumComboLength()
  1688. {
  1689. return $this->GetOptional('max_combo_length', MetaModel::GetConfig()->Get('max_combo_length'));
  1690. }
  1691. public function GetMinAutoCompleteChars()
  1692. {
  1693. return $this->GetOptional('min_autocomplete_chars', MetaModel::GetConfig()->Get('min_autocomplete_chars'));
  1694. }
  1695. public function AllowTargetCreation()
  1696. {
  1697. return $this->GetOptional('allow_target_creation', MetaModel::GetConfig()->Get('allow_target_creation'));
  1698. }
  1699. }
  1700. /**
  1701. * An attribute which corresponds to an external key (direct or indirect)
  1702. *
  1703. * @package iTopORM
  1704. */
  1705. class AttributeExternalField extends AttributeDefinition
  1706. {
  1707. static protected function ListExpectedParams()
  1708. {
  1709. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  1710. }
  1711. public function GetEditClass() {return "ExtField";}
  1712. protected function GetSQLCol()
  1713. {
  1714. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  1715. $oExtAttDef = $this->GetExtAttDef();
  1716. return $oExtAttDef->GetSQLCol();
  1717. }
  1718. public function GetSQLExpressions()
  1719. {
  1720. return array('' => $this->GetCode());
  1721. }
  1722. public function GetLabel()
  1723. {
  1724. $oRemoteAtt = $this->GetExtAttDef();
  1725. $sDefault = $oRemoteAtt->GetLabel();
  1726. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $sDefault);
  1727. }
  1728. public function GetDescription()
  1729. {
  1730. $oRemoteAtt = $this->GetExtAttDef();
  1731. $sDefault = $oRemoteAtt->GetDescription();
  1732. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', $sDefault);
  1733. }
  1734. public function GetHelpOnEdition()
  1735. {
  1736. $oRemoteAtt = $this->GetExtAttDef();
  1737. $sDefault = $oRemoteAtt->GetHelpOnEdition();
  1738. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'?', $sDefault);
  1739. }
  1740. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  1741. {
  1742. switch($iType)
  1743. {
  1744. case EXTKEY_ABSOLUTE:
  1745. // see further
  1746. $oRemoteAtt = $this->GetExtAttDef();
  1747. return $oRemoteAtt->IsExternalKey($iType);
  1748. case EXTKEY_RELATIVE:
  1749. return false;
  1750. default:
  1751. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1752. }
  1753. }
  1754. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  1755. {
  1756. return $this->GetKeyAttDef($iType)->GetTargetClass();
  1757. }
  1758. public function IsExternalField() {return true;}
  1759. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  1760. public function GetExtAttCode() {return $this->Get("target_attcode");}
  1761. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  1762. {
  1763. switch($iType)
  1764. {
  1765. case EXTKEY_ABSOLUTE:
  1766. // see further
  1767. $oRemoteAtt = $this->GetExtAttDef();
  1768. if ($oRemoteAtt->IsExternalField())
  1769. {
  1770. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  1771. }
  1772. else if ($oRemoteAtt->IsExternalKey())
  1773. {
  1774. return $oRemoteAtt;
  1775. }
  1776. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  1777. case EXTKEY_RELATIVE:
  1778. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  1779. default:
  1780. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1781. }
  1782. }
  1783. public function GetExtAttDef()
  1784. {
  1785. $oKeyAttDef = $this->GetKeyAttDef();
  1786. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  1787. 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"));
  1788. return $oExtAttDef;
  1789. }
  1790. public function GetSQLExpr()
  1791. {
  1792. $oExtAttDef = $this->GetExtAttDef();
  1793. return $oExtAttDef->GetSQLExpr();
  1794. }
  1795. public function GetDefaultValue()
  1796. {
  1797. $oExtAttDef = $this->GetExtAttDef();
  1798. return $oExtAttDef->GetDefaultValue();
  1799. }
  1800. public function IsNullAllowed()
  1801. {
  1802. $oExtAttDef = $this->GetExtAttDef();
  1803. return $oExtAttDef->IsNullAllowed();
  1804. }
  1805. public function IsScalar()
  1806. {
  1807. $oExtAttDef = $this->GetExtAttDef();
  1808. return $oExtAttDef->IsScalar();
  1809. }
  1810. public function GetFilterDefinitions()
  1811. {
  1812. return array($this->GetCode() => new FilterFromAttribute($this));
  1813. }
  1814. public function GetBasicFilterOperators()
  1815. {
  1816. $oExtAttDef = $this->GetExtAttDef();
  1817. return $oExtAttDef->GetBasicFilterOperators();
  1818. }
  1819. public function GetBasicFilterLooseOperator()
  1820. {
  1821. $oExtAttDef = $this->GetExtAttDef();
  1822. return $oExtAttDef->GetBasicFilterLooseOperator();
  1823. }
  1824. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1825. {
  1826. $oExtAttDef = $this->GetExtAttDef();
  1827. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  1828. }
  1829. public function GetNullValue()
  1830. {
  1831. $oExtAttDef = $this->GetExtAttDef();
  1832. return $oExtAttDef->GetNullValue();
  1833. }
  1834. public function IsNull($proposedValue)
  1835. {
  1836. $oExtAttDef = $this->GetExtAttDef();
  1837. return $oExtAttDef->IsNull($proposedValue);
  1838. }
  1839. public function MakeRealValue($proposedValue)
  1840. {
  1841. $oExtAttDef = $this->GetExtAttDef();
  1842. return $oExtAttDef->MakeRealValue($proposedValue);
  1843. }
  1844. public function ScalarToSQL($value)
  1845. {
  1846. // This one could be used in case of filtering only
  1847. $oExtAttDef = $this->GetExtAttDef();
  1848. return $oExtAttDef->ScalarToSQL($value);
  1849. }
  1850. // Do not overload GetSQLExpression here because this is handled in the joins
  1851. //public function GetSQLExpressions() {return array();}
  1852. // Here, we get the data...
  1853. public function FromSQLToValue($aCols, $sPrefix = '')
  1854. {
  1855. $oExtAttDef = $this->GetExtAttDef();
  1856. return $oExtAttDef->FromSQLToValue($aCols, $sPrefix);
  1857. }
  1858. public function GetAsHTML($value)
  1859. {
  1860. $oExtAttDef = $this->GetExtAttDef();
  1861. return $oExtAttDef->GetAsHTML($value);
  1862. }
  1863. public function GetAsXML($value)
  1864. {
  1865. $oExtAttDef = $this->GetExtAttDef();
  1866. return $oExtAttDef->GetAsXML($value);
  1867. }
  1868. public function GetAsCSV($value, $sSeparator = ',', $sTestQualifier = '"')
  1869. {
  1870. $oExtAttDef = $this->GetExtAttDef();
  1871. return $oExtAttDef->GetAsCSV($value, $sSeparator, $sTestQualifier);
  1872. }
  1873. }
  1874. /**
  1875. * Map a varchar column to an URL (formats the ouput in HMTL)
  1876. *
  1877. * @package iTopORM
  1878. */
  1879. class AttributeURL extends AttributeString
  1880. {
  1881. static protected function ListExpectedParams()
  1882. {
  1883. //return parent::ListExpectedParams();
  1884. return array_merge(parent::ListExpectedParams(), array("target"));
  1885. }
  1886. public function GetEditClass() {return "String";}
  1887. public function GetAsHTML($sValue)
  1888. {
  1889. $sTarget = $this->Get("target");
  1890. if (empty($sTarget)) $sTarget = "_blank";
  1891. $sLabel = Str::pure2html($sValue);
  1892. if (strlen($sLabel) > 40)
  1893. {
  1894. // Truncate the length to about 40 characters, by removing the middle
  1895. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  1896. }
  1897. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  1898. }
  1899. public function GetValidationPattern()
  1900. {
  1901. return "^(http|https|ftp)\://[a-zA-Z0-9\-\.]+(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$";
  1902. }
  1903. }
  1904. /**
  1905. * A blob is an ormDocument, it is stored as several columns in the database
  1906. *
  1907. * @package iTopORM
  1908. */
  1909. class AttributeBlob extends AttributeDefinition
  1910. {
  1911. static protected function ListExpectedParams()
  1912. {
  1913. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  1914. }
  1915. public function GetEditClass() {return "Document";}
  1916. public function IsDirectField() {return true;}
  1917. public function IsScalar() {return true;}
  1918. public function IsWritable() {return true;}
  1919. public function GetDefaultValue() {return "";}
  1920. public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
  1921. // Facilitate things: allow the user to Set the value from a string
  1922. public function MakeRealValue($proposedValue)
  1923. {
  1924. if (!is_object($proposedValue))
  1925. {
  1926. return new ormDocument($proposedValue, 'text/plain');
  1927. }
  1928. return $proposedValue;
  1929. }
  1930. public function GetSQLExpressions()
  1931. {
  1932. $aColumns = array();
  1933. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  1934. $aColumns[''] = $this->GetCode().'_mimetype';
  1935. $aColumns['_data'] = $this->GetCode().'_data';
  1936. $aColumns['_filename'] = $this->GetCode().'_filename';
  1937. return $aColumns;
  1938. }
  1939. public function FromSQLToValue($aCols, $sPrefix = '')
  1940. {
  1941. if (!isset($aCols[$sPrefix]))
  1942. {
  1943. $sAvailable = implode(', ', array_keys($aCols));
  1944. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  1945. }
  1946. $sMimeType = $aCols[$sPrefix];
  1947. if (!isset($aCols[$sPrefix.'_data']))
  1948. {
  1949. $sAvailable = implode(', ', array_keys($aCols));
  1950. throw new MissingColumnException("Missing column '".$sPrefix."_data' from {$sAvailable}");
  1951. }
  1952. $data = $aCols[$sPrefix.'_data'];
  1953. if (!isset($aCols[$sPrefix.'_filename']))
  1954. {
  1955. $sAvailable = implode(', ', array_keys($aCols));
  1956. throw new MissingColumnException("Missing column '".$sPrefix."_filename' from {$sAvailable}");
  1957. }
  1958. $sFileName = $aCols[$sPrefix.'_filename'];
  1959. $value = new ormDocument($data, $sMimeType, $sFileName);
  1960. return $value;
  1961. }
  1962. public function GetSQLValues($value)
  1963. {
  1964. // #@# Optimization: do not load blobs anytime
  1965. // As per mySQL doc, selecting blob columns will prevent mySQL from
  1966. // using memory in case a temporary table has to be created
  1967. // (temporary tables created on disk)
  1968. // We will have to remove the blobs from the list of attributes when doing the select
  1969. // then the use of Get() should finalize the load
  1970. if ($value instanceOf ormDocument)
  1971. {
  1972. $aValues = array();
  1973. $aValues[$this->GetCode().'_data'] = $value->GetData();
  1974. $aValues[$this->GetCode().'_mimetype'] = $value->GetMimeType();
  1975. $aValues[$this->GetCode().'_filename'] = $value->GetFileName();
  1976. }
  1977. else
  1978. {
  1979. $aValues = array();
  1980. $aValues[$this->GetCode().'_data'] = '';
  1981. $aValues[$this->GetCode().'_mimetype'] = '';
  1982. $aValues[$this->GetCode().'_filename'] = '';
  1983. }
  1984. return $aValues;
  1985. }
  1986. public function GetSQLColumns()
  1987. {
  1988. $aColumns = array();
  1989. $aColumns[$this->GetCode().'_data'] = 'LONGBLOB'; // 2^32 (4 Gb)
  1990. $aColumns[$this->GetCode().'_mimetype'] = 'VARCHAR(255)';
  1991. $aColumns[$this->GetCode().'_filename'] = 'VARCHAR(255)';
  1992. return $aColumns;
  1993. }
  1994. public function GetFilterDefinitions()
  1995. {
  1996. return array();
  1997. // still not working... see later...
  1998. return array(
  1999. $this->GetCode().'->filename' => new FilterFromAttribute($this, '_filename'),
  2000. $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype'),
  2001. $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype')
  2002. );
  2003. }
  2004. public function GetBasicFilterOperators()
  2005. {
  2006. return array();
  2007. }
  2008. public function GetBasicFilterLooseOperator()
  2009. {
  2010. return '=';
  2011. }
  2012. public function GetBasicFilterSQLExpr($sOpCode, $value)
  2013. {
  2014. return 'true';
  2015. }
  2016. public function GetAsHTML($value)
  2017. {
  2018. if (is_object($value))
  2019. {
  2020. return $value->GetAsHTML();
  2021. }
  2022. }
  2023. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  2024. {
  2025. return ''; // Not exportable in CSV !
  2026. }
  2027. public function GetAsXML($value)
  2028. {
  2029. return ''; // Not exportable in XML, or as CDATA + some subtags ??
  2030. }
  2031. }
  2032. /**
  2033. * One way encrypted (hashed) password
  2034. */
  2035. class AttributeOneWayPassword extends AttributeDefinition
  2036. {
  2037. static protected function ListExpectedParams()
  2038. {
  2039. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  2040. }
  2041. public function GetEditClass() {return "One Way Password";}
  2042. public function IsDirectField() {return true;}
  2043. public function IsScalar() {return true;}
  2044. public function IsWritable() {return true;}
  2045. public function GetDefaultValue() {return "";}
  2046. public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
  2047. // Facilitate things: allow the user to Set the value from a string or from an ormPassword (already encrypted)
  2048. public function MakeRealValue($proposedValue)
  2049. {
  2050. $oPassword = $proposedValue;
  2051. if (!is_object($oPassword))
  2052. {
  2053. $oPassword = new ormPassword('', '');
  2054. $oPassword->SetPassword($proposedValue);
  2055. }
  2056. return $oPassword;
  2057. }
  2058. public function GetSQLExpressions()
  2059. {
  2060. $aColumns = array();
  2061. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  2062. $aColumns[''] = $this->GetCode().'_hash';
  2063. $aColumns['_salt'] = $this->GetCode().'_salt';
  2064. return $aColumns;
  2065. }
  2066. public function FromSQLToValue($aCols, $sPrefix = '')
  2067. {
  2068. if (!isset($aCols[$sPrefix]))
  2069. {
  2070. $sAvailable = implode(', ', array_keys($aCols));
  2071. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  2072. }
  2073. $hashed = $aCols[$sPrefix];
  2074. if (!isset($aCols[$sPrefix.'_salt']))
  2075. {
  2076. $sAvailable = implode(', ', array_keys($aCols));
  2077. throw new MissingColumnException("Missing column '".$sPrefix."_salt' from {$sAvailable}");
  2078. }
  2079. $sSalt = $aCols[$sPrefix.'_salt'];
  2080. $value = new ormPassword($hashed, $sSalt);
  2081. return $value;
  2082. }
  2083. public function GetSQLValues($value)
  2084. {
  2085. // #@# Optimization: do not load blobs anytime
  2086. // As per mySQL doc, selecting blob columns will prevent mySQL from
  2087. // using memory in case a temporary table has to be created
  2088. // (temporary tables created on disk)
  2089. // We will have to remove the blobs from the list of attributes when doing the select
  2090. // then the use of Get() should finalize the load
  2091. if ($value instanceOf ormPassword)
  2092. {
  2093. $aValues = array();
  2094. $aValues[$this->GetCode().'_hash'] = $value->GetHash();
  2095. $aValues[$this->GetCode().'_salt'] = $value->GetSalt();
  2096. }
  2097. else
  2098. {
  2099. $aValues = array();
  2100. $aValues[$this->GetCode().'_hash'] = '';
  2101. $aValues[$this->GetCode().'_salt'] = '';
  2102. }
  2103. return $aValues;
  2104. }
  2105. public function GetSQLColumns()
  2106. {
  2107. $aColumns = array();
  2108. $aColumns[$this->GetCode().'_hash'] = 'TINYBLOB';
  2109. $aColumns[$this->GetCode().'_salt'] = 'TINYBLOB';
  2110. return $aColumns;
  2111. }
  2112. public function GetFilterDefinitions()
  2113. {
  2114. return array();
  2115. // still not working... see later...
  2116. }
  2117. public function GetBasicFilterOperators()
  2118. {
  2119. return array();
  2120. }
  2121. public function GetBasicFilterLooseOperator()
  2122. {
  2123. return '=';
  2124. }
  2125. public function GetBasicFilterSQLExpr($sOpCode, $value)
  2126. {
  2127. return 'true';
  2128. }
  2129. public function GetAsHTML($value)
  2130. {
  2131. if (is_object($value))
  2132. {
  2133. return $value->GetAsHTML();
  2134. }
  2135. }
  2136. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  2137. {
  2138. return ''; // Not exportable in CSV
  2139. }
  2140. public function GetAsXML($value)
  2141. {
  2142. return ''; // Not exportable in XML
  2143. }
  2144. }
  2145. // Indexed array having two dimensions
  2146. class AttributeTable extends AttributeText
  2147. {
  2148. public function GetEditClass() {return "Text";}
  2149. protected function GetSQLCol() {return "TEXT";}
  2150. public function GetMaxSize()
  2151. {
  2152. return null;
  2153. }
  2154. // Facilitate things: allow the user to Set the value from a string
  2155. public function MakeRealValue($proposedValue)
  2156. {
  2157. if (!is_array($proposedValue))
  2158. {
  2159. return array(0 => array(0 => $proposedValue));
  2160. }
  2161. return $proposedValue;
  2162. }
  2163. public function FromSQLToValue($aCols, $sPrefix = '')
  2164. {
  2165. try
  2166. {
  2167. $value = @unserialize($aCols[$sPrefix.'']);
  2168. if ($value === false)
  2169. {
  2170. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  2171. }
  2172. }
  2173. catch(Exception $e)
  2174. {
  2175. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  2176. }
  2177. return $value;
  2178. }
  2179. public function GetSQLValues($value)
  2180. {
  2181. $aValues = array();
  2182. $aValues[$this->Get("sql")] = serialize($value);
  2183. return $aValues;
  2184. }
  2185. public function GetAsHTML($value)
  2186. {
  2187. if (!is_array($value))
  2188. {
  2189. throw new CoreException('Expecting an array', array('found' => get_class($value)));
  2190. }
  2191. if (count($value) == 0)
  2192. {
  2193. return "";
  2194. }
  2195. $sRes = "<TABLE class=\"listResults\">";
  2196. $sRes .= "<TBODY>";
  2197. foreach($value as $iRow => $aRawData)
  2198. {
  2199. $sRes .= "<TR>";
  2200. foreach ($aRawData as $iCol => $cell)
  2201. {
  2202. $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$cell));
  2203. $sRes .= "<TD>$sCell</TD>";
  2204. }
  2205. $sRes .= "</TR>";
  2206. }
  2207. $sRes .= "</TBODY>";
  2208. $sRes .= "</TABLE>";
  2209. return $sRes;
  2210. }
  2211. }
  2212. // The PHP value is a hash array, it is stored as a TEXT column
  2213. class AttributePropertySet extends AttributeTable
  2214. {
  2215. public function GetEditClass() {return "Text";}
  2216. protected function GetSQLCol() {return "TEXT";}
  2217. // Facilitate things: allow the user to Set the value from a string
  2218. public function MakeRealValue($proposedValue)
  2219. {
  2220. if (!is_array($proposedValue))
  2221. {
  2222. return array('?' => (string)$proposedValue);
  2223. }
  2224. return $proposedValue;
  2225. }
  2226. public function GetAsHTML($value)
  2227. {
  2228. if (!is_array($value))
  2229. {
  2230. throw new CoreException('Expecting an array', array('found' => get_class($value)));
  2231. }
  2232. if (count($value) == 0)
  2233. {
  2234. return "";
  2235. }
  2236. $sRes = "<TABLE class=\"listResults\">";
  2237. $sRes .= "<TBODY>";
  2238. foreach($value as $sProperty => $sValue)
  2239. {
  2240. $sRes .= "<TR>";
  2241. $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$sValue));
  2242. $sRes .= "<TD class=\"label\">$sProperty</TD><TD>$sCell</TD>";
  2243. $sRes .= "</TR>";
  2244. }
  2245. $sRes .= "</TBODY>";
  2246. $sRes .= "</TABLE>";
  2247. return $sRes;
  2248. }
  2249. }
  2250. /**
  2251. * The attribute dedicated to the friendly name automatic attribute (not written)
  2252. *
  2253. * @package iTopORM
  2254. */
  2255. class AttributeComputedFieldVoid extends AttributeDefinition
  2256. {
  2257. static protected function ListExpectedParams()
  2258. {
  2259. return array_merge(parent::ListExpectedParams(), array());
  2260. }
  2261. public function GetEditClass() {return "";}
  2262. public function GetValuesDef() {return null;}
  2263. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  2264. public function IsDirectField() {return true;}
  2265. public function IsScalar() {return true;}
  2266. public function IsWritable() {return false;}
  2267. public function GetSQLExpr() {return null;}
  2268. public function GetDefaultValue() {return $this->MakeRealValue("");}
  2269. public function IsNullAllowed() {return false;}
  2270. //
  2271. // protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  2272. public function GetSQLExpressions()
  2273. {
  2274. return array('' => $this->GetCode());
  2275. }
  2276. public function FromSQLToValue($aCols, $sPrefix = '')
  2277. {
  2278. return null;
  2279. }
  2280. public function GetSQLValues($value)
  2281. {
  2282. return array();
  2283. }
  2284. public function GetSQLColumns()
  2285. {
  2286. return array();
  2287. }
  2288. public function GetFilterDefinitions()
  2289. {
  2290. return array($this->GetCode() => new FilterFromAttribute($this));
  2291. }
  2292. public function GetBasicFilterOperators()
  2293. {
  2294. return array();
  2295. }
  2296. public function GetBasicFilterLooseOperator()
  2297. {
  2298. return "=";
  2299. }
  2300. public function GetBasicFilterSQLExpr($sOpCode, $value)
  2301. {
  2302. $sQValue = CMDBSource::Quote($value);
  2303. switch ($sOpCode)
  2304. {
  2305. case '!=':
  2306. return $this->GetSQLExpr()." != $sQValue";
  2307. break;
  2308. case '=':
  2309. default:
  2310. return $this->GetSQLExpr()." = $sQValue";
  2311. }
  2312. }
  2313. }
  2314. /**
  2315. * The attribute dedicated to the friendly name automatic attribute (not written)
  2316. *
  2317. * @package iTopORM
  2318. */
  2319. class AttributeFriendlyName extends AttributeComputedFieldVoid
  2320. {
  2321. public function __construct($sCode, $sExtKeyAttCode)
  2322. {
  2323. $this->m_sCode = $sCode;
  2324. $aParams = array();
  2325. // $aParams["is_null_allowed"] = false,
  2326. $aParams["default_value"] = '';
  2327. $aParams["extkey_attcode"] = $sExtKeyAttCode;
  2328. parent::__construct($sCode, $aParams);
  2329. $this->m_sValue = $this->Get("default_value");
  2330. }
  2331. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  2332. // n/a, the friendly name is made of a complex expression (see GetNameSpec)
  2333. protected function GetSQLCol() {return "";}
  2334. public function FromSQLToValue($aCols, $sPrefix = '')
  2335. {
  2336. $sValue = $aCols[$sPrefix];
  2337. return $sValue;
  2338. }
  2339. /**
  2340. * Encrypt the value before storing it in the database
  2341. */
  2342. public function GetSQLValues($value)
  2343. {
  2344. return array();
  2345. }
  2346. public function IsWritable()
  2347. {
  2348. return false;
  2349. }
  2350. public function SetFixedValue($sValue)
  2351. {
  2352. $this->m_sValue = $sValue;
  2353. }
  2354. public function GetDefaultValue()
  2355. {
  2356. return $this->m_sValue;
  2357. }
  2358. public function GetAsHTML($sValue)
  2359. {
  2360. return Str::pure2html((string)$sValue);
  2361. }
  2362. }
  2363. ?>