attributedef.class.inc.php 41 KB

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