attributedef.class.inc.php 43 KB

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