attributedef.class.inc.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. <?php
  2. require_once('MyHelpers.class.inc.php');
  3. /**
  4. * add some description here...
  5. *
  6. * @package iTopORM
  7. */
  8. define('EXTKEY_RELATIVE', 1);
  9. /**
  10. * add some description here...
  11. *
  12. * @package iTopORM
  13. */
  14. define('EXTKEY_ABSOLUTE', 2);
  15. /**
  16. * Attribute definition API, implemented in and many flavours (Int, String, Enum, etc.)
  17. *
  18. * @package iTopORM
  19. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  20. * @author Denis Flaven <denisflave@free.fr>
  21. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  22. * @link www.itop.com
  23. * @since 1.0
  24. * @version 1.1.1.1 $
  25. */
  26. abstract class AttributeDefinition
  27. {
  28. abstract public function GetType();
  29. abstract public function GetTypeDesc();
  30. abstract public function GetEditClass();
  31. abstract public function GetDBFieldType();
  32. protected $m_sCode;
  33. private $m_aParams = array();
  34. private $m_sHostClass = array();
  35. protected function Get($sParamName) {return $this->m_aParams[$sParamName];}
  36. public function __construct($sCode, $aParams)
  37. {
  38. $this->m_sCode = $sCode;
  39. $this->m_aParams = $aParams;
  40. $this->ConsistencyCheck();
  41. }
  42. public function OverloadParams($aParams)
  43. {
  44. foreach ($aParams as $sParam => $value)
  45. {
  46. if (!array_key_exists($sParam, $this->m_aParams))
  47. {
  48. throw new CoreException("Unknown attribute definition parameter '$sParam', please select a value in {".implode(", ", $this->m_aParams)."}");
  49. }
  50. else
  51. {
  52. $this->m_aParams[$sParam] = $value;
  53. }
  54. }
  55. }
  56. public function SetHostClass($sHostClass)
  57. {
  58. $this->m_sHostClass = $sHostClass;
  59. }
  60. public function GetHostClass()
  61. {
  62. return $this->m_sHostClass;
  63. }
  64. // Note: I could factorize this code with the parameter management made for the AttributeDef class
  65. // to be overloaded
  66. static protected function ListExpectedParams()
  67. {
  68. return array("label", "description", "allowed_values");
  69. }
  70. private function ConsistencyCheck()
  71. {
  72. // Check that any mandatory param has been specified
  73. //
  74. $aExpectedParams = $this->ListExpectedParams();
  75. foreach($aExpectedParams as $sParamName)
  76. {
  77. if (!array_key_exists($sParamName, $this->m_aParams))
  78. {
  79. $aBacktrace = debug_backtrace();
  80. $sTargetClass = $aBacktrace[2]["class"];
  81. $sCodeInfo = $aBacktrace[1]["file"]." - ".$aBacktrace[1]["line"];
  82. throw new Exception("ERROR missing parameter '$sParamName' in ".get_class($this)." declaration for class $sTargetClass ($sCodeInfo)");
  83. }
  84. }
  85. }
  86. // table, key field, name field
  87. public function ListDBJoins()
  88. {
  89. return "";
  90. // e.g: return array("Site", "infrid", "name");
  91. }
  92. public function IsDirectField() {return false;}
  93. public function IsScalar() {return false;}
  94. public function IsLinkSet() {return false;}
  95. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return false;}
  96. public function IsExternalField() {return false;}
  97. public function IsWritable() {return false;}
  98. public function IsNullAllowed() {return true;}
  99. public function GetCode() {return $this->m_sCode;}
  100. public function GetLabel() {return $this->Get("label");}
  101. public function GetDescription() {return $this->Get("description");}
  102. public function GetValuesDef() {return $this->Get("allowed_values");}
  103. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  104. //public function IsSearchableStd() {return $this->Get("search_std");}
  105. //public function IsSearchableGlobal() {return $this->Get("search_global");}
  106. //public function IsMandatory() {return $this->Get("is_mandatory");}
  107. //public function GetMinVal() {return $this->Get("min");}
  108. //public function GetMaxVal() {return $this->Get("max");}
  109. //public function GetSize() {return $this->Get("size");}
  110. //public function GetCheckRegExp() {return $this->Get("regexp");}
  111. //public function GetCheckFunc() {return $this->Get("checkfunc");}
  112. // Definition: real value is what will be stored in memory and maintained by MetaModel
  113. // DBObject::Set() relies on MakeRealValue()
  114. // MetaModel::MakeQuery() relies on RealValueToSQLValue()
  115. // DBObject::FromRow() relies on SQLToRealValue()
  116. public function MakeRealValue($proposedValue) {return $proposedValue;} // force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing!)
  117. public function RealValueToSQLValue($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  118. public function SQLValueToRealValue($value) {return $value;} // take the result of a fetch... and make it a PHP variable
  119. public function GetJSCheckFunc()
  120. {
  121. $sRegExp = $this->Get("regexp");
  122. if (empty($sRegExp)) return 'return true;';
  123. return "return regexp('$sRegExp', myvalue);";
  124. }
  125. public function CheckValue($value)
  126. {
  127. $sRegExp = $this->Get("regexp");
  128. if (empty($sRegExp)) return true;
  129. return preg_match(preg_escape($this->Get("regexp")), $value);
  130. }
  131. public function MakeValue()
  132. {
  133. $sComputeFunc = $this->Get("compute_func");
  134. if (empty($sComputeFunc)) return null;
  135. return call_user_func($sComputeFunc);
  136. }
  137. abstract public function DBGetUsedFields();
  138. abstract public function GetDefaultValue();
  139. //
  140. // To be overloaded in subclasses
  141. //
  142. abstract public function GetBasicFilterOperators(); // returns an array of "opCode"=>"description"
  143. abstract public function GetBasicFilterLooseOperator(); // returns an "opCode"
  144. //abstract protected GetBasicFilterHTMLInput();
  145. abstract public function GetBasicFilterSQLExpr($sOpCode, $value);
  146. public function GetAsHTML($sValue)
  147. {
  148. return Str::pure2html($sValue);
  149. }
  150. public function GetAsXML($sValue)
  151. {
  152. return Str::pure2xml($sValue);
  153. }
  154. public function GetAsCSV($sValue, $sSeparator = ';', $sSepEscape = ',')
  155. {
  156. return str_replace($sSeparator, $sSepEscape, $sValue);
  157. }
  158. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  159. {
  160. $oValSetDef = $this->GetValuesDef();
  161. if (!$oValSetDef) return null;
  162. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  163. }
  164. }
  165. /**
  166. * Set of objects directly linked to an object, and being part of its definition
  167. *
  168. * @package iTopORM
  169. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  170. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  171. * @link www.itop.com
  172. * @since 1.0
  173. * @version $itopversion$
  174. */
  175. class AttributeLinkedSet extends AttributeDefinition
  176. {
  177. static protected function ListExpectedParams()
  178. {
  179. return array_merge(parent::ListExpectedParams(), array("depends_on", "linked_class", "ext_key_to_me", "count_min", "count_max"));
  180. }
  181. public function GetType() {return "Array of objects";}
  182. public function GetTypeDesc() {return "Any kind of objects [subclass] of the same class";}
  183. public function GetEditClass() {return "List";}
  184. public function GetDBFieldType() {return "N/A";} // should be moved out of the AttributeDef root class
  185. public function IsWritable() {return true;}
  186. public function IsLinkSet() {return true;}
  187. public function GetDefaultValue() {return DBObjectSet::FromScratch($this->Get('linked_class'));}
  188. public function GetLinkedClass() {return $this->Get('linked_class');}
  189. public function GetExtKeyToMe() {return $this->Get('ext_key_to_me');}
  190. public function DBGetUsedFields() {return array();}
  191. public function GetBasicFilterOperators() {return array();}
  192. public function GetBasicFilterLooseOperator() {return '';}
  193. public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
  194. public function GetAsHTML($sValue)
  195. {
  196. return "ERROR: LIST OF OBJECTS";
  197. }
  198. public function GetAsXML($sValue)
  199. {
  200. return "ERROR: LIST OF OBJECTS";
  201. }
  202. public function GetAsCSV($sValue, $sSeparator = ';', $sSepEscape = ',')
  203. {
  204. return "ERROR: LIST OF OBJECTS";
  205. }
  206. }
  207. /**
  208. * Set of objects linked to an object (n-n), and being part of its definition
  209. *
  210. * @package iTopORM
  211. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  212. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  213. * @link www.itop.com
  214. * @since 1.0
  215. * @version $itopversion$
  216. */
  217. class AttributeLinkedSetIndirect extends AttributeLinkedSet
  218. {
  219. static protected function ListExpectedParams()
  220. {
  221. return array_merge(parent::ListExpectedParams(), array("ext_key_to_remote"));
  222. }
  223. public function GetExtKeyToRemote() { return $this->Get('ext_key_to_remote'); }
  224. }
  225. /**
  226. * Abstract class implementing default filters for a DB column
  227. *
  228. * @package iTopORM
  229. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  230. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  231. * @link www.itop.com
  232. * @since 1.0
  233. * @version $itopversion$
  234. */
  235. class AttributeDBFieldVoid extends AttributeDefinition
  236. {
  237. static protected function ListExpectedParams()
  238. {
  239. return array_merge(parent::ListExpectedParams(), array("depends_on", "sql"));
  240. }
  241. public function GetType() {return "Void";}
  242. public function GetTypeDesc() {return "Any kind of value, from the DB";}
  243. public function GetEditClass() {return "String";}
  244. public function GetDBFieldType() {return "VARCHAR(255)";}
  245. public function IsDirectField() {return true;}
  246. public function IsScalar() {return true;}
  247. public function IsWritable() {return true;}
  248. public function GetSQLExpr() {return $this->Get("sql");}
  249. public function GetDefaultValue() {return "";}
  250. public function IsNullAllowed() {return false;}
  251. public function DBGetUsedFields()
  252. {
  253. // #@# bugge a mort... a suivre...
  254. return array($this->Get("sql"));
  255. }
  256. public function GetBasicFilterOperators()
  257. {
  258. return array("="=>"equals", "!="=>"differs from");
  259. }
  260. public function GetBasicFilterLooseOperator()
  261. {
  262. return "=";
  263. }
  264. public function GetBasicFilterSQLExpr($sOpCode, $value)
  265. {
  266. $sQValue = CMDBSource::Quote($value);
  267. switch ($sOpCode)
  268. {
  269. case '!=':
  270. return $this->GetSQLExpr()." != $sQValue";
  271. break;
  272. case '=':
  273. default:
  274. return $this->GetSQLExpr()." = $sQValue";
  275. }
  276. }
  277. }
  278. /**
  279. * Base class for all kind of DB attributes, with the exception of external keys
  280. *
  281. * @package iTopORM
  282. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  283. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  284. * @link www.itop.com
  285. * @since 1.0
  286. * @version $itopversion$
  287. */
  288. class AttributeDBField extends AttributeDBFieldVoid
  289. {
  290. static protected function ListExpectedParams()
  291. {
  292. return array_merge(parent::ListExpectedParams(), array("default_value", "is_null_allowed"));
  293. }
  294. public function GetDefaultValue() {return $this->Get("default_value");}
  295. public function IsNullAllowed() {return strtolower($this->Get("is_null_allowed"));}
  296. }
  297. /**
  298. * Map an integer column to an attribute
  299. *
  300. * @package iTopORM
  301. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  302. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  303. * @link www.itop.com
  304. * @since 1.0
  305. * @version $itopversion$
  306. */
  307. class AttributeInteger extends AttributeDBField
  308. {
  309. static protected function ListExpectedParams()
  310. {
  311. return parent::ListExpectedParams();
  312. //return array_merge(parent::ListExpectedParams(), array());
  313. }
  314. public function GetType() {return "Integer";}
  315. public function GetTypeDesc() {return "Numeric value (could be negative)";}
  316. public function GetEditClass() {return "String";}
  317. public function GetDBFieldType() {return "INT";}
  318. public function GetBasicFilterOperators()
  319. {
  320. return array(
  321. "!="=>"differs from",
  322. "="=>"equals",
  323. ">"=>"greater (strict) than",
  324. ">="=>"greater than",
  325. "<"=>"less (strict) than",
  326. "<="=>"less than",
  327. "in"=>"in"
  328. );
  329. }
  330. public function GetBasicFilterLooseOperator()
  331. {
  332. // Unless we implement an "equals approximately..." or "same order of magnitude"
  333. return "=";
  334. }
  335. public function GetBasicFilterSQLExpr($sOpCode, $value)
  336. {
  337. $sQValue = CMDBSource::Quote($value);
  338. switch ($sOpCode)
  339. {
  340. case '!=':
  341. return $this->GetSQLExpr()." != $sQValue";
  342. break;
  343. case '>':
  344. return $this->GetSQLExpr()." > $sQValue";
  345. break;
  346. case '>=':
  347. return $this->GetSQLExpr()." >= $sQValue";
  348. break;
  349. case '<':
  350. return $this->GetSQLExpr()." < $sQValue";
  351. break;
  352. case '<=':
  353. return $this->GetSQLExpr()." <= $sQValue";
  354. break;
  355. case 'in':
  356. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  357. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  358. break;
  359. case '=':
  360. default:
  361. return $this->GetSQLExpr()." = \"$value\"";
  362. }
  363. }
  364. public function MakeRealValue($proposedValue)
  365. {
  366. //return intval($proposedValue); could work as well
  367. return (int)$proposedValue;
  368. }
  369. public function RealValueToSQLValue($value)
  370. {
  371. assert(is_numeric($value));
  372. return $value; // supposed to be an int
  373. }
  374. public function SQLValueToRealValue($value)
  375. {
  376. // Use cast (int) or intval() ?
  377. return (int)$value;
  378. }
  379. }
  380. /**
  381. * Map a varchar column (size < ?) to an attribute
  382. *
  383. * @package iTopORM
  384. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  385. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  386. * @link www.itop.com
  387. * @since 1.0
  388. * @version $itopversion$
  389. */
  390. class AttributeString extends AttributeDBField
  391. {
  392. static protected function ListExpectedParams()
  393. {
  394. return parent::ListExpectedParams();
  395. //return array_merge(parent::ListExpectedParams(), array());
  396. }
  397. public function GetType() {return "String";}
  398. public function GetTypeDesc() {return "Alphanumeric string";}
  399. public function GetEditClass() {return "String";}
  400. public function GetDBFieldType() {return "VARCHAR(255)";}
  401. public function GetBasicFilterOperators()
  402. {
  403. return array(
  404. "="=>"equals",
  405. "!="=>"differs from",
  406. "Like"=>"equals (no case)",
  407. "NotLike"=>"differs from (no case)",
  408. "Contains"=>"contains",
  409. "Begins with"=>"begins with",
  410. "Finishes with"=>"finishes with"
  411. );
  412. }
  413. public function GetBasicFilterLooseOperator()
  414. {
  415. return "Contains";
  416. }
  417. public function GetBasicFilterSQLExpr($sOpCode, $value)
  418. {
  419. $sQValue = CMDBSource::Quote($value);
  420. switch ($sOpCode)
  421. {
  422. case '=':
  423. case '!=':
  424. return $this->GetSQLExpr()." $sOpCode $sQValue";
  425. case 'Begins with':
  426. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("$value%");
  427. case 'Finishes with':
  428. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value");
  429. case 'Contains':
  430. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
  431. case 'NotLike':
  432. return $this->GetSQLExpr()." NOT LIKE $sQValue";
  433. case 'Like':
  434. default:
  435. return $this->GetSQLExpr()." LIKE $sQValue";
  436. }
  437. }
  438. public function MakeRealValue($proposedValue)
  439. {
  440. return (string)$proposedValue;
  441. // if (!settype($proposedValue, "string"))
  442. // {
  443. // throw new CoreException("Failed to change the type of '$proposedValue' to a string");
  444. // }
  445. }
  446. public function RealValueToSQLValue($value)
  447. {
  448. if (!is_string($value))
  449. {
  450. throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetCode(), 'attribute' => $this->GetHostClass()));
  451. }
  452. return $value;
  453. }
  454. public function SQLValueToRealValue($value)
  455. {
  456. return $value;
  457. }
  458. }
  459. /**
  460. * Map a varchar column (size < ?) to an attribute that must never be shown to the user
  461. *
  462. * @package iTopORM
  463. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  464. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  465. * @link www.itop.com
  466. * @since 1.0
  467. * @version $itopversion$
  468. */
  469. class AttributePassword extends AttributeString
  470. {
  471. static protected function ListExpectedParams()
  472. {
  473. return parent::ListExpectedParams();
  474. //return array_merge(parent::ListExpectedParams(), array());
  475. }
  476. public function GetEditClass() {return "Password";}
  477. public function GetDBFieldType() {return "VARCHAR(64)";}
  478. }
  479. /**
  480. * Map a text column (size > ?) to an attribute
  481. *
  482. * @package iTopORM
  483. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  484. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  485. * @link www.itop.com
  486. * @since 1.0
  487. * @version $itopversion$
  488. */
  489. class AttributeText extends AttributeString
  490. {
  491. public function GetType() {return "Text";}
  492. public function GetTypeDesc() {return "Multiline character string";}
  493. public function GetEditClass() {return "Text";}
  494. public function GetDBFieldType() {return "TEXT";}
  495. public function GetAsHTML($sValue)
  496. {
  497. return str_replace("\n", "<br>\n", parent::GetAsHTML($sValue));
  498. }
  499. public function GetAsXML($value)
  500. {
  501. return Str::pure2xml($value);
  502. }
  503. public function GetAsCSV($value, $sSeparator = ';', $sSepEscape = ',')
  504. {
  505. return str_replace("\n", "[newline]", parent::GetAsCSV($sValue, $sSeparator, $sSepEscape));
  506. }
  507. }
  508. /**
  509. * Map a enum column to an attribute
  510. *
  511. * @package iTopORM
  512. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  513. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  514. * @link www.itop.com
  515. * @since 1.0
  516. * @version $itopversion$
  517. */
  518. class AttributeEnum extends AttributeString
  519. {
  520. static protected function ListExpectedParams()
  521. {
  522. return parent::ListExpectedParams();
  523. //return array_merge(parent::ListExpectedParams(), array());
  524. }
  525. public function GetType() {return "Enum";}
  526. public function GetTypeDesc() {return "List of predefined alphanumeric strings";}
  527. public function GetEditClass() {return "String";}
  528. public function GetDBFieldType()
  529. {
  530. $oValDef = $this->GetValuesDef();
  531. if ($oValDef)
  532. {
  533. $aValues = CMDBSource::Quote($oValDef->GetValues(array(), ""), true);
  534. }
  535. else
  536. {
  537. $aValues = array();
  538. }
  539. if (count($aValues) > 0)
  540. {
  541. return "ENUM(".implode(", ", $aValues).")";
  542. }
  543. else
  544. {
  545. return "VARCHAR(255)"; // ENUM() is not an allowed syntax!
  546. }
  547. }
  548. public function GetBasicFilterOperators()
  549. {
  550. return parent::GetBasicFilterOperators();
  551. }
  552. public function GetBasicFilterLooseOperator()
  553. {
  554. return parent::GetBasicFilterLooseOperator();
  555. }
  556. public function GetBasicFilterSQLExpr($sOpCode, $value)
  557. {
  558. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  559. }
  560. }
  561. /**
  562. * Map a date+time column to an attribute
  563. *
  564. * @package iTopORM
  565. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  566. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  567. * @link www.itop.com
  568. * @since 1.0
  569. * @version $itopversion$
  570. */
  571. class AttributeDate extends AttributeDBField
  572. {
  573. const MYDATEFORMAT = "Y-m-d H:i:s";
  574. //const MYDATETIMEZONE = "UTC";
  575. const MYDATETIMEZONE = "Europe/Paris";
  576. static protected $const_TIMEZONE = null; // set once for all upon object construct
  577. static public function InitStatics()
  578. {
  579. // Init static constant once for all (remove when PHP allows real static const)
  580. self::$const_TIMEZONE = new DateTimeZone(self::MYDATETIMEZONE);
  581. // #@# Init default timezone -> do not get a notice... to be improved !!!
  582. date_default_timezone_set(self::MYDATETIMEZONE);
  583. }
  584. static protected function ListExpectedParams()
  585. {
  586. return parent::ListExpectedParams();
  587. //return array_merge(parent::ListExpectedParams(), array());
  588. }
  589. public function GetType() {return "Date";}
  590. public function GetTypeDesc() {return "Date and time";}
  591. public function GetEditClass() {return "Date";}
  592. public function GetDBFieldType() {return "TIMESTAMP";}
  593. // #@# THIS HAS TO REVISED
  594. // Having null not allowed was interpreted by mySQL
  595. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  596. // Then, on each update of the record, the field was modified.
  597. // We will have to specify the default value if we want to restore this option
  598. // In fact, we could also have more verbs dedicated to the DB:
  599. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  600. public function IsNullAllowed() {return true;}
  601. public function GetDefaultValue()
  602. {
  603. $default = parent::GetDefaultValue();
  604. if (!parent::IsNullAllowed())
  605. {
  606. if (empty($default))
  607. {
  608. $default = date("Y-m-d H:i");
  609. }
  610. }
  611. return $default;
  612. }
  613. // END OF THE WORKAROUND
  614. ///////////////////////////////////////////////////////////////
  615. public function GetBasicFilterOperators()
  616. {
  617. return array(
  618. "="=>"equals",
  619. "!="=>"differs from",
  620. "<"=>"before",
  621. "<="=>"before",
  622. ">"=>"after (strictly)",
  623. ">="=>"after",
  624. "SameDay"=>"same day (strip time)",
  625. "SameMonth"=>"same year/month",
  626. "SameYear"=>"same year",
  627. "Today"=>"today",
  628. ">|"=>"after today + N days",
  629. "<|"=>"before today + N days",
  630. "=|"=>"equals today + N days",
  631. );
  632. }
  633. public function GetBasicFilterLooseOperator()
  634. {
  635. // Unless we implement a "same xxx, depending on given precision" !
  636. return "=";
  637. }
  638. public function GetBasicFilterSQLExpr($sOpCode, $value)
  639. {
  640. $sQValue = CMDBSource::Quote($value);
  641. switch ($sOpCode)
  642. {
  643. case '=':
  644. case '!=':
  645. case '<':
  646. case '<=':
  647. case '>':
  648. case '>=':
  649. return $this->GetSQLExpr()." $sOpCode $sQValue";
  650. case 'SameDay':
  651. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  652. case 'SameMonth':
  653. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  654. case 'SameYear':
  655. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  656. case 'Today':
  657. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  658. case '>|':
  659. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  660. case '<|':
  661. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  662. case '=|':
  663. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  664. default:
  665. return $this->GetSQLExpr()." = $sQValue";
  666. }
  667. }
  668. public function MakeRealValue($proposedValue)
  669. {
  670. if (!is_numeric($proposedValue))
  671. {
  672. return $proposedValue;
  673. }
  674. else
  675. {
  676. return date("Y-m-d H:i:s", $proposedValue);
  677. }
  678. throw new CoreException("Invalid type for a date (found ".gettype($proposedValue)." and accepting string/int/DateTime)");
  679. return null;
  680. }
  681. public function RealValueToSQLValue($value)
  682. {
  683. if (empty($value))
  684. {
  685. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  686. return '0000-00-00 00:00:00';
  687. }
  688. return $value;
  689. }
  690. public function SQLValueToRealValue($value)
  691. {
  692. return $value;
  693. }
  694. public function GetAsHTML($value)
  695. {
  696. return Str::pure2html($value);
  697. }
  698. public function GetAsXML($value)
  699. {
  700. return Str::pure2xml($value);
  701. }
  702. public function GetAsCSV($value, $sSeparator = ';', $sSepEscape = ',')
  703. {
  704. return str_replace($sSeparator, $sSepEscape, $value);
  705. }
  706. }
  707. // Init static constant once for all (remove when PHP allows real static const)
  708. AttributeDate::InitStatics();
  709. /**
  710. * Map a foreign key to an attribute
  711. * AttributeExternalKey and AttributeExternalField may be an external key
  712. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  713. * where an AttributeExternalField corresponds to a column into another table (class)
  714. *
  715. * @package iTopORM
  716. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  717. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  718. * @link www.itop.com
  719. * @since 1.0
  720. * @version $itopversion$
  721. */
  722. class AttributeExternalKey extends AttributeDBFieldVoid
  723. {
  724. static protected function ListExpectedParams()
  725. {
  726. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed"));
  727. }
  728. public function GetType() {return "Extkey";}
  729. public function GetTypeDesc() {return "Link to another object";}
  730. public function GetEditClass() {return "ExtKey";}
  731. public function GetDBFieldType() {return "INT";}
  732. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  733. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  734. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  735. public function GetKeyAttCode() {return $this->GetCode();}
  736. public function GetDefaultValue() {return 0;}
  737. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  738. public function GetBasicFilterOperators()
  739. {
  740. return parent::GetBasicFilterOperators();
  741. }
  742. public function GetBasicFilterLooseOperator()
  743. {
  744. return parent::GetBasicFilterLooseOperator();
  745. }
  746. public function GetBasicFilterSQLExpr($sOpCode, $value)
  747. {
  748. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  749. }
  750. // overloaded here so that an ext key always have the answer to
  751. // "what are your possible values?"
  752. public function GetValuesDef()
  753. {
  754. $oValSetDef = $this->Get("allowed_values");
  755. if (!$oValSetDef)
  756. {
  757. // Let's propose every existing value
  758. $oValSetDef = new ValueSetObjects($this->GetTargetClass());
  759. }
  760. return $oValSetDef;
  761. }
  762. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  763. {
  764. try
  765. {
  766. return parent::GetAllowedValues($aArgs, $sBeginsWith);
  767. }
  768. catch (MissingQueryArgument $e)
  769. {
  770. // Some required arguments could not be found, enlarge to any existing value
  771. $oValSetDef = new ValueSetObjects($this->GetTargetClass());
  772. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  773. }
  774. }
  775. }
  776. /**
  777. * An attribute which corresponds to an external key (direct or indirect)
  778. *
  779. * @package iTopORM
  780. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  781. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  782. * @link www.itop.com
  783. * @since 1.0
  784. * @version $itopversion$
  785. */
  786. class AttributeExternalField extends AttributeDefinition
  787. {
  788. static protected function ListExpectedParams()
  789. {
  790. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  791. }
  792. public function GetType() {return "ExtkeyField";}
  793. public function GetTypeDesc() {return "Field of an object pointed to by the current object";}
  794. public function GetEditClass() {return "ExtField";}
  795. public function GetDBFieldType()
  796. {
  797. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  798. $oExtAttDef = $this->GetExtAttDef();
  799. return $oExtAttDef->GetDBFieldType();
  800. }
  801. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  802. {
  803. switch($iType)
  804. {
  805. case EXTKEY_ABSOLUTE:
  806. // see further
  807. $oRemoteAtt = $this->GetExtAttDef();
  808. return $oRemoteAtt->IsExternalKey($iType);
  809. case EXTKEY_RELATIVE:
  810. return false;
  811. default:
  812. throw new CoreException("Unexpected value for argument iType: '$iType'");
  813. }
  814. }
  815. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  816. {
  817. return $this->GetKeyAttDef($iType)->GetTargetClass();
  818. }
  819. public function IsExternalField() {return true;}
  820. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  821. public function GetExtAttCode() {return $this->Get("target_attcode");}
  822. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  823. {
  824. switch($iType)
  825. {
  826. case EXTKEY_ABSOLUTE:
  827. // see further
  828. $oRemoteAtt = $this->GetExtAttDef();
  829. if ($oRemoteAtt->IsExternalField())
  830. {
  831. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  832. }
  833. else if ($oRemoteAtt->IsExternalKey())
  834. {
  835. return $oRemoteAtt;
  836. }
  837. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  838. case EXTKEY_RELATIVE:
  839. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  840. default:
  841. throw new CoreException("Unexpected value for argument iType: '$iType'");
  842. }
  843. }
  844. public function GetExtAttDef()
  845. {
  846. $oKeyAttDef = $this->GetKeyAttDef();
  847. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  848. return $oExtAttDef;
  849. }
  850. public function GetSQLExpr()
  851. {
  852. $oExtAttDef = $this->GetExtAttDef();
  853. return $oExtAttDef->GetSQLExpr();
  854. }
  855. public function DBGetUsedFields()
  856. {
  857. // No field is used but the one defined in the field of the external class
  858. // #@# so what ?
  859. return array();
  860. }
  861. public function GetDefaultValue()
  862. {
  863. $oExtAttDef = $this->GetExtAttDef();
  864. return $oExtAttDef->GetDefaultValue();
  865. }
  866. public function IsNullAllowed()
  867. {
  868. $oExtAttDef = $this->GetExtAttDef();
  869. return $oExtAttDef->IsNullAllowed();
  870. }
  871. public function GetBasicFilterOperators()
  872. {
  873. $oExtAttDef = $this->GetExtAttDef();
  874. return $oExtAttDef->GetBasicFilterOperators();
  875. }
  876. public function GetBasicFilterLooseOperator()
  877. {
  878. $oExtAttDef = $this->GetExtAttDef();
  879. return $oExtAttDef->GetBasicFilterLooseOperator();
  880. }
  881. public function GetBasicFilterSQLExpr($sOpCode, $value)
  882. {
  883. $oExtAttDef = $this->GetExtAttDef();
  884. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  885. }
  886. public function MakeRealValue($proposedValue)
  887. {
  888. $oExtAttDef = $this->GetExtAttDef();
  889. return $oExtAttDef->MakeRealValue($proposedValue);
  890. }
  891. public function RealValueToSQLValue($value)
  892. {
  893. // This one could be used in case of filtering only
  894. $oExtAttDef = $this->GetExtAttDef();
  895. return $oExtAttDef->RealValueToSQLValue($value);
  896. }
  897. public function SQLValueToRealValue($value)
  898. {
  899. $oExtAttDef = $this->GetExtAttDef();
  900. return $oExtAttDef->SQLValueToRealValue($value);
  901. }
  902. public function GetAsHTML($value)
  903. {
  904. $oExtAttDef = $this->GetExtAttDef();
  905. return $oExtAttDef->GetAsHTML($value);
  906. }
  907. public function GetAsXML($value)
  908. {
  909. $oExtAttDef = $this->GetExtAttDef();
  910. return $oExtAttDef->GetAsXML($value);
  911. }
  912. public function GetAsCSV($value, $sSeparator = ';', $sSepEscape = ',')
  913. {
  914. $oExtAttDef = $this->GetExtAttDef();
  915. return $oExtAttDef->GetAsCSV($value);
  916. }
  917. }
  918. /**
  919. * Map a varchar column to an URL (formats the ouput in HMTL)
  920. *
  921. * @package iTopORM
  922. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  923. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  924. * @link www.itop.com
  925. * @since 1.0
  926. * @version $itopversion$
  927. */
  928. class AttributeURL extends AttributeString
  929. {
  930. static protected function ListExpectedParams()
  931. {
  932. //return parent::ListExpectedParams();
  933. return array_merge(parent::ListExpectedParams(), array("target", "label"));
  934. }
  935. public function GetType() {return "Url";}
  936. public function GetTypeDesc() {return "Absolute or relative URL as a text string";}
  937. public function GetEditClass() {return "String";}
  938. public function GetAsHTML($sValue)
  939. {
  940. $sTarget = $this->Get("target");
  941. if (empty($sTarget)) $sTarget = "_blank";
  942. $sLabel = Str::pure2html($sValue);
  943. if (strlen($sLabel) > 40)
  944. {
  945. // Truncate the length to about 40 characters, by removing the middle
  946. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  947. }
  948. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  949. }
  950. }
  951. ?>