attributedef.class.inc.php 42 KB

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