attributedef.class.inc.php 42 KB

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