attributedef.class.inc.php 41 KB

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