attributedef.class.inc.php 42 KB

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