attributedef.class.inc.php 30 KB

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