attributedef.class.inc.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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. $this->m_sValue = $this->Get("default_value");
  619. }
  620. public function IsWritable()
  621. {
  622. return false;
  623. }
  624. public function SetFixedValue($sValue)
  625. {
  626. $this->m_sValue = $sValue;
  627. }
  628. public function GetDefaultValue()
  629. {
  630. return $this->m_sValue;
  631. }
  632. public function GetAsHTML($sValue)
  633. {
  634. return MetaModel::GetName($sValue);
  635. }
  636. }
  637. /**
  638. * Map a varchar column (size < ?) to an attribute that must never be shown to the user
  639. *
  640. * @package iTopORM
  641. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  642. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  643. * @link www.itop.com
  644. * @since 1.0
  645. * @version $itopversion$
  646. */
  647. class AttributePassword extends AttributeString
  648. {
  649. static protected function ListExpectedParams()
  650. {
  651. return parent::ListExpectedParams();
  652. //return array_merge(parent::ListExpectedParams(), array());
  653. }
  654. public function GetEditClass() {return "Password";}
  655. protected function GetSQLCol() {return "VARCHAR(64)";}
  656. }
  657. /**
  658. * Map a text column (size > ?) to an attribute
  659. *
  660. * @package iTopORM
  661. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  662. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  663. * @link www.itop.com
  664. * @since 1.0
  665. * @version $itopversion$
  666. */
  667. class AttributeText extends AttributeString
  668. {
  669. public function GetType() {return "Text";}
  670. public function GetTypeDesc() {return "Multiline character string";}
  671. public function GetEditClass() {return "Text";}
  672. protected function GetSQLCol() {return "TEXT";}
  673. public function GetAsHTML($sValue)
  674. {
  675. return str_replace("\n", "<br>\n", parent::GetAsHTML($sValue));
  676. }
  677. public function GetAsXML($value)
  678. {
  679. return Str::pure2xml($value);
  680. }
  681. }
  682. /**
  683. * Specialization of a string: email
  684. *
  685. * @package iTopORM
  686. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  687. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  688. * @link www.itop.com
  689. * @since 1.0
  690. * @version $itopversion$
  691. */
  692. class AttributeEmailAddress extends AttributeString
  693. {
  694. public function GetTypeDesc() {return "Email address(es)";}
  695. }
  696. /**
  697. * Specialization of a string: OQL expression
  698. *
  699. * @package iTopORM
  700. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  701. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  702. * @link www.itop.com
  703. * @since 1.0
  704. * @version $itopversion$
  705. */
  706. class AttributeOQL extends AttributeString
  707. {
  708. public function GetTypeDesc() {return "OQL expression";}
  709. }
  710. /**
  711. * Specialization of a string: template
  712. *
  713. * @package iTopORM
  714. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  715. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  716. * @link www.itop.com
  717. * @since 1.0
  718. * @version $itopversion$
  719. */
  720. class AttributeTemplateString extends AttributeString
  721. {
  722. public function GetTypeDesc() {return "Template string";}
  723. }
  724. /**
  725. * Specialization of a text: template
  726. *
  727. * @package iTopORM
  728. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  729. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  730. * @link www.itop.com
  731. * @since 1.0
  732. * @version $itopversion$
  733. */
  734. class AttributeTemplateText extends AttributeText
  735. {
  736. public function GetTypeDesc() {return "Multiline template string";}
  737. }
  738. /**
  739. * Map a enum column to an attribute
  740. *
  741. * @package iTopORM
  742. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  743. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  744. * @link www.itop.com
  745. * @since 1.0
  746. * @version $itopversion$
  747. */
  748. class AttributeEnum extends AttributeString
  749. {
  750. static protected function ListExpectedParams()
  751. {
  752. return parent::ListExpectedParams();
  753. //return array_merge(parent::ListExpectedParams(), array());
  754. }
  755. public function GetType() {return "Enum";}
  756. public function GetTypeDesc() {return "List of predefined alphanumeric strings";}
  757. public function GetEditClass() {return "String";}
  758. protected function GetSQLCol()
  759. {
  760. $oValDef = $this->GetValuesDef();
  761. if ($oValDef)
  762. {
  763. $aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
  764. }
  765. else
  766. {
  767. $aValues = array();
  768. }
  769. if (count($aValues) > 0)
  770. {
  771. // The syntax used here is matters
  772. // In particular, I had to remove unnecessary spaces to stick to
  773. // make sure that this string will match the field type returned by the DB
  774. // (used to perform a comparison between the current DB format and the data model)
  775. return "ENUM(".implode(",", $aValues).")";
  776. }
  777. else
  778. {
  779. return "VARCHAR(255)"; // ENUM() is not an allowed syntax!
  780. }
  781. }
  782. public function GetBasicFilterOperators()
  783. {
  784. return parent::GetBasicFilterOperators();
  785. }
  786. public function GetBasicFilterLooseOperator()
  787. {
  788. return parent::GetBasicFilterLooseOperator();
  789. }
  790. public function GetBasicFilterSQLExpr($sOpCode, $value)
  791. {
  792. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  793. }
  794. public function GetAsHTML($sValue)
  795. {
  796. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  797. // later, we could imagine a detailed description in the title
  798. return "<span title=\"\">".parent::GetAsHtml($sLabel)."</span>";
  799. }
  800. public function GetEditValue($sValue)
  801. {
  802. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  803. return $sLabel;
  804. }
  805. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  806. {
  807. $aRawValues = parent::GetAllowedValues($aArgs, $sBeginsWith);
  808. $aLocalizedValues = array();
  809. foreach ($aRawValues as $sKey => $sValue)
  810. {
  811. $aLocalizedValues[$sKey] = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sKey, $sKey);
  812. }
  813. return $aLocalizedValues;
  814. }
  815. }
  816. /**
  817. * Map a date+time column to an attribute
  818. *
  819. * @package iTopORM
  820. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  821. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  822. * @link www.itop.com
  823. * @since 1.0
  824. * @version $itopversion$
  825. */
  826. class AttributeDate extends AttributeDBField
  827. {
  828. const MYDATEFORMAT = "Y-m-d H:i:s";
  829. //const MYDATETIMEZONE = "UTC";
  830. const MYDATETIMEZONE = "Europe/Paris";
  831. static protected $const_TIMEZONE = null; // set once for all upon object construct
  832. static public function InitStatics()
  833. {
  834. // Init static constant once for all (remove when PHP allows real static const)
  835. self::$const_TIMEZONE = new DateTimeZone(self::MYDATETIMEZONE);
  836. // #@# Init default timezone -> do not get a notice... to be improved !!!
  837. // duplicated in the email test page (the mail function does trigger a notice as well)
  838. date_default_timezone_set(self::MYDATETIMEZONE);
  839. }
  840. static protected function ListExpectedParams()
  841. {
  842. return parent::ListExpectedParams();
  843. //return array_merge(parent::ListExpectedParams(), array());
  844. }
  845. public function GetType() {return "Date";}
  846. public function GetTypeDesc() {return "Date and time";}
  847. public function GetEditClass() {return "Date";}
  848. protected function GetSQLCol() {return "TIMESTAMP";}
  849. // #@# THIS HAS TO REVISED
  850. // Having null not allowed was interpreted by mySQL
  851. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  852. // Then, on each update of the record, the field was modified.
  853. // We will have to specify the default value if we want to restore this option
  854. // In fact, we could also have more verbs dedicated to the DB:
  855. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  856. public function IsNullAllowed() {return true;}
  857. public function GetDefaultValue()
  858. {
  859. $default = parent::GetDefaultValue();
  860. if (!parent::IsNullAllowed())
  861. {
  862. if (empty($default))
  863. {
  864. $default = date("Y-m-d H:i");
  865. }
  866. }
  867. return $default;
  868. }
  869. // END OF THE WORKAROUND
  870. ///////////////////////////////////////////////////////////////
  871. public function GetBasicFilterOperators()
  872. {
  873. return array(
  874. "="=>"equals",
  875. "!="=>"differs from",
  876. "<"=>"before",
  877. "<="=>"before",
  878. ">"=>"after (strictly)",
  879. ">="=>"after",
  880. "SameDay"=>"same day (strip time)",
  881. "SameMonth"=>"same year/month",
  882. "SameYear"=>"same year",
  883. "Today"=>"today",
  884. ">|"=>"after today + N days",
  885. "<|"=>"before today + N days",
  886. "=|"=>"equals today + N days",
  887. );
  888. }
  889. public function GetBasicFilterLooseOperator()
  890. {
  891. // Unless we implement a "same xxx, depending on given precision" !
  892. return "=";
  893. }
  894. public function GetBasicFilterSQLExpr($sOpCode, $value)
  895. {
  896. $sQValue = CMDBSource::Quote($value);
  897. switch ($sOpCode)
  898. {
  899. case '=':
  900. case '!=':
  901. case '<':
  902. case '<=':
  903. case '>':
  904. case '>=':
  905. return $this->GetSQLExpr()." $sOpCode $sQValue";
  906. case 'SameDay':
  907. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  908. case 'SameMonth':
  909. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  910. case 'SameYear':
  911. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  912. case 'Today':
  913. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  914. case '>|':
  915. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  916. case '<|':
  917. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  918. case '=|':
  919. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  920. default:
  921. return $this->GetSQLExpr()." = $sQValue";
  922. }
  923. }
  924. public function MakeRealValue($proposedValue)
  925. {
  926. if (!is_numeric($proposedValue))
  927. {
  928. return $proposedValue;
  929. }
  930. else
  931. {
  932. return date("Y-m-d H:i:s", $proposedValue);
  933. }
  934. throw new CoreException("Invalid type for a date (found ".gettype($proposedValue)." and accepting string/int/DateTime)");
  935. return null;
  936. }
  937. public function ScalarToSQL($value)
  938. {
  939. if (empty($value))
  940. {
  941. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  942. return '0000-00-00 00:00:00';
  943. }
  944. return $value;
  945. }
  946. public function GetAsHTML($value)
  947. {
  948. return Str::pure2html($value);
  949. }
  950. public function GetAsXML($value)
  951. {
  952. return Str::pure2xml($value);
  953. }
  954. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  955. {
  956. $sFrom = array("\r\n", $sTextQualifier);
  957. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  958. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  959. return '"'.$sEscaped.'"';
  960. }
  961. }
  962. // Init static constant once for all (remove when PHP allows real static const)
  963. AttributeDate::InitStatics();
  964. /**
  965. * Map a foreign key to an attribute
  966. * AttributeExternalKey and AttributeExternalField may be an external key
  967. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  968. * where an AttributeExternalField corresponds to a column into another table (class)
  969. *
  970. * @package iTopORM
  971. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  972. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  973. * @link www.itop.com
  974. * @since 1.0
  975. * @version $itopversion$
  976. */
  977. class AttributeExternalKey extends AttributeDBFieldVoid
  978. {
  979. static protected function ListExpectedParams()
  980. {
  981. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed", "on_target_delete"));
  982. }
  983. public function GetType() {return "Extkey";}
  984. public function GetTypeDesc() {return "Link to another object";}
  985. public function GetEditClass() {return "ExtKey";}
  986. protected function GetSQLCol() {return "INT(11)";}
  987. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  988. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  989. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  990. public function GetKeyAttCode() {return $this->GetCode();}
  991. public function GetDefaultValue() {return 0;}
  992. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  993. public function GetNullValue() {return 0;}
  994. public function GetBasicFilterOperators()
  995. {
  996. return parent::GetBasicFilterOperators();
  997. }
  998. public function GetBasicFilterLooseOperator()
  999. {
  1000. return parent::GetBasicFilterLooseOperator();
  1001. }
  1002. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1003. {
  1004. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  1005. }
  1006. // overloaded here so that an ext key always have the answer to
  1007. // "what are your possible values?"
  1008. public function GetValuesDef()
  1009. {
  1010. $oValSetDef = $this->Get("allowed_values");
  1011. if (!$oValSetDef)
  1012. {
  1013. // Let's propose every existing value
  1014. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1015. }
  1016. return $oValSetDef;
  1017. }
  1018. public function GetAllowedValues($aArgs = array(), $sBeginsWith = '')
  1019. {
  1020. try
  1021. {
  1022. return parent::GetAllowedValues($aArgs, $sBeginsWith);
  1023. }
  1024. catch (MissingQueryArgument $e)
  1025. {
  1026. // Some required arguments could not be found, enlarge to any existing value
  1027. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1028. return $oValSetDef->GetValues($aArgs, $sBeginsWith);
  1029. }
  1030. }
  1031. public function GetDeletionPropagationOption()
  1032. {
  1033. return $this->Get("on_target_delete");
  1034. }
  1035. public function MakeRealValue($proposedValue)
  1036. {
  1037. return (int)$proposedValue;
  1038. }
  1039. }
  1040. /**
  1041. * An attribute which corresponds to an external key (direct or indirect)
  1042. *
  1043. * @package iTopORM
  1044. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  1045. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1046. * @link www.itop.com
  1047. * @since 1.0
  1048. * @version $itopversion$
  1049. */
  1050. class AttributeExternalField extends AttributeDefinition
  1051. {
  1052. static protected function ListExpectedParams()
  1053. {
  1054. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  1055. }
  1056. public function GetType() {return "ExtkeyField";}
  1057. public function GetTypeDesc() {return "Field of an object pointed to by the current object";}
  1058. public function GetEditClass() {return "ExtField";}
  1059. protected function GetSQLCol()
  1060. {
  1061. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  1062. $oExtAttDef = $this->GetExtAttDef();
  1063. return $oExtAttDef->GetSQLCol();
  1064. }
  1065. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  1066. {
  1067. switch($iType)
  1068. {
  1069. case EXTKEY_ABSOLUTE:
  1070. // see further
  1071. $oRemoteAtt = $this->GetExtAttDef();
  1072. return $oRemoteAtt->IsExternalKey($iType);
  1073. case EXTKEY_RELATIVE:
  1074. return false;
  1075. default:
  1076. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1077. }
  1078. }
  1079. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  1080. {
  1081. return $this->GetKeyAttDef($iType)->GetTargetClass();
  1082. }
  1083. public function IsExternalField() {return true;}
  1084. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  1085. public function GetExtAttCode() {return $this->Get("target_attcode");}
  1086. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  1087. {
  1088. switch($iType)
  1089. {
  1090. case EXTKEY_ABSOLUTE:
  1091. // see further
  1092. $oRemoteAtt = $this->GetExtAttDef();
  1093. if ($oRemoteAtt->IsExternalField())
  1094. {
  1095. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  1096. }
  1097. else if ($oRemoteAtt->IsExternalKey())
  1098. {
  1099. return $oRemoteAtt;
  1100. }
  1101. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  1102. case EXTKEY_RELATIVE:
  1103. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  1104. default:
  1105. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1106. }
  1107. }
  1108. public function GetExtAttDef()
  1109. {
  1110. $oKeyAttDef = $this->GetKeyAttDef();
  1111. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  1112. if (!is_object($oExtAttDef)) throw new CoreException("Invalid external field ".$this->GetCode()." in class ".$this->GetHostClass().". The class ".$oKeyAttDef->Get("targetclass")." has no attribute ".$this->Get("target_attcode"));
  1113. return $oExtAttDef;
  1114. }
  1115. public function GetSQLExpr()
  1116. {
  1117. $oExtAttDef = $this->GetExtAttDef();
  1118. return $oExtAttDef->GetSQLExpr();
  1119. }
  1120. public function GetDefaultValue()
  1121. {
  1122. $oExtAttDef = $this->GetExtAttDef();
  1123. return $oExtAttDef->GetDefaultValue();
  1124. }
  1125. public function IsNullAllowed()
  1126. {
  1127. $oExtAttDef = $this->GetExtAttDef();
  1128. return $oExtAttDef->IsNullAllowed();
  1129. }
  1130. public function IsScalar()
  1131. {
  1132. $oExtAttDef = $this->GetExtAttDef();
  1133. return $oExtAttDef->IsScalar();
  1134. }
  1135. public function GetBasicFilterOperators()
  1136. {
  1137. $oExtAttDef = $this->GetExtAttDef();
  1138. return $oExtAttDef->GetBasicFilterOperators();
  1139. }
  1140. public function GetBasicFilterLooseOperator()
  1141. {
  1142. $oExtAttDef = $this->GetExtAttDef();
  1143. return $oExtAttDef->GetBasicFilterLooseOperator();
  1144. }
  1145. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1146. {
  1147. $oExtAttDef = $this->GetExtAttDef();
  1148. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  1149. }
  1150. public function MakeRealValue($proposedValue)
  1151. {
  1152. $oExtAttDef = $this->GetExtAttDef();
  1153. return $oExtAttDef->MakeRealValue($proposedValue);
  1154. }
  1155. public function ScalarToSQL($value)
  1156. {
  1157. // This one could be used in case of filtering only
  1158. $oExtAttDef = $this->GetExtAttDef();
  1159. return $oExtAttDef->ScalarToSQL($value);
  1160. }
  1161. // Do not overload GetSQLExpression here because this is handled in the joins
  1162. //public function GetSQLExpressions() {return array();}
  1163. // Here, we get the data...
  1164. public function FromSQLToValue($aCols, $sPrefix = '')
  1165. {
  1166. $oExtAttDef = $this->GetExtAttDef();
  1167. return $oExtAttDef->FromSQLToValue($aCols, $sPrefix);
  1168. }
  1169. public function GetAsHTML($value)
  1170. {
  1171. $oExtAttDef = $this->GetExtAttDef();
  1172. return $oExtAttDef->GetAsHTML($value);
  1173. }
  1174. public function GetAsXML($value)
  1175. {
  1176. $oExtAttDef = $this->GetExtAttDef();
  1177. return $oExtAttDef->GetAsXML($value);
  1178. }
  1179. public function GetAsCSV($value, $sSeparator = ',', $sTestQualifier = '"')
  1180. {
  1181. $oExtAttDef = $this->GetExtAttDef();
  1182. return $oExtAttDef->GetAsCSV($value, $sSeparator, $sTestQualifier);
  1183. }
  1184. }
  1185. /**
  1186. * Map a varchar column to an URL (formats the ouput in HMTL)
  1187. *
  1188. * @package iTopORM
  1189. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  1190. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1191. * @link www.itop.com
  1192. * @since 1.0
  1193. * @version $itopversion$
  1194. */
  1195. class AttributeURL extends AttributeString
  1196. {
  1197. static protected function ListExpectedParams()
  1198. {
  1199. //return parent::ListExpectedParams();
  1200. return array_merge(parent::ListExpectedParams(), array("target"));
  1201. }
  1202. public function GetType() {return "Url";}
  1203. public function GetTypeDesc() {return "Absolute or relative URL as a text string";}
  1204. public function GetEditClass() {return "String";}
  1205. public function GetAsHTML($sValue)
  1206. {
  1207. $sTarget = $this->Get("target");
  1208. if (empty($sTarget)) $sTarget = "_blank";
  1209. $sLabel = Str::pure2html($sValue);
  1210. if (strlen($sLabel) > 40)
  1211. {
  1212. // Truncate the length to about 40 characters, by removing the middle
  1213. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  1214. }
  1215. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  1216. }
  1217. }
  1218. /**
  1219. * Data column, consisting in TWO columns in the DB
  1220. *
  1221. * @package iTopORM
  1222. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  1223. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  1224. * @link www.itop.com
  1225. * @since 1.0
  1226. * @version $itopversion$
  1227. */
  1228. class AttributeBlob extends AttributeDefinition
  1229. {
  1230. static protected function ListExpectedParams()
  1231. {
  1232. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  1233. }
  1234. public function GetType() {return "Blob";}
  1235. public function GetTypeDesc() {return "Document";}
  1236. public function GetEditClass() {return "Document";}
  1237. public function IsDirectField() {return true;}
  1238. public function IsScalar() {return true;}
  1239. public function IsWritable() {return true;}
  1240. public function GetDefaultValue() {return "";}
  1241. public function IsNullAllowed() {return false;}
  1242. // Facilitate things: allow the user to Set the value from a string
  1243. public function MakeRealValue($proposedValue)
  1244. {
  1245. if (!is_object($proposedValue))
  1246. {
  1247. return new ormDocument($proposedValue, 'text/plain');
  1248. }
  1249. return $proposedValue;
  1250. }
  1251. public function GetSQLExpressions()
  1252. {
  1253. $aColumns = array();
  1254. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  1255. $aColumns[''] = $this->GetCode().'_mimetype';
  1256. $aColumns['_data'] = $this->GetCode().'_data';
  1257. $aColumns['_filename'] = $this->GetCode().'_filename';
  1258. return $aColumns;
  1259. }
  1260. public function FromSQLToValue($aCols, $sPrefix = '')
  1261. {
  1262. if (!isset($aCols[$sPrefix]))
  1263. {
  1264. $sAvailable = implode(', ', array_keys($aCols));
  1265. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  1266. }
  1267. $sMimeType = $aCols[$sPrefix];
  1268. if (!isset($aCols[$sPrefix.'_data']))
  1269. {
  1270. $sAvailable = implode(', ', array_keys($aCols));
  1271. throw new MissingColumnException("Missing column '".$sPrefix."_data' from {$sAvailable}");
  1272. }
  1273. $data = $aCols[$sPrefix.'_data'];
  1274. if (!isset($aCols[$sPrefix.'_filename']))
  1275. {
  1276. $sAvailable = implode(', ', array_keys($aCols));
  1277. throw new MissingColumnException("Missing column '".$sPrefix."_filename' from {$sAvailable}");
  1278. }
  1279. $sFileName = $aCols[$sPrefix.'_filename'];
  1280. $value = new ormDocument($data, $sMimeType, $sFileName);
  1281. return $value;
  1282. }
  1283. public function GetSQLValues($value)
  1284. {
  1285. // #@# Optimization: do not load blobs anytime
  1286. // As per mySQL doc, selecting blob columns will prevent mySQL from
  1287. // using memory in case a temporary table has to be created
  1288. // (temporary tables created on disk)
  1289. // We will have to remove the blobs from the list of attributes when doing the select
  1290. // then the use of Get() should finalize the load
  1291. $aValues = array();
  1292. $aValues[$this->GetCode().'_data'] = $value->GetData();
  1293. $aValues[$this->GetCode().'_mimetype'] = $value->GetMimeType();
  1294. $aValues[$this->GetCode().'_filename'] = $value->GetFileName();
  1295. return $aValues;
  1296. }
  1297. public function GetSQLColumns()
  1298. {
  1299. $aColumns = array();
  1300. $aColumns[$this->GetCode().'_data'] = 'LONGBLOB'; // 2^32 (4 Gb)
  1301. $aColumns[$this->GetCode().'_mimetype'] = 'VARCHAR(255)';
  1302. $aColumns[$this->GetCode().'_filename'] = 'VARCHAR(255)';
  1303. return $aColumns;
  1304. }
  1305. public function GetBasicFilterOperators()
  1306. {
  1307. return array();
  1308. }
  1309. public function GetBasicFilterLooseOperator()
  1310. {
  1311. return '=';
  1312. }
  1313. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1314. {
  1315. return 'true';
  1316. }
  1317. public function GetAsHTML($value)
  1318. {
  1319. if (is_object($value))
  1320. {
  1321. return $value->GetAsHTML();
  1322. }
  1323. }
  1324. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  1325. {
  1326. return ''; // Not exportable in CSV !
  1327. }
  1328. public function GetAsXML($value)
  1329. {
  1330. return ''; // Not exportable in XML, or as CDATA + some subtags ??
  1331. }
  1332. }
  1333. ?>