attributedef.class.inc.php 42 KB

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