attributedef.class.inc.php 41 KB

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