attributedef.class.inc.php 40 KB

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