attributedef.class.inc.php 41 KB

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