attributedef.class.inc.php 40 KB

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