attributedef.class.inc.php 31 KB

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