attributedef.class.inc.php 47 KB

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