attributedef.class.inc.php 41 KB

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