attributedef.class.inc.php 28 KB

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