attributedef.class.inc.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. <?php
  2. require_once('MyHelpers.class.inc.php');
  3. require_once('ormdocument.class.inc.php');
  4. /**
  5. * MissingColumnException - sent if an attribute is being created but the column is missing in the row
  6. *
  7. * @package iTopORM
  8. */
  9. class MissingColumnException extends Exception
  10. {}
  11. /**
  12. * add some description here...
  13. *
  14. * @package iTopORM
  15. */
  16. define('EXTKEY_RELATIVE', 1);
  17. /**
  18. * add some description here...
  19. *
  20. * @package iTopORM
  21. */
  22. define('EXTKEY_ABSOLUTE', 2);
  23. /**
  24. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  25. *
  26. * @package iTopORM
  27. */
  28. define('DEL_MANUAL', 1);
  29. /**
  30. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  31. *
  32. * @package iTopORM
  33. */
  34. define('DEL_AUTO', 2);
  35. /**
  36. * Attribute definition API, implemented in and many flavours (Int, String, Enum, etc.)
  37. *
  38. * @package iTopORM
  39. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  40. * @author Denis Flaven <denisflave@free.fr>
  41. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  42. * @link www.itop.com
  43. * @since 1.0
  44. * @version 1.1.1.1 $
  45. */
  46. abstract class AttributeDefinition
  47. {
  48. abstract public function GetType();
  49. abstract public function GetTypeDesc();
  50. abstract public function GetEditClass();
  51. protected $m_sCode;
  52. private $m_aParams = array();
  53. private $m_sHostClass = '!undefined!';
  54. protected function Get($sParamName) {return $this->m_aParams[$sParamName];}
  55. protected function IsParam($sParamName) {return (array_key_exists($sParamName, $this->m_aParams));}
  56. public function __construct($sCode, $aParams)
  57. {
  58. $this->m_sCode = $sCode;
  59. $this->m_aParams = $aParams;
  60. $this->ConsistencyCheck();
  61. }
  62. public function OverloadParams($aParams)
  63. {
  64. foreach ($aParams as $sParam => $value)
  65. {
  66. if (!array_key_exists($sParam, $this->m_aParams))
  67. {
  68. throw new CoreException("Unknown attribute definition parameter '$sParam', please select a value in {".implode(", ", $this->m_aParams)."}");
  69. }
  70. else
  71. {
  72. $this->m_aParams[$sParam] = $value;
  73. }
  74. }
  75. }
  76. public function SetHostClass($sHostClass)
  77. {
  78. $this->m_sHostClass = $sHostClass;
  79. }
  80. public function GetHostClass()
  81. {
  82. return $this->m_sHostClass;
  83. }
  84. // Note: I could factorize this code with the parameter management made for the AttributeDef class
  85. // to be overloaded
  86. static protected function ListExpectedParams()
  87. {
  88. return array();
  89. }
  90. private function ConsistencyCheck()
  91. {
  92. // Check that any mandatory param has been specified
  93. //
  94. $aExpectedParams = $this->ListExpectedParams();
  95. foreach($aExpectedParams as $sParamName)
  96. {
  97. if (!array_key_exists($sParamName, $this->m_aParams))
  98. {
  99. $aBacktrace = debug_backtrace();
  100. $sTargetClass = $aBacktrace[2]["class"];
  101. $sCodeInfo = $aBacktrace[1]["file"]." - ".$aBacktrace[1]["line"];
  102. throw new Exception("ERROR missing parameter '$sParamName' in ".get_class($this)." declaration for class $sTargetClass ($sCodeInfo)");
  103. }
  104. }
  105. }
  106. // table, key field, name field
  107. public function ListDBJoins()
  108. {
  109. return "";
  110. // e.g: return array("Site", "infrid", "name");
  111. }
  112. public function IsDirectField() {return false;}
  113. public function IsScalar() {return false;}
  114. public function IsLinkSet() {return false;}
  115. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return false;}
  116. public function IsExternalField() {return false;}
  117. public function IsWritable() {return false;}
  118. public function IsNullAllowed() {return true;}
  119. public function GetNullValue() {return null;}
  120. public function GetCode() {return $this->m_sCode;}
  121. public function GetLabel() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $this->m_sCode);}
  122. public function GetLabel_Obsolete()
  123. {
  124. // Written for compatibility with a data model written prior to version 0.9.1
  125. if (array_key_exists('label', $this->m_aParams))
  126. {
  127. return $this->m_aParams['label'];
  128. }
  129. else
  130. {
  131. return $this->GetLabel();
  132. }
  133. }
  134. public function GetDescription() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', '');}
  135. public function GetDescription_Obsolete()
  136. {
  137. // Written for compatibility with a data model written prior to version 0.9.1
  138. if (array_key_exists('description', $this->m_aParams))
  139. {
  140. return $this->m_aParams['description'];
  141. }
  142. else
  143. {
  144. return $this->GetDescription();
  145. }
  146. }
  147. public function GetValuesDef() {return null;}
  148. public function GetPrerequisiteAttributes() {return array();}
  149. //public function IsSearchableStd() {return $this->Get("search_std");}
  150. //public function IsSearchableGlobal() {return $this->Get("search_global");}
  151. //public function IsMandatory() {return $this->Get("is_mandatory");}
  152. //public function GetMinVal() {return $this->Get("min");}
  153. //public function GetMaxVal() {return $this->Get("max");}
  154. //public function GetSize() {return $this->Get("size");}
  155. //public function GetCheckRegExp() {return $this->Get("regexp");}
  156. //public function GetCheckFunc() {return $this->Get("checkfunc");}
  157. public function MakeRealValue($proposedValue) {return $proposedValue;} // force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing!)
  158. public function GetSQLExpressions() {return array();} // returns suffix/expression pairs (1 in most of the cases), for READING (Select)
  159. public function FromSQLToValue($aCols, $sPrefix = '') {return null;} // returns a value out of suffix/value pairs, for SELECT result interpretation
  160. public function GetSQLColumns() {return array();} // returns column/spec pairs (1 in most of the cases), for STRUCTURING (DB creation)
  161. public function GetSQLValues($value) {return array();} // returns column/value pairs (1 in most of the cases), for WRITING (Insert, Update)
  162. public function GetJSCheckFunc()
  163. {
  164. $sRegExp = $this->Get("regexp");
  165. if (empty($sRegExp)) return 'return true;';
  166. return "return regexp('$sRegExp', myvalue);";
  167. }
  168. public function CheckValue($value)
  169. {
  170. $sRegExp = $this->Get("regexp");
  171. if (empty($sRegExp)) return true;
  172. return preg_match(preg_escape($this->Get("regexp")), $value);
  173. }
  174. public function MakeValue()
  175. {
  176. $sComputeFunc = $this->Get("compute_func");
  177. if (empty($sComputeFunc)) return null;
  178. return call_user_func($sComputeFunc);
  179. }
  180. abstract public function GetDefaultValue();
  181. //
  182. // To be overloaded in subclasses
  183. //
  184. abstract public function GetBasicFilterOperators(); // returns an array of "opCode"=>"description"
  185. abstract public function GetBasicFilterLooseOperator(); // returns an "opCode"
  186. //abstract protected GetBasicFilterHTMLInput();
  187. abstract public function GetBasicFilterSQLExpr($sOpCode, $value);
  188. public function GetEditValue($sValue)
  189. {
  190. return (string)$sValue;
  191. }
  192. public function GetAsHTML($sValue)
  193. {
  194. return Str::pure2html((string)$sValue);
  195. }
  196. public function GetAsXML($sValue)
  197. {
  198. return Str::pure2xml((string)$sValue);
  199. }
  200. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  201. {
  202. return (string)$sValue;
  203. }
  204. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  205. {
  206. $oValSetDef = $this->GetValuesDef();
  207. if (!$oValSetDef) return null;
  208. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  209. }
  210. }
  211. /**
  212. * Set of objects directly linked to an object, and being part of its definition
  213. *
  214. * @package iTopORM
  215. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  216. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  217. * @link www.itop.com
  218. * @since 1.0
  219. * @version $itopversion$
  220. */
  221. class AttributeLinkedSet extends AttributeDefinition
  222. {
  223. static protected function ListExpectedParams()
  224. {
  225. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "linked_class", "ext_key_to_me", "count_min", "count_max"));
  226. }
  227. public function GetType() {return "Array of objects";}
  228. public function GetTypeDesc() {return "Any kind of objects [subclass] of the same class";}
  229. public function GetEditClass() {return "List";}
  230. public function IsWritable() {return true;}
  231. public function IsLinkSet() {return true;}
  232. public function GetValuesDef() {return $this->Get("allowed_values");}
  233. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  234. public function GetDefaultValue($aArgs = array())
  235. {
  236. // Note: so far, this feature is a prototype,
  237. // later, the argument 'this' should always be present in the arguments
  238. //
  239. if (($this->IsParam('default_value')) && array_key_exists('this', $aArgs))
  240. {
  241. $aValues = $this->Get('default_value')->GetValues($aArgs);
  242. $oSet = DBObjectSet::FromArray($this->Get('linked_class'), $aValues);
  243. return $oSet;
  244. }
  245. else
  246. {
  247. return DBObjectSet::FromScratch($this->Get('linked_class'));
  248. }
  249. }
  250. public function GetLinkedClass() {return $this->Get('linked_class');}
  251. public function GetExtKeyToMe() {return $this->Get('ext_key_to_me');}
  252. public function GetBasicFilterOperators() {return array();}
  253. public function GetBasicFilterLooseOperator() {return '';}
  254. public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
  255. public function GetAsHTML($sValue)
  256. {
  257. return "ERROR: LIST OF OBJECTS";
  258. }
  259. public function GetAsXML($sValue)
  260. {
  261. return "ERROR: LIST OF OBJECTS";
  262. }
  263. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  264. {
  265. return "ERROR: LIST OF OBJECTS";
  266. }
  267. }
  268. /**
  269. * Set of objects linked to an object (n-n), and being part of its definition
  270. *
  271. * @package iTopORM
  272. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  273. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  274. * @link www.itop.com
  275. * @since 1.0
  276. * @version $itopversion$
  277. */
  278. class AttributeLinkedSetIndirect extends AttributeLinkedSet
  279. {
  280. static protected function ListExpectedParams()
  281. {
  282. return array_merge(parent::ListExpectedParams(), array("ext_key_to_remote"));
  283. }
  284. public function GetExtKeyToRemote() { return $this->Get('ext_key_to_remote'); }
  285. }
  286. /**
  287. * Abstract class implementing default filters for a DB column
  288. *
  289. * @package iTopORM
  290. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  291. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  292. * @link www.itop.com
  293. * @since 1.0
  294. * @version $itopversion$
  295. */
  296. class AttributeDBFieldVoid extends AttributeDefinition
  297. {
  298. static protected function ListExpectedParams()
  299. {
  300. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "sql"));
  301. }
  302. // To be overriden, used in GetSQLColumns
  303. protected function GetSQLCol() {return "VARCHAR(255)";}
  304. public function GetType() {return "Void";}
  305. public function GetTypeDesc() {return "Any kind of value, from the DB";}
  306. public function GetEditClass() {return "String";}
  307. public function GetValuesDef() {return $this->Get("allowed_values");}
  308. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  309. public function IsDirectField() {return true;}
  310. public function IsScalar() {return true;}
  311. public function IsWritable() {return true;}
  312. public function GetSQLExpr() {return $this->Get("sql");}
  313. public function GetDefaultValue() {return "";}
  314. public function IsNullAllowed() {return false;}
  315. protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  316. public function GetSQLExpressions()
  317. {
  318. $aColumns = array();
  319. // Note: to optimize things, the existence of the attribute is determine by the existence of one column with an empty suffix
  320. $aColumns[''] = $this->Get("sql");
  321. return $aColumns;
  322. }
  323. public function FromSQLToValue($aCols, $sPrefix = '')
  324. {
  325. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  326. return $value;
  327. }
  328. public function GetSQLValues($value)
  329. {
  330. $aValues = array();
  331. $aValues[$this->Get("sql")] = $this->ScalarToSQL($value);
  332. return $aValues;
  333. }
  334. public function GetSQLColumns()
  335. {
  336. $aColumns = array();
  337. $aColumns[$this->Get("sql")] = $this->GetSQLCol();
  338. return $aColumns;
  339. }
  340. public function GetBasicFilterOperators()
  341. {
  342. return array("="=>"equals", "!="=>"differs from");
  343. }
  344. public function GetBasicFilterLooseOperator()
  345. {
  346. return "=";
  347. }
  348. public function GetBasicFilterSQLExpr($sOpCode, $value)
  349. {
  350. $sQValue = CMDBSource::Quote($value);
  351. switch ($sOpCode)
  352. {
  353. case '!=':
  354. return $this->GetSQLExpr()." != $sQValue";
  355. break;
  356. case '=':
  357. default:
  358. return $this->GetSQLExpr()." = $sQValue";
  359. }
  360. }
  361. }
  362. /**
  363. * Base class for all kind of DB attributes, with the exception of external keys
  364. *
  365. * @package iTopORM
  366. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  367. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  368. * @link www.itop.com
  369. * @since 1.0
  370. * @version $itopversion$
  371. */
  372. class AttributeDBField extends AttributeDBFieldVoid
  373. {
  374. static protected function ListExpectedParams()
  375. {
  376. return array_merge(parent::ListExpectedParams(), array("default_value", "is_null_allowed"));
  377. }
  378. public function GetDefaultValue() {return $this->Get("default_value");}
  379. public function IsNullAllowed() {return strtolower($this->Get("is_null_allowed"));}
  380. }
  381. /**
  382. * Map an integer column to an attribute
  383. *
  384. * @package iTopORM
  385. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  386. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  387. * @link www.itop.com
  388. * @since 1.0
  389. * @version $itopversion$
  390. */
  391. class AttributeInteger extends AttributeDBField
  392. {
  393. static protected function ListExpectedParams()
  394. {
  395. return parent::ListExpectedParams();
  396. //return array_merge(parent::ListExpectedParams(), array());
  397. }
  398. public function GetType() {return "Integer";}
  399. public function GetTypeDesc() {return "Numeric value (could be negative)";}
  400. public function GetEditClass() {return "String";}
  401. protected function GetSQLCol() {return "INT(11)";}
  402. public function GetBasicFilterOperators()
  403. {
  404. return array(
  405. "!="=>"differs from",
  406. "="=>"equals",
  407. ">"=>"greater (strict) than",
  408. ">="=>"greater than",
  409. "<"=>"less (strict) than",
  410. "<="=>"less than",
  411. "in"=>"in"
  412. );
  413. }
  414. public function GetBasicFilterLooseOperator()
  415. {
  416. // Unless we implement an "equals approximately..." or "same order of magnitude"
  417. return "=";
  418. }
  419. public function GetBasicFilterSQLExpr($sOpCode, $value)
  420. {
  421. $sQValue = CMDBSource::Quote($value);
  422. switch ($sOpCode)
  423. {
  424. case '!=':
  425. return $this->GetSQLExpr()." != $sQValue";
  426. break;
  427. case '>':
  428. return $this->GetSQLExpr()." > $sQValue";
  429. break;
  430. case '>=':
  431. return $this->GetSQLExpr()." >= $sQValue";
  432. break;
  433. case '<':
  434. return $this->GetSQLExpr()." < $sQValue";
  435. break;
  436. case '<=':
  437. return $this->GetSQLExpr()." <= $sQValue";
  438. break;
  439. case 'in':
  440. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  441. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  442. break;
  443. case '=':
  444. default:
  445. return $this->GetSQLExpr()." = \"$value\"";
  446. }
  447. }
  448. public function MakeRealValue($proposedValue)
  449. {
  450. //return intval($proposedValue); could work as well
  451. return (int)$proposedValue;
  452. }
  453. public function ScalarToSQL($value)
  454. {
  455. assert(is_numeric($value));
  456. return $value; // supposed to be an int
  457. }
  458. }
  459. /**
  460. * Map a boolean column to an attribute
  461. *
  462. * @package iTopORM
  463. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  464. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  465. * @link www.itop.com
  466. * @since 1.0
  467. * @version $itopversion$
  468. */
  469. class AttributeBoolean extends AttributeInteger
  470. {
  471. static protected function ListExpectedParams()
  472. {
  473. return parent::ListExpectedParams();
  474. //return array_merge(parent::ListExpectedParams(), array());
  475. }
  476. public function GetType() {return "Boolean";}
  477. public function GetTypeDesc() {return "Boolean";}
  478. public function GetEditClass() {return "Integer";}
  479. protected function GetSQLCol() {return "TINYINT(1)";}
  480. public function MakeRealValue($proposedValue)
  481. {
  482. if ((int)$proposedValue) return true;
  483. return false;
  484. }
  485. public function ScalarToSQL($value)
  486. {
  487. assert(is_bool($value));
  488. if ($value) return 1;
  489. return 0;
  490. }
  491. }
  492. /**
  493. * Map a varchar column (size < ?) to an attribute
  494. *
  495. * @package iTopORM
  496. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  497. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  498. * @link www.itop.com
  499. * @since 1.0
  500. * @version $itopversion$
  501. */
  502. class AttributeString extends AttributeDBField
  503. {
  504. static protected function ListExpectedParams()
  505. {
  506. return parent::ListExpectedParams();
  507. //return array_merge(parent::ListExpectedParams(), array());
  508. }
  509. public function GetType() {return "String";}
  510. public function GetTypeDesc() {return "Alphanumeric string";}
  511. public function GetEditClass() {return "String";}
  512. protected function GetSQLCol() {return "VARCHAR(255)";}
  513. public function GetBasicFilterOperators()
  514. {
  515. return array(
  516. "="=>"equals",
  517. "!="=>"differs from",
  518. "Like"=>"equals (no case)",
  519. "NotLike"=>"differs from (no case)",
  520. "Contains"=>"contains",
  521. "Begins with"=>"begins with",
  522. "Finishes with"=>"finishes with"
  523. );
  524. }
  525. public function GetBasicFilterLooseOperator()
  526. {
  527. return "Contains";
  528. }
  529. public function GetBasicFilterSQLExpr($sOpCode, $value)
  530. {
  531. $sQValue = CMDBSource::Quote($value);
  532. switch ($sOpCode)
  533. {
  534. case '=':
  535. case '!=':
  536. return $this->GetSQLExpr()." $sOpCode $sQValue";
  537. case 'Begins with':
  538. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("$value%");
  539. case 'Finishes with':
  540. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value");
  541. case 'Contains':
  542. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
  543. case 'NotLike':
  544. return $this->GetSQLExpr()." NOT LIKE $sQValue";
  545. case 'Like':
  546. default:
  547. return $this->GetSQLExpr()." LIKE $sQValue";
  548. }
  549. }
  550. public function MakeRealValue($proposedValue)
  551. {
  552. return (string)$proposedValue;
  553. // if (!settype($proposedValue, "string"))
  554. // {
  555. // throw new CoreException("Failed to change the type of '$proposedValue' to a string");
  556. // }
  557. }
  558. public function ScalarToSQL($value)
  559. {
  560. if (!is_string($value) && !is_null($value))
  561. {
  562. throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetCode(), 'attribute' => $this->GetHostClass()));
  563. }
  564. return $value;
  565. }
  566. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  567. {
  568. $sFrom = array("\r\n", $sTextQualifier);
  569. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  570. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  571. return '"'.$sEscaped.'"';
  572. }
  573. }
  574. /**
  575. * An attibute that matches an object class
  576. *
  577. * @package iTopORM
  578. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  579. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  580. * @link www.itop.com
  581. * @since 1.0
  582. * @version $itopversion$
  583. */
  584. class AttributeClass extends AttributeString
  585. {
  586. static protected function ListExpectedParams()
  587. {
  588. return array_merge(parent::ListExpectedParams(), array("class_category", "more_values"));
  589. }
  590. public function __construct($sCode, $aParams)
  591. {
  592. $this->m_sCode = $sCode;
  593. $aParams["allowed_values"] = new ValueSetEnumClasses($aParams['class_category'], $aParams['more_values']);
  594. parent::__construct($sCode, $aParams);
  595. }
  596. public function GetAsHTML($sValue)
  597. {
  598. return MetaModel::GetName($sValue);
  599. }
  600. }
  601. /**
  602. * The attribute dedicated to the finalclass automatic attribute
  603. *
  604. * @package iTopORM
  605. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  606. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  607. * @link www.itop.com
  608. * @since 1.0
  609. * @version $itopversion$
  610. */
  611. class AttributeFinalClass extends AttributeString
  612. {
  613. public function __construct($sCode, $aParams)
  614. {
  615. $this->m_sCode = $sCode;
  616. $aParams["allowed_values"] = null;
  617. parent::__construct($sCode, $aParams);
  618. }
  619. public function IsWritable()
  620. {
  621. return false;
  622. }
  623. public function GetAsHTML($sValue)
  624. {
  625. return MetaModel::GetName($sValue);
  626. }
  627. }
  628. /**
  629. * Map a varchar column (size < ?) to an attribute that must never be shown to the user
  630. *
  631. * @package iTopORM
  632. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  633. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  634. * @link www.itop.com
  635. * @since 1.0
  636. * @version $itopversion$
  637. */
  638. class AttributePassword extends AttributeString
  639. {
  640. static protected function ListExpectedParams()
  641. {
  642. return parent::ListExpectedParams();
  643. //return array_merge(parent::ListExpectedParams(), array());
  644. }
  645. public function GetEditClass() {return "Password";}
  646. protected function GetSQLCol() {return "VARCHAR(64)";}
  647. }
  648. /**
  649. * Map a text column (size > ?) to an attribute
  650. *
  651. * @package iTopORM
  652. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  653. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  654. * @link www.itop.com
  655. * @since 1.0
  656. * @version $itopversion$
  657. */
  658. class AttributeText extends AttributeString
  659. {
  660. public function GetType() {return "Text";}
  661. public function GetTypeDesc() {return "Multiline character string";}
  662. public function GetEditClass() {return "Text";}
  663. protected function GetSQLCol() {return "TEXT";}
  664. public function GetAsHTML($sValue)
  665. {
  666. return str_replace("\n", "<br>\n", parent::GetAsHTML($sValue));
  667. }
  668. public function GetAsXML($value)
  669. {
  670. return Str::pure2xml($value);
  671. }
  672. }
  673. /**
  674. * Specialization of a string: email
  675. *
  676. * @package iTopORM
  677. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  678. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  679. * @link www.itop.com
  680. * @since 1.0
  681. * @version $itopversion$
  682. */
  683. class AttributeEmailAddress extends AttributeString
  684. {
  685. public function GetTypeDesc() {return "Email address(es)";}
  686. }
  687. /**
  688. * Specialization of a string: OQL expression
  689. *
  690. * @package iTopORM
  691. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  692. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  693. * @link www.itop.com
  694. * @since 1.0
  695. * @version $itopversion$
  696. */
  697. class AttributeOQL extends AttributeString
  698. {
  699. public function GetTypeDesc() {return "OQL expression";}
  700. }
  701. /**
  702. * Specialization of a string: template
  703. *
  704. * @package iTopORM
  705. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  706. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  707. * @link www.itop.com
  708. * @since 1.0
  709. * @version $itopversion$
  710. */
  711. class AttributeTemplateString extends AttributeString
  712. {
  713. public function GetTypeDesc() {return "Template string";}
  714. }
  715. /**
  716. * Specialization of a text: template
  717. *
  718. * @package iTopORM
  719. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  720. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  721. * @link www.itop.com
  722. * @since 1.0
  723. * @version $itopversion$
  724. */
  725. class AttributeTemplateText extends AttributeText
  726. {
  727. public function GetTypeDesc() {return "Multiline template string";}
  728. }
  729. /**
  730. * Map a enum column to an attribute
  731. *
  732. * @package iTopORM
  733. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  734. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  735. * @link www.itop.com
  736. * @since 1.0
  737. * @version $itopversion$
  738. */
  739. class AttributeEnum extends AttributeString
  740. {
  741. static protected function ListExpectedParams()
  742. {
  743. return parent::ListExpectedParams();
  744. //return array_merge(parent::ListExpectedParams(), array());
  745. }
  746. public function GetType() {return "Enum";}
  747. public function GetTypeDesc() {return "List of predefined alphanumeric strings";}
  748. public function GetEditClass() {return "String";}
  749. protected function GetSQLCol()
  750. {
  751. $oValDef = $this->GetValuesDef();
  752. if ($oValDef)
  753. {
  754. $aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
  755. }
  756. else
  757. {
  758. $aValues = array();
  759. }
  760. if (count($aValues) > 0)
  761. {
  762. // The syntax used here is matters
  763. // In particular, I had to remove unnecessary spaces to stick to
  764. // make sure that this string will match the field type returned by the DB
  765. // (used to perform a comparison between the current DB format and the data model)
  766. return "ENUM(".implode(",", $aValues).")";
  767. }
  768. else
  769. {
  770. return "VARCHAR(255)"; // ENUM() is not an allowed syntax!
  771. }
  772. }
  773. public function GetBasicFilterOperators()
  774. {
  775. return parent::GetBasicFilterOperators();
  776. }
  777. public function GetBasicFilterLooseOperator()
  778. {
  779. return parent::GetBasicFilterLooseOperator();
  780. }
  781. public function GetBasicFilterSQLExpr($sOpCode, $value)
  782. {
  783. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  784. }
  785. public function GetAsHTML($sValue)
  786. {
  787. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  788. // later, we could imagine a detailed description in the title
  789. return "<span title=\"\">".parent::GetAsHtml($sLabel)."</span>";
  790. }
  791. public function GetEditValue($sValue)
  792. {
  793. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  794. return $sLabel;
  795. }
  796. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  797. {
  798. $aRawValues = parent::GetAllowedValues($aArgs, $sBeginsWith);
  799. $aLocalizedValues = array();
  800. foreach ($aRawValues as $sKey => $sValue)
  801. {
  802. $aLocalizedValues[$sKey] = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sKey, $sKey);
  803. }
  804. return $aLocalizedValues;
  805. }
  806. }
  807. /**
  808. * Map a date+time column to an attribute
  809. *
  810. * @package iTopORM
  811. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  812. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  813. * @link www.itop.com
  814. * @since 1.0
  815. * @version $itopversion$
  816. */
  817. class AttributeDate extends AttributeDBField
  818. {
  819. const MYDATEFORMAT = "Y-m-d H:i:s";
  820. //const MYDATETIMEZONE = "UTC";
  821. const MYDATETIMEZONE = "Europe/Paris";
  822. static protected $const_TIMEZONE = null; // set once for all upon object construct
  823. static public function InitStatics()
  824. {
  825. // Init static constant once for all (remove when PHP allows real static const)
  826. self::$const_TIMEZONE = new DateTimeZone(self::MYDATETIMEZONE);
  827. // #@# Init default timezone -> do not get a notice... to be improved !!!
  828. // duplicated in the email test page (the mail function does trigger a notice as well)
  829. date_default_timezone_set(self::MYDATETIMEZONE);
  830. }
  831. static protected function ListExpectedParams()
  832. {
  833. return parent::ListExpectedParams();
  834. //return array_merge(parent::ListExpectedParams(), array());
  835. }
  836. public function GetType() {return "Date";}
  837. public function GetTypeDesc() {return "Date and time";}
  838. public function GetEditClass() {return "Date";}
  839. protected function GetSQLCol() {return "TIMESTAMP";}
  840. // #@# THIS HAS TO REVISED
  841. // Having null not allowed was interpreted by mySQL
  842. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  843. // Then, on each update of the record, the field was modified.
  844. // We will have to specify the default value if we want to restore this option
  845. // In fact, we could also have more verbs dedicated to the DB:
  846. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  847. public function IsNullAllowed() {return true;}
  848. public function GetDefaultValue()
  849. {
  850. $default = parent::GetDefaultValue();
  851. if (!parent::IsNullAllowed())
  852. {
  853. if (empty($default))
  854. {
  855. $default = date("Y-m-d H:i");
  856. }
  857. }
  858. return $default;
  859. }
  860. // END OF THE WORKAROUND
  861. ///////////////////////////////////////////////////////////////
  862. public function GetBasicFilterOperators()
  863. {
  864. return array(
  865. "="=>"equals",
  866. "!="=>"differs from",
  867. "<"=>"before",
  868. "<="=>"before",
  869. ">"=>"after (strictly)",
  870. ">="=>"after",
  871. "SameDay"=>"same day (strip time)",
  872. "SameMonth"=>"same year/month",
  873. "SameYear"=>"same year",
  874. "Today"=>"today",
  875. ">|"=>"after today + N days",
  876. "<|"=>"before today + N days",
  877. "=|"=>"equals today + N days",
  878. );
  879. }
  880. public function GetBasicFilterLooseOperator()
  881. {
  882. // Unless we implement a "same xxx, depending on given precision" !
  883. return "=";
  884. }
  885. public function GetBasicFilterSQLExpr($sOpCode, $value)
  886. {
  887. $sQValue = CMDBSource::Quote($value);
  888. switch ($sOpCode)
  889. {
  890. case '=':
  891. case '!=':
  892. case '<':
  893. case '<=':
  894. case '>':
  895. case '>=':
  896. return $this->GetSQLExpr()." $sOpCode $sQValue";
  897. case 'SameDay':
  898. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  899. case 'SameMonth':
  900. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  901. case 'SameYear':
  902. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  903. case 'Today':
  904. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  905. case '>|':
  906. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  907. case '<|':
  908. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  909. case '=|':
  910. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  911. default:
  912. return $this->GetSQLExpr()." = $sQValue";
  913. }
  914. }
  915. public function MakeRealValue($proposedValue)
  916. {
  917. if (!is_numeric($proposedValue))
  918. {
  919. return $proposedValue;
  920. }
  921. else
  922. {
  923. return date("Y-m-d H:i:s", $proposedValue);
  924. }
  925. throw new CoreException("Invalid type for a date (found ".gettype($proposedValue)." and accepting string/int/DateTime)");
  926. return null;
  927. }
  928. public function ScalarToSQL($value)
  929. {
  930. if (empty($value))
  931. {
  932. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  933. return '0000-00-00 00:00:00';
  934. }
  935. return $value;
  936. }
  937. public function GetAsHTML($value)
  938. {
  939. return Str::pure2html($value);
  940. }
  941. public function GetAsXML($value)
  942. {
  943. return Str::pure2xml($value);
  944. }
  945. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  946. {
  947. $sFrom = array("\r\n", $sTextQualifier);
  948. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  949. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  950. return '"'.$sEscaped.'"';
  951. }
  952. }
  953. // Init static constant once for all (remove when PHP allows real static const)
  954. AttributeDate::InitStatics();
  955. /**
  956. * Map a foreign key to an attribute
  957. * AttributeExternalKey and AttributeExternalField may be an external key
  958. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  959. * where an AttributeExternalField corresponds to a column into another table (class)
  960. *
  961. * @package iTopORM
  962. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  963. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  964. * @link www.itop.com
  965. * @since 1.0
  966. * @version $itopversion$
  967. */
  968. class AttributeExternalKey extends AttributeDBFieldVoid
  969. {
  970. static protected function ListExpectedParams()
  971. {
  972. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed", "on_target_delete"));
  973. }
  974. public function GetType() {return "Extkey";}
  975. public function GetTypeDesc() {return "Link to another object";}
  976. public function GetEditClass() {return "ExtKey";}
  977. protected function GetSQLCol() {return "INT(11)";}
  978. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  979. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  980. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  981. public function GetKeyAttCode() {return $this->GetCode();}
  982. public function GetDefaultValue() {return 0;}
  983. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  984. public function GetNullValue() {return 0;}
  985. public function GetBasicFilterOperators()
  986. {
  987. return parent::GetBasicFilterOperators();
  988. }
  989. public function GetBasicFilterLooseOperator()
  990. {
  991. return parent::GetBasicFilterLooseOperator();
  992. }
  993. public function GetBasicFilterSQLExpr($sOpCode, $value)
  994. {
  995. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  996. }
  997. // overloaded here so that an ext key always have the answer to
  998. // "what are your possible values?"
  999. public function GetValuesDef()
  1000. {
  1001. $oValSetDef = $this->Get("allowed_values");
  1002. if (!$oValSetDef)
  1003. {
  1004. // Let's propose every existing value
  1005. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1006. }
  1007. return $oValSetDef;
  1008. }
  1009. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  1010. {
  1011. try
  1012. {
  1013. return parent::GetAllowedValues($aArgs, $sBeginsWith);
  1014. }
  1015. catch (MissingQueryArgument $e)
  1016. {
  1017. // Some required arguments could not be found, enlarge to any existing value
  1018. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1019. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  1020. }
  1021. }
  1022. public function GetDeletionPropagationOption()
  1023. {
  1024. return $this->Get("on_target_delete");
  1025. }
  1026. public function MakeRealValue($proposedValue)
  1027. {
  1028. return (int)$proposedValue;
  1029. }
  1030. }
  1031. /**
  1032. * An attribute which corresponds to an external key (direct or indirect)
  1033. *
  1034. * @package iTopORM
  1035. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  1036. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1037. * @link www.itop.com
  1038. * @since 1.0
  1039. * @version $itopversion$
  1040. */
  1041. class AttributeExternalField extends AttributeDefinition
  1042. {
  1043. static protected function ListExpectedParams()
  1044. {
  1045. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  1046. }
  1047. public function GetType() {return "ExtkeyField";}
  1048. public function GetTypeDesc() {return "Field of an object pointed to by the current object";}
  1049. public function GetEditClass() {return "ExtField";}
  1050. protected function GetSQLCol()
  1051. {
  1052. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  1053. $oExtAttDef = $this->GetExtAttDef();
  1054. return $oExtAttDef->GetSQLCol();
  1055. }
  1056. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  1057. {
  1058. switch($iType)
  1059. {
  1060. case EXTKEY_ABSOLUTE:
  1061. // see further
  1062. $oRemoteAtt = $this->GetExtAttDef();
  1063. return $oRemoteAtt->IsExternalKey($iType);
  1064. case EXTKEY_RELATIVE:
  1065. return false;
  1066. default:
  1067. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1068. }
  1069. }
  1070. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  1071. {
  1072. return $this->GetKeyAttDef($iType)->GetTargetClass();
  1073. }
  1074. public function IsExternalField() {return true;}
  1075. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  1076. public function GetExtAttCode() {return $this->Get("target_attcode");}
  1077. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  1078. {
  1079. switch($iType)
  1080. {
  1081. case EXTKEY_ABSOLUTE:
  1082. // see further
  1083. $oRemoteAtt = $this->GetExtAttDef();
  1084. if ($oRemoteAtt->IsExternalField())
  1085. {
  1086. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  1087. }
  1088. else if ($oRemoteAtt->IsExternalKey())
  1089. {
  1090. return $oRemoteAtt;
  1091. }
  1092. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  1093. case EXTKEY_RELATIVE:
  1094. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  1095. default:
  1096. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1097. }
  1098. }
  1099. public function GetExtAttDef()
  1100. {
  1101. $oKeyAttDef = $this->GetKeyAttDef();
  1102. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  1103. return $oExtAttDef;
  1104. }
  1105. public function GetSQLExpr()
  1106. {
  1107. $oExtAttDef = $this->GetExtAttDef();
  1108. return $oExtAttDef->GetSQLExpr();
  1109. }
  1110. public function GetDefaultValue()
  1111. {
  1112. $oExtAttDef = $this->GetExtAttDef();
  1113. return $oExtAttDef->GetDefaultValue();
  1114. }
  1115. public function IsNullAllowed()
  1116. {
  1117. $oExtAttDef = $this->GetExtAttDef();
  1118. return $oExtAttDef->IsNullAllowed();
  1119. }
  1120. public function IsScalar()
  1121. {
  1122. $oExtAttDef = $this->GetExtAttDef();
  1123. return $oExtAttDef->IsScalar();
  1124. }
  1125. public function GetBasicFilterOperators()
  1126. {
  1127. $oExtAttDef = $this->GetExtAttDef();
  1128. return $oExtAttDef->GetBasicFilterOperators();
  1129. }
  1130. public function GetBasicFilterLooseOperator()
  1131. {
  1132. $oExtAttDef = $this->GetExtAttDef();
  1133. return $oExtAttDef->GetBasicFilterLooseOperator();
  1134. }
  1135. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1136. {
  1137. $oExtAttDef = $this->GetExtAttDef();
  1138. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  1139. }
  1140. public function MakeRealValue($proposedValue)
  1141. {
  1142. $oExtAttDef = $this->GetExtAttDef();
  1143. return $oExtAttDef->MakeRealValue($proposedValue);
  1144. }
  1145. public function ScalarToSQL($value)
  1146. {
  1147. // This one could be used in case of filtering only
  1148. $oExtAttDef = $this->GetExtAttDef();
  1149. return $oExtAttDef->ScalarToSQL($value);
  1150. }
  1151. // Do not overload GetSQLExpression here because this is handled in the joins
  1152. //public function GetSQLExpressions() {return array();}
  1153. // Here, we get the data...
  1154. public function FromSQLToValue($aCols, $sPrefix = '')
  1155. {
  1156. $oExtAttDef = $this->GetExtAttDef();
  1157. return $oExtAttDef->FromSQLToValue($aCols, $sPrefix);
  1158. }
  1159. public function GetAsHTML($value)
  1160. {
  1161. $oExtAttDef = $this->GetExtAttDef();
  1162. return $oExtAttDef->GetAsHTML($value);
  1163. }
  1164. public function GetAsXML($value)
  1165. {
  1166. $oExtAttDef = $this->GetExtAttDef();
  1167. return $oExtAttDef->GetAsXML($value);
  1168. }
  1169. public function GetAsCSV($value, $sSeparator = ',', $sTestQualifier = '"')
  1170. {
  1171. $oExtAttDef = $this->GetExtAttDef();
  1172. return $oExtAttDef->GetAsCSV($value, $sSeparator, $sTestQualifier);
  1173. }
  1174. }
  1175. /**
  1176. * Map a varchar column to an URL (formats the ouput in HMTL)
  1177. *
  1178. * @package iTopORM
  1179. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  1180. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1181. * @link www.itop.com
  1182. * @since 1.0
  1183. * @version $itopversion$
  1184. */
  1185. class AttributeURL extends AttributeString
  1186. {
  1187. static protected function ListExpectedParams()
  1188. {
  1189. //return parent::ListExpectedParams();
  1190. return array_merge(parent::ListExpectedParams(), array("target"));
  1191. }
  1192. public function GetType() {return "Url";}
  1193. public function GetTypeDesc() {return "Absolute or relative URL as a text string";}
  1194. public function GetEditClass() {return "String";}
  1195. public function GetAsHTML($sValue)
  1196. {
  1197. $sTarget = $this->Get("target");
  1198. if (empty($sTarget)) $sTarget = "_blank";
  1199. $sLabel = Str::pure2html($sValue);
  1200. if (strlen($sLabel) > 40)
  1201. {
  1202. // Truncate the length to about 40 characters, by removing the middle
  1203. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  1204. }
  1205. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  1206. }
  1207. }
  1208. /**
  1209. * Data column, consisting in TWO columns in the DB
  1210. *
  1211. * @package iTopORM
  1212. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  1213. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1214. * @link www.itop.com
  1215. * @since 1.0
  1216. * @version $itopversion$
  1217. */
  1218. class AttributeBlob extends AttributeDefinition
  1219. {
  1220. static protected function ListExpectedParams()
  1221. {
  1222. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  1223. }
  1224. public function GetType() {return "Blob";}
  1225. public function GetTypeDesc() {return "Document";}
  1226. public function GetEditClass() {return "Document";}
  1227. public function IsDirectField() {return true;}
  1228. public function IsScalar() {return true;}
  1229. public function IsWritable() {return true;}
  1230. public function GetDefaultValue() {return "";}
  1231. public function IsNullAllowed() {return false;}
  1232. // Facilitate things: allow the user to Set the value from a string
  1233. public function MakeRealValue($proposedValue)
  1234. {
  1235. if (!is_object($proposedValue))
  1236. {
  1237. return new ormDocument($proposedValue, 'text/plain');
  1238. }
  1239. return $proposedValue;
  1240. }
  1241. public function GetSQLExpressions()
  1242. {
  1243. $aColumns = array();
  1244. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  1245. $aColumns[''] = $this->GetCode().'_mimetype';
  1246. $aColumns['_data'] = $this->GetCode().'_data';
  1247. $aColumns['_filename'] = $this->GetCode().'_filename';
  1248. return $aColumns;
  1249. }
  1250. public function FromSQLToValue($aCols, $sPrefix = '')
  1251. {
  1252. if (!isset($aCols[$sPrefix]))
  1253. {
  1254. $sAvailable = implode(', ', array_keys($aCols));
  1255. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  1256. }
  1257. $sMimeType = $aCols[$sPrefix];
  1258. if (!isset($aCols[$sPrefix.'_data']))
  1259. {
  1260. $sAvailable = implode(', ', array_keys($aCols));
  1261. throw new MissingColumnException("Missing column '".$sPrefix."_data' from {$sAvailable}");
  1262. }
  1263. $data = $aCols[$sPrefix.'_data'];
  1264. if (!isset($aCols[$sPrefix.'_filename']))
  1265. {
  1266. $sAvailable = implode(', ', array_keys($aCols));
  1267. throw new MissingColumnException("Missing column '".$sPrefix."_filename' from {$sAvailable}");
  1268. }
  1269. $sFileName = $aCols[$sPrefix.'_filename'];
  1270. $value = new ormDocument($data, $sMimeType, $sFileName);
  1271. return $value;
  1272. }
  1273. public function GetSQLValues($value)
  1274. {
  1275. // #@# Optimization: do not load blobs anytime
  1276. // As per mySQL doc, selecting blob columns will prevent mySQL from
  1277. // using memory in case a temporary table has to be created
  1278. // (temporary tables created on disk)
  1279. // We will have to remove the blobs from the list of attributes when doing the select
  1280. // then the use of Get() should finalize the load
  1281. $aValues = array();
  1282. $aValues[$this->GetCode().'_data'] = $value->GetData();
  1283. $aValues[$this->GetCode().'_mimetype'] = $value->GetMimeType();
  1284. $aValues[$this->GetCode().'_filename'] = $value->GetFileName();
  1285. return $aValues;
  1286. }
  1287. public function GetSQLColumns()
  1288. {
  1289. $aColumns = array();
  1290. $aColumns[$this->GetCode().'_data'] = 'LONGBLOB'; // 2^32 (4 Gb)
  1291. $aColumns[$this->GetCode().'_mimetype'] = 'VARCHAR(255)';
  1292. $aColumns[$this->GetCode().'_filename'] = 'VARCHAR(255)';
  1293. return $aColumns;
  1294. }
  1295. public function GetBasicFilterOperators()
  1296. {
  1297. return array();
  1298. }
  1299. public function GetBasicFilterLooseOperator()
  1300. {
  1301. return '=';
  1302. }
  1303. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1304. {
  1305. return 'true';
  1306. }
  1307. public function GetAsHTML($value)
  1308. {
  1309. if (is_object($value))
  1310. {
  1311. return $value->GetAsHTML();
  1312. }
  1313. }
  1314. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  1315. {
  1316. return ''; // Not exportable in CSV !
  1317. }
  1318. public function GetAsXML($value)
  1319. {
  1320. return ''; // Not exportable in XML, or as CDATA + some subtags ??
  1321. }
  1322. }
  1323. ?>