attributedef.class.inc.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. // #@# THIS HAS TO REVISED
  593. // Having null not allowed was interpreted by mySQL
  594. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  595. // Then, on each update of the record, the field was modified.
  596. // We will have to specify the default value if we want to restore this option
  597. // In fact, we could also have more verbs dedicated to the DB:
  598. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  599. public function IsNullAllowed() {return true;}
  600. public function GetDefaultValue()
  601. {
  602. $default = parent::GetDefaultValue();
  603. if (!parent::IsNullAllowed())
  604. {
  605. if (empty($default))
  606. {
  607. $default = date("Y-m-d H:i");
  608. }
  609. }
  610. return $default;
  611. }
  612. // END OF THE WORKAROUND
  613. ///////////////////////////////////////////////////////////////
  614. public function GetBasicFilterOperators()
  615. {
  616. return array(
  617. "="=>"equals",
  618. "!="=>"differs from",
  619. "<"=>"before",
  620. "<="=>"before",
  621. ">"=>"after (strictly)",
  622. ">="=>"after",
  623. "SameDay"=>"same day (strip time)",
  624. "SameMonth"=>"same year/month",
  625. "SameYear"=>"same year",
  626. "Today"=>"today",
  627. ">|"=>"after today + N days",
  628. "<|"=>"before today + N days",
  629. "=|"=>"equals today + N days",
  630. );
  631. }
  632. public function GetBasicFilterLooseOperator()
  633. {
  634. // Unless we implement a "same xxx, depending on given precision" !
  635. return "=";
  636. }
  637. public function GetBasicFilterSQLExpr($sOpCode, $value)
  638. {
  639. $sQValue = CMDBSource::Quote($value);
  640. switch ($sOpCode)
  641. {
  642. case '=':
  643. case '!=':
  644. case '<':
  645. case '<=':
  646. case '>':
  647. case '>=':
  648. return $this->GetSQLExpr()." $sOpCode $sQValue";
  649. case 'SameDay':
  650. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  651. case 'SameMonth':
  652. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  653. case 'SameYear':
  654. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  655. case 'Today':
  656. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  657. case '>|':
  658. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  659. case '<|':
  660. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  661. case '=|':
  662. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  663. default:
  664. return $this->GetSQLExpr()." = $sQValue";
  665. }
  666. }
  667. public function MakeRealValue($proposedValue)
  668. {
  669. if (!is_numeric($proposedValue))
  670. {
  671. return $proposedValue;
  672. }
  673. else
  674. {
  675. return date("Y-m-d H:i:s", $proposedValue);
  676. }
  677. throw new CoreException("Invalid type for a date (found ".gettype($proposedValue)." and accepting string/int/DateTime)");
  678. return null;
  679. }
  680. public function RealValueToSQLValue($value)
  681. {
  682. if (empty($value))
  683. {
  684. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  685. return '0000-00-00 00:00:00';
  686. }
  687. return $value;
  688. }
  689. public function SQLValueToRealValue($value)
  690. {
  691. return $value;
  692. }
  693. public function GetAsHTML($value)
  694. {
  695. return Str::pure2html($value);
  696. }
  697. public function GetAsXML($value)
  698. {
  699. return Str::pure2xml($value);
  700. }
  701. public function GetAsCSV($value, $sSeparator = ';', $sSepEscape = ',')
  702. {
  703. return str_replace($sSeparator, $sSepEscape, $value);
  704. }
  705. }
  706. // Init static constant once for all (remove when PHP allows real static const)
  707. AttributeDate::InitStatics();
  708. /**
  709. * Map a foreign key to an attribute
  710. * AttributeExternalKey and AttributeExternalField may be an external key
  711. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  712. * where an AttributeExternalField corresponds to a column into another table (class)
  713. *
  714. * @package iTopORM
  715. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  716. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  717. * @link www.itop.com
  718. * @since 1.0
  719. * @version $itopversion$
  720. */
  721. class AttributeExternalKey extends AttributeDBFieldVoid
  722. {
  723. static protected function ListExpectedParams()
  724. {
  725. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed"));
  726. }
  727. public function GetType() {return "Extkey";}
  728. public function GetTypeDesc() {return "Link to another object";}
  729. public function GetEditClass() {return "ExtKey";}
  730. public function GetDBFieldType() {return "INT";}
  731. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  732. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  733. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  734. public function GetKeyAttCode() {return $this->GetCode();}
  735. public function GetDefaultValue() {return 0;}
  736. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  737. public function GetBasicFilterOperators()
  738. {
  739. return parent::GetBasicFilterOperators();
  740. }
  741. public function GetBasicFilterLooseOperator()
  742. {
  743. return parent::GetBasicFilterLooseOperator();
  744. }
  745. public function GetBasicFilterSQLExpr($sOpCode, $value)
  746. {
  747. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  748. }
  749. // overloaded here so that an ext key always have the answer to
  750. // "what are your possible values?"
  751. public function GetValuesDef()
  752. {
  753. $oValSetDef = $this->Get("allowed_values");
  754. if (!$oValSetDef)
  755. {
  756. // Let's propose every existing value
  757. $oValSetDef = new ValueSetObjects($this->GetTargetClass());
  758. }
  759. return $oValSetDef;
  760. }
  761. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  762. {
  763. try
  764. {
  765. return parent::GetAllowedValues($aArgs, $sBeginsWith);
  766. }
  767. catch (MissingQueryArgument $e)
  768. {
  769. // Some required arguments could not be found, enlarge to any existing value
  770. $oValSetDef = new ValueSetObjects($this->GetTargetClass());
  771. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  772. }
  773. }
  774. }
  775. /**
  776. * An attribute which corresponds to an external key (direct or indirect)
  777. *
  778. * @package iTopORM
  779. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  780. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  781. * @link www.itop.com
  782. * @since 1.0
  783. * @version $itopversion$
  784. */
  785. class AttributeExternalField extends AttributeDefinition
  786. {
  787. static protected function ListExpectedParams()
  788. {
  789. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  790. }
  791. public function GetType() {return "ExtkeyField";}
  792. public function GetTypeDesc() {return "Field of an object pointed to by the current object";}
  793. public function GetEditClass() {return "ExtField";}
  794. public function GetDBFieldType()
  795. {
  796. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  797. $oExtAttDef = $this->GetExtAttDef();
  798. return $oExtAttDef->GetDBFieldType();
  799. }
  800. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  801. {
  802. switch($iType)
  803. {
  804. case EXTKEY_ABSOLUTE:
  805. // see further
  806. $oRemoteAtt = $this->GetExtAttDef();
  807. return $oRemoteAtt->IsExternalKey($iType);
  808. case EXTKEY_RELATIVE:
  809. return false;
  810. default:
  811. throw new CoreException("Unexpected value for argument iType: '$iType'");
  812. }
  813. }
  814. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  815. {
  816. return $this->GetKeyAttDef($iType)->GetTargetClass();
  817. }
  818. public function IsExternalField() {return true;}
  819. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  820. public function GetExtAttCode() {return $this->Get("target_attcode");}
  821. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  822. {
  823. switch($iType)
  824. {
  825. case EXTKEY_ABSOLUTE:
  826. // see further
  827. $oRemoteAtt = $this->GetExtAttDef();
  828. if ($oRemoteAtt->IsExternalField())
  829. {
  830. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  831. }
  832. else if ($oRemoteAtt->IsExternalKey())
  833. {
  834. return $oRemoteAtt;
  835. }
  836. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  837. case EXTKEY_RELATIVE:
  838. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  839. default:
  840. throw new CoreException("Unexpected value for argument iType: '$iType'");
  841. }
  842. }
  843. public function GetExtAttDef()
  844. {
  845. $oKeyAttDef = $this->GetKeyAttDef();
  846. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  847. return $oExtAttDef;
  848. }
  849. public function GetSQLExpr()
  850. {
  851. $oExtAttDef = $this->GetExtAttDef();
  852. return $oExtAttDef->GetSQLExpr();
  853. }
  854. public function DBGetUsedFields()
  855. {
  856. // No field is used but the one defined in the field of the external class
  857. // #@# so what ?
  858. return array();
  859. }
  860. public function GetDefaultValue()
  861. {
  862. $oExtAttDef = $this->GetExtAttDef();
  863. return $oExtAttDef->GetDefaultValue();
  864. }
  865. public function IsNullAllowed()
  866. {
  867. $oExtAttDef = $this->GetExtAttDef();
  868. return $oExtAttDef->IsNullAllowed();
  869. }
  870. public function GetBasicFilterOperators()
  871. {
  872. $oExtAttDef = $this->GetExtAttDef();
  873. return $oExtAttDef->GetBasicFilterOperators();
  874. }
  875. public function GetBasicFilterLooseOperator()
  876. {
  877. $oExtAttDef = $this->GetExtAttDef();
  878. return $oExtAttDef->GetBasicFilterLooseOperator();
  879. }
  880. public function GetBasicFilterSQLExpr($sOpCode, $value)
  881. {
  882. $oExtAttDef = $this->GetExtAttDef();
  883. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  884. }
  885. public function MakeRealValue($proposedValue)
  886. {
  887. $oExtAttDef = $this->GetExtAttDef();
  888. return $oExtAttDef->MakeRealValue($proposedValue);
  889. }
  890. public function RealValueToSQLValue($value)
  891. {
  892. // This one could be used in case of filtering only
  893. $oExtAttDef = $this->GetExtAttDef();
  894. return $oExtAttDef->RealValueToSQLValue($value);
  895. }
  896. public function SQLValueToRealValue($value)
  897. {
  898. $oExtAttDef = $this->GetExtAttDef();
  899. return $oExtAttDef->SQLValueToRealValue($value);
  900. }
  901. public function GetAsHTML($value)
  902. {
  903. $oExtAttDef = $this->GetExtAttDef();
  904. return $oExtAttDef->GetAsHTML($value);
  905. }
  906. public function GetAsXML($value)
  907. {
  908. $oExtAttDef = $this->GetExtAttDef();
  909. return $oExtAttDef->GetAsXML($value);
  910. }
  911. public function GetAsCSV($value, $sSeparator = ';', $sSepEscape = ',')
  912. {
  913. $oExtAttDef = $this->GetExtAttDef();
  914. return $oExtAttDef->GetAsCSV($value);
  915. }
  916. }
  917. /**
  918. * Map a varchar column to an URL (formats the ouput in HMTL)
  919. *
  920. * @package iTopORM
  921. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  922. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  923. * @link www.itop.com
  924. * @since 1.0
  925. * @version $itopversion$
  926. */
  927. class AttributeURL extends AttributeString
  928. {
  929. static protected function ListExpectedParams()
  930. {
  931. //return parent::ListExpectedParams();
  932. return array_merge(parent::ListExpectedParams(), array("target", "label"));
  933. }
  934. public function GetType() {return "Url";}
  935. public function GetTypeDesc() {return "Absolute or relative URL as a text string";}
  936. public function GetEditClass() {return "String";}
  937. public function GetAsHTML($sValue)
  938. {
  939. $sTarget = $this->Get("target");
  940. if (empty($sTarget)) $sTarget = "_blank";
  941. $sLabel = Str::pure2html($sValue);
  942. if (strlen($sLabel) > 40)
  943. {
  944. // Truncate the length to about 40 characters, by removing the middle
  945. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  946. }
  947. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  948. }
  949. }
  950. ?>