attributedef.class.inc.php 47 KB

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