attributedef.class.inc.php 42 KB

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