attributedef.class.inc.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Typology for the attributes
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('MyHelpers.class.inc.php');
  25. require_once('ormdocument.class.inc.php');
  26. require_once('ormpassword.class.inc.php');
  27. /**
  28. * MissingColumnException - sent if an attribute is being created but the column is missing in the row
  29. *
  30. * @package iTopORM
  31. */
  32. class MissingColumnException extends Exception
  33. {}
  34. /**
  35. * add some description here...
  36. *
  37. * @package iTopORM
  38. */
  39. define('EXTKEY_RELATIVE', 1);
  40. /**
  41. * add some description here...
  42. *
  43. * @package iTopORM
  44. */
  45. define('EXTKEY_ABSOLUTE', 2);
  46. /**
  47. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  48. *
  49. * @package iTopORM
  50. */
  51. define('DEL_MANUAL', 1);
  52. /**
  53. * Propagation of the deletion through an external key - ask the user to delete the referencing object
  54. *
  55. * @package iTopORM
  56. */
  57. define('DEL_AUTO', 2);
  58. /**
  59. * Attribute definition API, implemented in and many flavours (Int, String, Enum, etc.)
  60. *
  61. * @package iTopORM
  62. */
  63. abstract class AttributeDefinition
  64. {
  65. public function GetType()
  66. {
  67. return Dict::S('Core:'.get_class($this));
  68. }
  69. public function GetTypeDesc()
  70. {
  71. return Dict::S('Core:'.get_class($this).'+');
  72. }
  73. abstract public function GetEditClass();
  74. protected $m_sCode;
  75. private $m_aParams = array();
  76. protected $m_sHostClass = '!undefined!';
  77. protected function Get($sParamName) {return $this->m_aParams[$sParamName];}
  78. protected function IsParam($sParamName) {return (array_key_exists($sParamName, $this->m_aParams));}
  79. protected function GetOptional($sParamName, $default)
  80. {
  81. if (array_key_exists($sParamName, $this->m_aParams))
  82. {
  83. return $this->m_aParams[$sParamName];
  84. }
  85. else
  86. {
  87. return $default;
  88. }
  89. }
  90. public function __construct($sCode, $aParams)
  91. {
  92. $this->m_sCode = $sCode;
  93. $this->m_aParams = $aParams;
  94. $this->ConsistencyCheck();
  95. }
  96. public function OverloadParams($aParams)
  97. {
  98. foreach ($aParams as $sParam => $value)
  99. {
  100. if (!array_key_exists($sParam, $this->m_aParams))
  101. {
  102. throw new CoreException("Unknown attribute definition parameter '$sParam', please select a value in {".implode(", ", array_keys($this->m_aParams))."}");
  103. }
  104. else
  105. {
  106. $this->m_aParams[$sParam] = $value;
  107. }
  108. }
  109. }
  110. public function SetHostClass($sHostClass)
  111. {
  112. $this->m_sHostClass = $sHostClass;
  113. }
  114. public function GetHostClass()
  115. {
  116. return $this->m_sHostClass;
  117. }
  118. // Note: I could factorize this code with the parameter management made for the AttributeDef class
  119. // to be overloaded
  120. static protected function ListExpectedParams()
  121. {
  122. return array();
  123. }
  124. private function ConsistencyCheck()
  125. {
  126. // Check that any mandatory param has been specified
  127. //
  128. $aExpectedParams = $this->ListExpectedParams();
  129. foreach($aExpectedParams as $sParamName)
  130. {
  131. if (!array_key_exists($sParamName, $this->m_aParams))
  132. {
  133. $aBacktrace = debug_backtrace();
  134. $sTargetClass = $aBacktrace[2]["class"];
  135. $sCodeInfo = $aBacktrace[1]["file"]." - ".$aBacktrace[1]["line"];
  136. throw new Exception("ERROR missing parameter '$sParamName' in ".get_class($this)." declaration for class $sTargetClass ($sCodeInfo)");
  137. }
  138. }
  139. }
  140. // table, key field, name field
  141. public function ListDBJoins()
  142. {
  143. return "";
  144. // e.g: return array("Site", "infrid", "name");
  145. }
  146. public function IsDirectField() {return false;}
  147. public function IsScalar() {return false;}
  148. public function IsLinkSet() {return false;}
  149. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return false;}
  150. public function IsExternalField() {return false;}
  151. public function IsWritable() {return false;}
  152. public function IsNullAllowed() {return true;}
  153. public function GetCode() {return $this->m_sCode;}
  154. public function GetLabel() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $this->m_sCode);}
  155. public function GetLabel_Obsolete()
  156. {
  157. // Written for compatibility with a data model written prior to version 0.9.1
  158. if (array_key_exists('label', $this->m_aParams))
  159. {
  160. return $this->m_aParams['label'];
  161. }
  162. else
  163. {
  164. return $this->GetLabel();
  165. }
  166. }
  167. public function GetDescription() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', '');}
  168. public function GetHelpOnEdition() {return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'?', '');}
  169. public function GetDescription_Obsolete()
  170. {
  171. // Written for compatibility with a data model written prior to version 0.9.1
  172. if (array_key_exists('description', $this->m_aParams))
  173. {
  174. return $this->m_aParams['description'];
  175. }
  176. else
  177. {
  178. return $this->GetDescription();
  179. }
  180. }
  181. public function GetValuesDef() {return null;}
  182. public function GetPrerequisiteAttributes() {return array();}
  183. public function GetNullValue() {return null;}
  184. public function IsNull($proposedValue) {return is_null($proposedValue);}
  185. public function MakeRealValue($proposedValue) {return $proposedValue;} // force an allowed value (type conversion and possibly forces a value as mySQL would do upon writing!)
  186. public function GetSQLExpressions($sPrefix = '') {return array();} // returns suffix/expression pairs (1 in most of the cases), for READING (Select)
  187. public function FromSQLToValue($aCols, $sPrefix = '') {return null;} // returns a value out of suffix/value pairs, for SELECT result interpretation
  188. public function GetSQLColumns() {return array();} // returns column/spec pairs (1 in most of the cases), for STRUCTURING (DB creation)
  189. public function GetSQLValues($value) {return array();} // returns column/value pairs (1 in most of the cases), for WRITING (Insert, Update)
  190. public function RequiresIndex() {return false;}
  191. // Import - differs slightly from SQL input, but identical in most cases
  192. //
  193. public function GetImportColumns() {return $this->GetSQLColumns();}
  194. public function FromImportToValue($aCols, $sPrefix = '')
  195. {
  196. $aValues = array();
  197. foreach ($this->GetSQLExpressions($sPrefix) as $sAlias => $sExpr)
  198. {
  199. // This is working, based on the assumption that importable fields
  200. // are not computed fields => the expression is the name of a column
  201. $aValues[$sPrefix.$sAlias] = $aCols[$sExpr];
  202. }
  203. return $this->FromSQLToValue($aValues, $sPrefix);
  204. }
  205. public function GetValidationPattern()
  206. {
  207. return '';
  208. }
  209. public function CheckFormat($value)
  210. {
  211. return true;
  212. }
  213. public function GetMaxSize()
  214. {
  215. return null;
  216. }
  217. public function MakeValue()
  218. {
  219. $sComputeFunc = $this->Get("compute_func");
  220. if (empty($sComputeFunc)) return null;
  221. return call_user_func($sComputeFunc);
  222. }
  223. abstract public function GetDefaultValue();
  224. //
  225. // To be overloaded in subclasses
  226. //
  227. abstract public function GetBasicFilterOperators(); // returns an array of "opCode"=>"description"
  228. abstract public function GetBasicFilterLooseOperator(); // returns an "opCode"
  229. //abstract protected GetBasicFilterHTMLInput();
  230. abstract public function GetBasicFilterSQLExpr($sOpCode, $value);
  231. public function GetFilterDefinitions()
  232. {
  233. return array();
  234. }
  235. public function GetEditValue($sValue)
  236. {
  237. return (string)$sValue;
  238. }
  239. public function GetAsHTML($sValue)
  240. {
  241. return Str::pure2html((string)$sValue);
  242. }
  243. public function GetAsXML($sValue)
  244. {
  245. return Str::pure2xml((string)$sValue);
  246. }
  247. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  248. {
  249. return (string)$sValue;
  250. }
  251. public function GetAllowedValues($aArgs = array(), $sContains = '')
  252. {
  253. $oValSetDef = $this->GetValuesDef();
  254. if (!$oValSetDef) return null;
  255. return $oValSetDef->GetValues($aArgs, $sContains);
  256. }
  257. /**
  258. * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
  259. * Each derived class is reponsible for defining and processing their own smart patterns, the base class
  260. * does nothing special, and just calls the default (loose) operator
  261. * @param string $sSearchText The search string to analyze for smart patterns
  262. * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
  263. * @param Hash $aParams Values of the query parameters
  264. * @return Expression The search condition to be added (AND) to the current search
  265. */
  266. public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
  267. {
  268. $sParamName = $oField->GetParent().'_'.$oField->GetName();
  269. $oRightExpr = new VariableExpression($sParamName);
  270. $sOperator = $this->GetBasicFilterLooseOperator();
  271. switch ($sOperator)
  272. {
  273. case 'Contains':
  274. $aParams[$sParamName] = "%$sSearchText%";
  275. $sSQLOperator = 'LIKE';
  276. break;
  277. default:
  278. $sSQLOperator = $sOperator;
  279. $aParams[$sParamName] = $sSearchText;
  280. }
  281. $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
  282. return $oNewCondition;
  283. }
  284. }
  285. /**
  286. * Set of objects directly linked to an object, and being part of its definition
  287. *
  288. * @package iTopORM
  289. */
  290. class AttributeLinkedSet extends AttributeDefinition
  291. {
  292. static protected function ListExpectedParams()
  293. {
  294. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "linked_class", "ext_key_to_me", "count_min", "count_max"));
  295. }
  296. public function GetEditClass() {return "List";}
  297. public function IsWritable() {return true;}
  298. public function IsLinkSet() {return true;}
  299. public function IsIndirect() {return false;}
  300. public function GetValuesDef() {return $this->Get("allowed_values");}
  301. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  302. public function GetDefaultValue($aArgs = array())
  303. {
  304. // Note: so far, this feature is a prototype,
  305. // later, the argument 'this' should always be present in the arguments
  306. //
  307. if (($this->IsParam('default_value')) && array_key_exists('this', $aArgs))
  308. {
  309. $aValues = $this->Get('default_value')->GetValues($aArgs);
  310. $oSet = DBObjectSet::FromArray($this->Get('linked_class'), $aValues);
  311. return $oSet;
  312. }
  313. else
  314. {
  315. return DBObjectSet::FromScratch($this->Get('linked_class'));
  316. }
  317. }
  318. public function GetLinkedClass() {return $this->Get('linked_class');}
  319. public function GetExtKeyToMe() {return $this->Get('ext_key_to_me');}
  320. public function GetBasicFilterOperators() {return array();}
  321. public function GetBasicFilterLooseOperator() {return '';}
  322. public function GetBasicFilterSQLExpr($sOpCode, $value) {return '';}
  323. public function GetAsHTML($sValue)
  324. {
  325. return "ERROR: LIST OF OBJECTS";
  326. }
  327. public function GetAsXML($sValue)
  328. {
  329. return "ERROR: LIST OF OBJECTS";
  330. }
  331. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  332. {
  333. return "ERROR: LIST OF OBJECTS";
  334. }
  335. public function DuplicatesAllowed() {return false;} // No duplicates for 1:n links, never
  336. }
  337. /**
  338. * Set of objects linked to an object (n-n), and being part of its definition
  339. *
  340. * @package iTopORM
  341. */
  342. class AttributeLinkedSetIndirect extends AttributeLinkedSet
  343. {
  344. static protected function ListExpectedParams()
  345. {
  346. return array_merge(parent::ListExpectedParams(), array("ext_key_to_remote"));
  347. }
  348. public function IsIndirect() {return true;}
  349. public function GetExtKeyToRemote() { return $this->Get('ext_key_to_remote'); }
  350. public function GetEditClass() {return "LinkedSet";}
  351. public function DuplicatesAllowed() {return $this->GetOptional("duplicates", false);} // The same object may be linked several times... or not...
  352. }
  353. /**
  354. * Abstract class implementing default filters for a DB column
  355. *
  356. * @package iTopORM
  357. */
  358. class AttributeDBFieldVoid extends AttributeDefinition
  359. {
  360. static protected function ListExpectedParams()
  361. {
  362. return array_merge(parent::ListExpectedParams(), array("allowed_values", "depends_on", "sql"));
  363. }
  364. // To be overriden, used in GetSQLColumns
  365. protected function GetSQLCol() {return "VARCHAR(255)";}
  366. public function GetEditClass() {return "String";}
  367. public function GetValuesDef() {return $this->Get("allowed_values");}
  368. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  369. public function IsDirectField() {return true;}
  370. public function IsScalar() {return true;}
  371. public function IsWritable() {return true;}
  372. public function GetSQLExpr() {return $this->Get("sql");}
  373. public function GetDefaultValue() {return $this->MakeRealValue("");}
  374. public function IsNullAllowed() {return false;}
  375. //
  376. protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  377. public function GetSQLExpressions($sPrefix = '')
  378. {
  379. $aColumns = array();
  380. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  381. $aColumns[''] = $this->Get("sql");
  382. return $aColumns;
  383. }
  384. public function FromSQLToValue($aCols, $sPrefix = '')
  385. {
  386. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  387. return $value;
  388. }
  389. public function GetSQLValues($value)
  390. {
  391. $aValues = array();
  392. $aValues[$this->Get("sql")] = $this->ScalarToSQL($value);
  393. return $aValues;
  394. }
  395. public function GetSQLColumns()
  396. {
  397. $aColumns = array();
  398. $aColumns[$this->Get("sql")] = $this->GetSQLCol();
  399. return $aColumns;
  400. }
  401. public function GetFilterDefinitions()
  402. {
  403. return array($this->GetCode() => new FilterFromAttribute($this));
  404. }
  405. public function GetBasicFilterOperators()
  406. {
  407. return array("="=>"equals", "!="=>"differs from");
  408. }
  409. public function GetBasicFilterLooseOperator()
  410. {
  411. return "=";
  412. }
  413. public function GetBasicFilterSQLExpr($sOpCode, $value)
  414. {
  415. $sQValue = CMDBSource::Quote($value);
  416. switch ($sOpCode)
  417. {
  418. case '!=':
  419. return $this->GetSQLExpr()." != $sQValue";
  420. break;
  421. case '=':
  422. default:
  423. return $this->GetSQLExpr()." = $sQValue";
  424. }
  425. }
  426. }
  427. /**
  428. * Base class for all kind of DB attributes, with the exception of external keys
  429. *
  430. * @package iTopORM
  431. */
  432. class AttributeDBField extends AttributeDBFieldVoid
  433. {
  434. static protected function ListExpectedParams()
  435. {
  436. return array_merge(parent::ListExpectedParams(), array("default_value", "is_null_allowed"));
  437. }
  438. public function GetDefaultValue() {return $this->MakeRealValue($this->Get("default_value"));}
  439. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  440. }
  441. /**
  442. * Map an integer column to an attribute
  443. *
  444. * @package iTopORM
  445. */
  446. class AttributeInteger extends AttributeDBField
  447. {
  448. static protected function ListExpectedParams()
  449. {
  450. return parent::ListExpectedParams();
  451. //return array_merge(parent::ListExpectedParams(), array());
  452. }
  453. public function GetEditClass() {return "String";}
  454. protected function GetSQLCol() {return "INT(11)";}
  455. public function GetValidationPattern()
  456. {
  457. return "^[0-9]+$";
  458. }
  459. public function GetBasicFilterOperators()
  460. {
  461. return array(
  462. "!="=>"differs from",
  463. "="=>"equals",
  464. ">"=>"greater (strict) than",
  465. ">="=>"greater than",
  466. "<"=>"less (strict) than",
  467. "<="=>"less than",
  468. "in"=>"in"
  469. );
  470. }
  471. public function GetBasicFilterLooseOperator()
  472. {
  473. // Unless we implement an "equals approximately..." or "same order of magnitude"
  474. return "=";
  475. }
  476. public function GetBasicFilterSQLExpr($sOpCode, $value)
  477. {
  478. $sQValue = CMDBSource::Quote($value);
  479. switch ($sOpCode)
  480. {
  481. case '!=':
  482. return $this->GetSQLExpr()." != $sQValue";
  483. break;
  484. case '>':
  485. return $this->GetSQLExpr()." > $sQValue";
  486. break;
  487. case '>=':
  488. return $this->GetSQLExpr()." >= $sQValue";
  489. break;
  490. case '<':
  491. return $this->GetSQLExpr()." < $sQValue";
  492. break;
  493. case '<=':
  494. return $this->GetSQLExpr()." <= $sQValue";
  495. break;
  496. case 'in':
  497. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  498. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  499. break;
  500. case '=':
  501. default:
  502. return $this->GetSQLExpr()." = \"$value\"";
  503. }
  504. }
  505. public function GetNullValue()
  506. {
  507. return null;
  508. }
  509. public function IsNull($proposedValue)
  510. {
  511. return is_null($proposedValue);
  512. }
  513. public function MakeRealValue($proposedValue)
  514. {
  515. if (is_null($proposedValue)) return null;
  516. if ($proposedValue === '') return null; // 0 is transformed into '' !
  517. return (int)$proposedValue;
  518. }
  519. public function ScalarToSQL($value)
  520. {
  521. assert(is_numeric($value) || is_null($value));
  522. return $value; // supposed to be an int
  523. }
  524. }
  525. /**
  526. * Map a decimal value column (suitable for financial computations) to an attribute
  527. * internally in PHP such numbers are represented as string. Should you want to perform
  528. * a calculation on them, it is recommended to use the BC Math functions in order to
  529. * retain the precision
  530. *
  531. * @package iTopORM
  532. */
  533. class AttributeDecimal extends AttributeDBField
  534. {
  535. static protected function ListExpectedParams()
  536. {
  537. return array_merge(parent::ListExpectedParams(), array('digits', 'decimals' /* including precision */));
  538. }
  539. public function GetEditClass() {return "String";}
  540. protected function GetSQLCol() {return "DECIMAL(".$this->Get('digits').",".$this->Get('decimals').")";}
  541. public function GetValidationPattern()
  542. {
  543. $iNbDigits = $this->Get('digits');
  544. $iPrecision = $this->Get('decimals');
  545. $iNbIntegerDigits = $iNbDigits - $iPrecision - 1; // -1 because the first digit is treated separately in the pattern below
  546. return "^[-+]?[0-9]\d{0,$iNbIntegerDigits}(\.\d{0,$iPrecision})?$";
  547. }
  548. public function GetBasicFilterOperators()
  549. {
  550. return array(
  551. "!="=>"differs from",
  552. "="=>"equals",
  553. ">"=>"greater (strict) than",
  554. ">="=>"greater than",
  555. "<"=>"less (strict) than",
  556. "<="=>"less than",
  557. "in"=>"in"
  558. );
  559. }
  560. public function GetBasicFilterLooseOperator()
  561. {
  562. // Unless we implement an "equals approximately..." or "same order of magnitude"
  563. return "=";
  564. }
  565. public function GetBasicFilterSQLExpr($sOpCode, $value)
  566. {
  567. $sQValue = CMDBSource::Quote($value);
  568. switch ($sOpCode)
  569. {
  570. case '!=':
  571. return $this->GetSQLExpr()." != $sQValue";
  572. break;
  573. case '>':
  574. return $this->GetSQLExpr()." > $sQValue";
  575. break;
  576. case '>=':
  577. return $this->GetSQLExpr()." >= $sQValue";
  578. break;
  579. case '<':
  580. return $this->GetSQLExpr()." < $sQValue";
  581. break;
  582. case '<=':
  583. return $this->GetSQLExpr()." <= $sQValue";
  584. break;
  585. case 'in':
  586. if (!is_array($value)) throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')");
  587. return $this->GetSQLExpr()." IN ('".implode("', '", $value)."')";
  588. break;
  589. case '=':
  590. default:
  591. return $this->GetSQLExpr()." = \"$value\"";
  592. }
  593. }
  594. public function GetNullValue()
  595. {
  596. return null;
  597. }
  598. public function IsNull($proposedValue)
  599. {
  600. return is_null($proposedValue);
  601. }
  602. public function MakeRealValue($proposedValue)
  603. {
  604. if (is_null($proposedValue)) return null;
  605. if ($proposedValue == '') return null;
  606. return (string)$proposedValue;
  607. }
  608. public function ScalarToSQL($value)
  609. {
  610. assert(is_null($value) || preg_match('/'.$this->GetValidationPattern().'/', $value));
  611. return (string)$value; // treated as a string
  612. }
  613. }
  614. /**
  615. * Map a boolean column to an attribute
  616. *
  617. * @package iTopORM
  618. */
  619. class AttributeBoolean extends AttributeInteger
  620. {
  621. static protected function ListExpectedParams()
  622. {
  623. return parent::ListExpectedParams();
  624. //return array_merge(parent::ListExpectedParams(), array());
  625. }
  626. public function GetEditClass() {return "Integer";}
  627. protected function GetSQLCol() {return "TINYINT(1)";}
  628. public function MakeRealValue($proposedValue)
  629. {
  630. if (is_null($proposedValue)) return null;
  631. if ($proposedValue === '') return null;
  632. if ((int)$proposedValue) return true;
  633. return false;
  634. }
  635. public function ScalarToSQL($value)
  636. {
  637. if ($value) return 1;
  638. return 0;
  639. }
  640. }
  641. /**
  642. * Map a varchar column (size < ?) to an attribute
  643. *
  644. * @package iTopORM
  645. */
  646. class AttributeString extends AttributeDBField
  647. {
  648. static protected function ListExpectedParams()
  649. {
  650. return parent::ListExpectedParams();
  651. //return array_merge(parent::ListExpectedParams(), array());
  652. }
  653. public function GetEditClass() {return "String";}
  654. protected function GetSQLCol() {return "VARCHAR(255)";}
  655. public function CheckFormat($value)
  656. {
  657. $sRegExp = $this->GetValidationPattern();
  658. if (empty($sRegExp))
  659. {
  660. return true;
  661. }
  662. else
  663. {
  664. $sRegExp = str_replace('/', '\\/', $sRegExp);
  665. return preg_match("/$sRegExp/", $value);
  666. }
  667. }
  668. public function GetMaxSize()
  669. {
  670. return 255;
  671. }
  672. public function GetBasicFilterOperators()
  673. {
  674. return array(
  675. "="=>"equals",
  676. "!="=>"differs from",
  677. "Like"=>"equals (no case)",
  678. "NotLike"=>"differs from (no case)",
  679. "Contains"=>"contains",
  680. "Begins with"=>"begins with",
  681. "Finishes with"=>"finishes with"
  682. );
  683. }
  684. public function GetBasicFilterLooseOperator()
  685. {
  686. return "Contains";
  687. }
  688. public function GetBasicFilterSQLExpr($sOpCode, $value)
  689. {
  690. $sQValue = CMDBSource::Quote($value);
  691. switch ($sOpCode)
  692. {
  693. case '=':
  694. case '!=':
  695. return $this->GetSQLExpr()." $sOpCode $sQValue";
  696. case 'Begins with':
  697. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("$value%");
  698. case 'Finishes with':
  699. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value");
  700. case 'Contains':
  701. return $this->GetSQLExpr()." LIKE ".CMDBSource::Quote("%$value%");
  702. case 'NotLike':
  703. return $this->GetSQLExpr()." NOT LIKE $sQValue";
  704. case 'Like':
  705. default:
  706. return $this->GetSQLExpr()." LIKE $sQValue";
  707. }
  708. }
  709. public function GetNullValue()
  710. {
  711. return '';
  712. }
  713. public function IsNull($proposedValue)
  714. {
  715. return ($proposedValue == '');
  716. }
  717. public function MakeRealValue($proposedValue)
  718. {
  719. if (is_null($proposedValue)) return '';
  720. return (string)$proposedValue;
  721. }
  722. public function ScalarToSQL($value)
  723. {
  724. if (!is_string($value) && !is_null($value))
  725. {
  726. throw new CoreWarning('Expected the attribute value to be a string', array('found_type' => gettype($value), 'value' => $value, 'class' => $this->GetCode(), 'attribute' => $this->GetHostClass()));
  727. }
  728. return $value;
  729. }
  730. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  731. {
  732. $sFrom = array("\r\n", $sTextQualifier);
  733. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  734. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  735. return '"'.$sEscaped.'"';
  736. }
  737. }
  738. /**
  739. * An attibute that matches an object class
  740. *
  741. * @package iTopORM
  742. */
  743. class AttributeClass extends AttributeString
  744. {
  745. static protected function ListExpectedParams()
  746. {
  747. return array_merge(parent::ListExpectedParams(), array("class_category", "more_values"));
  748. }
  749. public function __construct($sCode, $aParams)
  750. {
  751. $this->m_sCode = $sCode;
  752. $aParams["allowed_values"] = new ValueSetEnumClasses($aParams['class_category'], $aParams['more_values']);
  753. parent::__construct($sCode, $aParams);
  754. }
  755. public function GetDefaultValue()
  756. {
  757. $sDefault = parent::GetDefaultValue();
  758. if (!$this->IsNullAllowed() && $this->IsNull($sDefault))
  759. {
  760. // For this kind of attribute specifying null as default value
  761. // is authorized even if null is not allowed
  762. // Pick the first one...
  763. $aClasses = $this->GetAllowedValues();
  764. $sDefault = key($aClasses);
  765. }
  766. return $sDefault;
  767. }
  768. public function GetAsHTML($sValue)
  769. {
  770. if (empty($sValue)) return '';
  771. return MetaModel::GetName($sValue);
  772. }
  773. public function RequiresIndex()
  774. {
  775. return true;
  776. }
  777. }
  778. /**
  779. * An attibute that matches one of the language codes availables in the dictionnary
  780. *
  781. * @package iTopORM
  782. */
  783. class AttributeApplicationLanguage extends AttributeString
  784. {
  785. static protected function ListExpectedParams()
  786. {
  787. return parent::ListExpectedParams();
  788. }
  789. public function __construct($sCode, $aParams)
  790. {
  791. $this->m_sCode = $sCode;
  792. $aAvailableLanguages = Dict::GetLanguages();
  793. $aLanguageCodes = array();
  794. foreach($aAvailableLanguages as $sLangCode => $aInfo)
  795. {
  796. $aLanguageCodes[$sLangCode] = $aInfo['description'].' ('.$aInfo['localized_description'].')';
  797. }
  798. $aParams["allowed_values"] = new ValueSetEnum($aLanguageCodes);
  799. parent::__construct($sCode, $aParams);
  800. }
  801. public function RequiresIndex()
  802. {
  803. return true;
  804. }
  805. }
  806. /**
  807. * The attribute dedicated to the finalclass automatic attribute
  808. *
  809. * @package iTopORM
  810. */
  811. class AttributeFinalClass extends AttributeString
  812. {
  813. public function __construct($sCode, $aParams)
  814. {
  815. $this->m_sCode = $sCode;
  816. $aParams["allowed_values"] = null;
  817. parent::__construct($sCode, $aParams);
  818. $this->m_sValue = $this->Get("default_value");
  819. }
  820. public function IsWritable()
  821. {
  822. return false;
  823. }
  824. public function RequiresIndex()
  825. {
  826. return true;
  827. }
  828. public function SetFixedValue($sValue)
  829. {
  830. $this->m_sValue = $sValue;
  831. }
  832. public function GetDefaultValue()
  833. {
  834. return $this->m_sValue;
  835. }
  836. public function GetAsHTML($sValue)
  837. {
  838. if (empty($sValue)) return '';
  839. return MetaModel::GetName($sValue);
  840. }
  841. }
  842. /**
  843. * Map a varchar column (size < ?) to an attribute that must never be shown to the user
  844. *
  845. * @package iTopORM
  846. */
  847. class AttributePassword extends AttributeString
  848. {
  849. static protected function ListExpectedParams()
  850. {
  851. return parent::ListExpectedParams();
  852. //return array_merge(parent::ListExpectedParams(), array());
  853. }
  854. public function GetEditClass() {return "Password";}
  855. protected function GetSQLCol() {return "VARCHAR(64)";}
  856. public function GetMaxSize()
  857. {
  858. return 64;
  859. }
  860. public function GetFilterDefinitions()
  861. {
  862. // Note: due to this, you will get an error if a password is being declared as a search criteria (see ZLists)
  863. // not allowed to search on passwords!
  864. return array();
  865. }
  866. public function GetAsHTML($sValue)
  867. {
  868. if (strlen($sValue) == 0)
  869. {
  870. return '';
  871. }
  872. else
  873. {
  874. return '******';
  875. }
  876. }
  877. }
  878. /**
  879. * Map a text column (size < 255) to an attribute that is encrypted in the database
  880. * The encryption is based on a key set per iTop instance. Thus if you export your
  881. * database (in SQL) to someone else without providing the key at the same time
  882. * the encrypted fields will remain encrypted
  883. *
  884. * @package iTopORM
  885. */
  886. class AttributeEncryptedString extends AttributeString
  887. {
  888. static $sKey = null; // Encryption key used for all encrypted fields
  889. public function __construct($sCode, $aParams)
  890. {
  891. parent::__construct($sCode, $aParams);
  892. if (self::$sKey == null)
  893. {
  894. self::$sKey = MetaModel::GetConfig()->GetEncryptionKey();
  895. }
  896. }
  897. protected function GetSQLCol() {return "TINYBLOB";}
  898. public function GetMaxSize()
  899. {
  900. return 255;
  901. }
  902. public function GetFilterDefinitions()
  903. {
  904. // Note: due to this, you will get an error if a an encrypted field is declared as a search criteria (see ZLists)
  905. // not allowed to search on encrypted fields !
  906. return array();
  907. }
  908. public function MakeRealValue($proposedValue)
  909. {
  910. if (is_null($proposedValue)) return null;
  911. return (string)$proposedValue;
  912. }
  913. /**
  914. * Decrypt the value when reading from the database
  915. */
  916. public function FromSQLToValue($aCols, $sPrefix = '')
  917. {
  918. $oSimpleCrypt = new SimpleCrypt();
  919. $sValue = $oSimpleCrypt->Decrypt(self::$sKey, $aCols[$sPrefix]);
  920. return $sValue;
  921. }
  922. /**
  923. * Encrypt the value before storing it in the database
  924. */
  925. public function GetSQLValues($value)
  926. {
  927. $oSimpleCrypt = new SimpleCrypt();
  928. $encryptedValue = $oSimpleCrypt->Encrypt(self::$sKey, $value);
  929. $aValues = array();
  930. $aValues[$this->Get("sql")] = $encryptedValue;
  931. return $aValues;
  932. }
  933. }
  934. /**
  935. * Map a text column (size > ?) to an attribute
  936. *
  937. * @package iTopORM
  938. */
  939. define('WIKI_OBJECT_REGEXP', '/\[\[(.+):(.+)\]\]/U');
  940. class AttributeText extends AttributeString
  941. {
  942. public function GetEditClass() {return "Text";}
  943. protected function GetSQLCol() {return "TEXT";}
  944. public function GetMaxSize()
  945. {
  946. // Is there a way to know the current limitation for mysql?
  947. // See mysql_field_len()
  948. return 65535;
  949. }
  950. public function GetAsHTML($sValue)
  951. {
  952. $sValue = parent::GetAsHTML($sValue);
  953. if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
  954. {
  955. foreach($aAllMatches as $iPos => $aMatches)
  956. {
  957. $sClass = $aMatches[1];
  958. $sName = $aMatches[2];
  959. if (MetaModel::IsValidClass($sClass))
  960. {
  961. $oObj = MetaModel::GetObjectByName($sClass, $sName, false /* MustBeFound */);
  962. if (is_object($oObj))
  963. {
  964. // Propose a std link to the object
  965. $sValue = str_replace($aMatches[0], $oObj->GetHyperlink(), $sValue);
  966. }
  967. else
  968. {
  969. // Propose a std link to the object
  970. $sClassLabel = MetaModel::GetName($sClass);
  971. $sValue = str_replace($aMatches[0], "<span class=\"wiki_broken_link\">$sClassLabel:$sName</span>", $sValue);
  972. // Later: propose a link to create a new object
  973. // Anyhow... there is no easy way to suggest default values based on the given FRIENDLY name
  974. //$sValue = preg_replace('/\[\[(.+):(.+)\]\]/', '<a href="./UI.php?operation=new&class='.$sClass.'&default[att1]=xxx&default[att2]=yyy">'.$sName.'</a>', $sValue);
  975. }
  976. }
  977. }
  978. }
  979. return str_replace("\n", "<br>\n", $sValue);
  980. }
  981. public function GetEditValue($sValue)
  982. {
  983. if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
  984. {
  985. foreach($aAllMatches as $iPos => $aMatches)
  986. {
  987. $sClass = $aMatches[1];
  988. $sName = $aMatches[2];
  989. if (MetaModel::IsValidClass($sClass))
  990. {
  991. $sClassLabel = MetaModel::GetName($sClass);
  992. $sValue = str_replace($aMatches[0], "[[$sClassLabel:$sName]]", $sValue);
  993. }
  994. }
  995. }
  996. return $sValue;
  997. }
  998. public function MakeRealValue($proposedValue)
  999. {
  1000. $sValue = $proposedValue;
  1001. if (preg_match_all(WIKI_OBJECT_REGEXP, $sValue, $aAllMatches, PREG_SET_ORDER))
  1002. {
  1003. foreach($aAllMatches as $iPos => $aMatches)
  1004. {
  1005. $sClassLabel = $aMatches[1];
  1006. $sName = $aMatches[2];
  1007. if (!MetaModel::IsValidClass($sClassLabel))
  1008. {
  1009. $sClass = MetaModel::GetClassFromLabel($sClassLabel);
  1010. if ($sClass)
  1011. {
  1012. $sValue = str_replace($aMatches[0], "[[$sClass:$sName]]", $sValue);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. return $sValue;
  1018. }
  1019. public function GetAsXML($value)
  1020. {
  1021. return Str::pure2xml($value);
  1022. }
  1023. }
  1024. /**
  1025. * Map a log to an attribute
  1026. *
  1027. * @package iTopORM
  1028. */
  1029. class AttributeLongText extends AttributeText
  1030. {
  1031. protected function GetSQLCol() {return "LONGTEXT";}
  1032. public function GetMaxSize()
  1033. {
  1034. // Is there a way to know the current limitation for mysql?
  1035. // See mysql_field_len()
  1036. return 65535*1024; // Limited... still 64 Mb!
  1037. }
  1038. }
  1039. /**
  1040. * Map a text column (size > ?), containing HTML code, to an attribute
  1041. *
  1042. * @package iTopORM
  1043. */
  1044. class AttributeHTML extends AttributeText
  1045. {
  1046. public function GetEditClass() {return "HTML";}
  1047. public function GetAsHTML($sValue)
  1048. {
  1049. return $sValue;
  1050. }
  1051. }
  1052. /**
  1053. * Specialization of a string: email
  1054. *
  1055. * @package iTopORM
  1056. */
  1057. class AttributeEmailAddress extends AttributeString
  1058. {
  1059. public function GetValidationPattern()
  1060. {
  1061. // return "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
  1062. return "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$";
  1063. }
  1064. public function GetAsHTML($sValue)
  1065. {
  1066. if (empty($sValue)) return '';
  1067. return '<a class="mailto" href="mailto:'.$sValue.'">'.parent::GetAsHTML($sValue).'</a>';
  1068. }
  1069. }
  1070. /**
  1071. * Specialization of a string: IP address
  1072. *
  1073. * @package iTopORM
  1074. */
  1075. class AttributeIPAddress extends AttributeString
  1076. {
  1077. public function GetValidationPattern()
  1078. {
  1079. $sNum = '(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])';
  1080. return "^($sNum\\.$sNum\\.$sNum\\.$sNum)$";
  1081. }
  1082. }
  1083. /**
  1084. * Specialization of a string: OQL expression
  1085. *
  1086. * @package iTopORM
  1087. */
  1088. class AttributeOQL extends AttributeText
  1089. {
  1090. }
  1091. /**
  1092. * Specialization of a string: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
  1093. *
  1094. * @package iTopORM
  1095. */
  1096. class AttributeTemplateString extends AttributeString
  1097. {
  1098. }
  1099. /**
  1100. * Specialization of a text: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
  1101. *
  1102. * @package iTopORM
  1103. */
  1104. class AttributeTemplateText extends AttributeText
  1105. {
  1106. }
  1107. /**
  1108. * Specialization of a HTML: template (contains iTop placeholders like $current_contact_id$ or $this->name$)
  1109. *
  1110. * @package iTopORM
  1111. */
  1112. class AttributeTemplateHTML extends AttributeText
  1113. {
  1114. public function GetEditClass() {return "HTML";}
  1115. public function GetAsHTML($sValue)
  1116. {
  1117. return $sValue;
  1118. }
  1119. }
  1120. /**
  1121. * Specialization of a text: wiki formatting
  1122. *
  1123. * @package iTopORM
  1124. */
  1125. class AttributeWikiText extends AttributeText
  1126. {
  1127. public function GetAsHTML($value)
  1128. {
  1129. // [SELECT xxxx.... [label]] => hyperlink to a result list
  1130. // {SELECT xxxx.... [label]} => result list displayed inline
  1131. // [myclass/nnn [label]] => hyperlink to an object
  1132. // {myclass/nnn/attcode} => attribute displayed inline
  1133. // etc.
  1134. return parent::GetAsHTML($value);
  1135. }
  1136. }
  1137. /**
  1138. * Map a enum column to an attribute
  1139. *
  1140. * @package iTopORM
  1141. */
  1142. class AttributeEnum extends AttributeString
  1143. {
  1144. static protected function ListExpectedParams()
  1145. {
  1146. return parent::ListExpectedParams();
  1147. //return array_merge(parent::ListExpectedParams(), array());
  1148. }
  1149. public function GetEditClass() {return "String";}
  1150. protected function GetSQLCol()
  1151. {
  1152. $oValDef = $this->GetValuesDef();
  1153. if ($oValDef)
  1154. {
  1155. $aValues = CMDBSource::Quote(array_keys($oValDef->GetValues(array(), "")), true);
  1156. }
  1157. else
  1158. {
  1159. $aValues = array();
  1160. }
  1161. if (count($aValues) > 0)
  1162. {
  1163. // The syntax used here do matters
  1164. // In particular, I had to remove unnecessary spaces to
  1165. // make sure that this string will match the field type returned by the DB
  1166. // (used to perform a comparison between the current DB format and the data model)
  1167. return "ENUM(".implode(",", $aValues).")";
  1168. }
  1169. else
  1170. {
  1171. return "VARCHAR(255)"; // ENUM() is not an allowed syntax!
  1172. }
  1173. }
  1174. public function ScalarToSQL($value)
  1175. {
  1176. // Note: for strings, the null value is an empty string and it is recorded as such in the DB
  1177. // but that wasn't working for enums, because '' is NOT one of the allowed values
  1178. // that's why a null value must be forced to a real null
  1179. $value = parent::ScalarToSQL($value);
  1180. if ($this->IsNull($value))
  1181. {
  1182. return null;
  1183. }
  1184. else
  1185. {
  1186. return $value;
  1187. }
  1188. }
  1189. public function RequiresIndex()
  1190. {
  1191. return false;
  1192. }
  1193. public function GetBasicFilterOperators()
  1194. {
  1195. return parent::GetBasicFilterOperators();
  1196. }
  1197. public function GetBasicFilterLooseOperator()
  1198. {
  1199. return parent::GetBasicFilterLooseOperator();
  1200. }
  1201. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1202. {
  1203. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  1204. }
  1205. public function GetAsHTML($sValue)
  1206. {
  1207. if (is_null($sValue))
  1208. {
  1209. // Unless a specific label is defined for the null value of this enum, use a generic "undefined" label
  1210. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, Dict::S('Enum:Undefined'));
  1211. }
  1212. else
  1213. {
  1214. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  1215. }
  1216. $sDescription = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue.'+', $sValue);
  1217. // later, we could imagine a detailed description in the title
  1218. return "<span title=\"$sDescription\">".parent::GetAsHtml($sLabel)."</span>";
  1219. }
  1220. public function GetEditValue($sValue)
  1221. {
  1222. $sLabel = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sValue, $sValue);
  1223. return $sLabel;
  1224. }
  1225. public function GetAllowedValues($aArgs = array(), $sContains = '')
  1226. {
  1227. $aRawValues = parent::GetAllowedValues($aArgs, $sContains);
  1228. if (is_null($aRawValues)) return null;
  1229. $aLocalizedValues = array();
  1230. foreach ($aRawValues as $sKey => $sValue)
  1231. {
  1232. $aLocalizedValues[$sKey] = Dict::S('Class:'.$this->GetHostClass().'/Attribute:'.$this->GetCode().'/Value:'.$sKey, $sKey);
  1233. }
  1234. return $aLocalizedValues;
  1235. }
  1236. /**
  1237. * Processes the input value to align it with the values supported
  1238. * by this type of attribute. In this case: turns empty strings into nulls
  1239. * @param mixed $proposedValue The value to be set for the attribute
  1240. * @return mixed The actual value that will be set
  1241. */
  1242. public function MakeRealValue($proposedValue)
  1243. {
  1244. if ($proposedValue == '') return null;
  1245. return parent::MakeRealValue($proposedValue);
  1246. }
  1247. }
  1248. /**
  1249. * Map a date+time column to an attribute
  1250. *
  1251. * @package iTopORM
  1252. */
  1253. class AttributeDateTime extends AttributeDBField
  1254. {
  1255. //const MYDATETIMEZONE = "UTC";
  1256. const MYDATETIMEZONE = "Europe/Paris";
  1257. static protected $const_TIMEZONE = null; // set once for all upon object construct
  1258. static public function InitStatics()
  1259. {
  1260. // Init static constant once for all (remove when PHP allows real static const)
  1261. self::$const_TIMEZONE = new DateTimeZone(self::MYDATETIMEZONE);
  1262. // #@# Init default timezone -> do not get a notice... to be improved !!!
  1263. // duplicated in the email test page (the mail function does trigger a notice as well)
  1264. date_default_timezone_set(self::MYDATETIMEZONE);
  1265. }
  1266. static protected function GetDateFormat()
  1267. {
  1268. return "Y-m-d H:i:s";
  1269. }
  1270. static protected function ListExpectedParams()
  1271. {
  1272. return parent::ListExpectedParams();
  1273. //return array_merge(parent::ListExpectedParams(), array());
  1274. }
  1275. public function GetEditClass() {return "DateTime";}
  1276. protected function GetSQLCol() {return "TIMESTAMP";}
  1277. public static function GetAsUnixSeconds($value)
  1278. {
  1279. $oDeadlineDateTime = new DateTime($value, self::$const_TIMEZONE);
  1280. $iUnixSeconds = $oDeadlineDateTime->format('U');
  1281. return $iUnixSeconds;
  1282. }
  1283. // #@# THIS HAS TO REVISED
  1284. // Having null not allowed was interpreted by mySQL
  1285. // which was creating the field with the flag 'ON UPDATE CURRENT_TIMESTAMP'
  1286. // Then, on each update of the record, the field was modified.
  1287. // We will have to specify the default value if we want to restore this option
  1288. // In fact, we could also have more verbs dedicated to the DB:
  1289. // GetDBDefaultValue()... or GetDBFieldCreationStatement()....
  1290. public function IsNullAllowed() {return true;}
  1291. public function GetDefaultValue()
  1292. {
  1293. $default = parent::GetDefaultValue();
  1294. if (!parent::IsNullAllowed())
  1295. {
  1296. if (empty($default))
  1297. {
  1298. $default = date(self::GetDateFormat());
  1299. }
  1300. }
  1301. return $default;
  1302. }
  1303. // END OF THE WORKAROUND
  1304. ///////////////////////////////////////////////////////////////
  1305. public function GetValidationPattern()
  1306. {
  1307. return "^([0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30))))( (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])){0,1}|0000-00-00 00:00:00|0000-00-00$";
  1308. }
  1309. public function GetBasicFilterOperators()
  1310. {
  1311. return array(
  1312. "="=>"equals",
  1313. "!="=>"differs from",
  1314. "<"=>"before",
  1315. "<="=>"before",
  1316. ">"=>"after (strictly)",
  1317. ">="=>"after",
  1318. "SameDay"=>"same day (strip time)",
  1319. "SameMonth"=>"same year/month",
  1320. "SameYear"=>"same year",
  1321. "Today"=>"today",
  1322. ">|"=>"after today + N days",
  1323. "<|"=>"before today + N days",
  1324. "=|"=>"equals today + N days",
  1325. );
  1326. }
  1327. public function GetBasicFilterLooseOperator()
  1328. {
  1329. // Unless we implement a "same xxx, depending on given precision" !
  1330. return "=";
  1331. }
  1332. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1333. {
  1334. $sQValue = CMDBSource::Quote($value);
  1335. switch ($sOpCode)
  1336. {
  1337. case '=':
  1338. case '!=':
  1339. case '<':
  1340. case '<=':
  1341. case '>':
  1342. case '>=':
  1343. return $this->GetSQLExpr()." $sOpCode $sQValue";
  1344. case 'SameDay':
  1345. return "DATE(".$this->GetSQLExpr().") = DATE($sQValue)";
  1346. case 'SameMonth':
  1347. return "DATE_FORMAT(".$this->GetSQLExpr().", '%Y-%m') = DATE_FORMAT($sQValue, '%Y-%m')";
  1348. case 'SameYear':
  1349. return "MONTH(".$this->GetSQLExpr().") = MONTH($sQValue)";
  1350. case 'Today':
  1351. return "DATE(".$this->GetSQLExpr().") = CURRENT_DATE()";
  1352. case '>|':
  1353. return "DATE(".$this->GetSQLExpr().") > DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1354. case '<|':
  1355. return "DATE(".$this->GetSQLExpr().") < DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1356. case '=|':
  1357. return "DATE(".$this->GetSQLExpr().") = DATE_ADD(CURRENT_DATE(), INTERVAL $sQValue DAY)";
  1358. default:
  1359. return $this->GetSQLExpr()." = $sQValue";
  1360. }
  1361. }
  1362. public function MakeRealValue($proposedValue)
  1363. {
  1364. if (is_null($proposedValue))
  1365. {
  1366. return null;
  1367. }
  1368. if (is_string($proposedValue) && ($proposedValue == "") && $this->IsNullAllowed())
  1369. {
  1370. return null;
  1371. }
  1372. if (!is_numeric($proposedValue))
  1373. {
  1374. return $proposedValue;
  1375. }
  1376. return date(self::GetDateFormat(), $proposedValue);
  1377. }
  1378. public function ScalarToSQL($value)
  1379. {
  1380. if (is_null($value))
  1381. {
  1382. return null;
  1383. }
  1384. elseif (empty($value))
  1385. {
  1386. // Make a valid date for MySQL. TO DO: support NULL as a literal value for fields that can be null.
  1387. return '0000-00-00 00:00:00';
  1388. }
  1389. return $value;
  1390. }
  1391. public function GetAsHTML($value)
  1392. {
  1393. return Str::pure2html($value);
  1394. }
  1395. public function GetAsXML($value)
  1396. {
  1397. return Str::pure2xml($value);
  1398. }
  1399. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  1400. {
  1401. $sFrom = array("\r\n", $sTextQualifier);
  1402. $sTo = array("\n", $sTextQualifier.$sTextQualifier);
  1403. $sEscaped = str_replace($sFrom, $sTo, (string)$sValue);
  1404. return '"'.$sEscaped.'"';
  1405. }
  1406. /**
  1407. * Parses a string to find some smart search patterns and build the corresponding search/OQL condition
  1408. * Each derived class is reponsible for defining and processing their own smart patterns, the base class
  1409. * does nothing special, and just calls the default (loose) operator
  1410. * @param string $sSearchText The search string to analyze for smart patterns
  1411. * @param FieldExpression The FieldExpression representing the atttribute code in this OQL query
  1412. * @param Hash $aParams Values of the query parameters
  1413. * @return Expression The search condition to be added (AND) to the current search
  1414. */
  1415. public function GetSmartConditionExpression($sSearchText, FieldExpression $oField, &$aParams)
  1416. {
  1417. // Possible smart patterns
  1418. $aPatterns = array(
  1419. 'between' => array('pattern' => '/^\[(.*),(.*)\]$/', 'operator' => 'n/a'),
  1420. 'greater than or equal' => array('pattern' => '/^>=(.*)$/', 'operator' => '>='),
  1421. 'greater than' => array('pattern' => '/^>(.*)$/', 'operator' => '>'),
  1422. 'less than or equal' => array('pattern' => '/^<=(.*)$/', 'operator' => '<='),
  1423. 'less than' => array('pattern' => '/^<(.*)$/', 'operator' => '<'),
  1424. );
  1425. $sPatternFound = '';
  1426. $aMatches = array();
  1427. foreach($aPatterns as $sPatName => $sPattern)
  1428. {
  1429. if (preg_match($sPattern['pattern'], $sSearchText, $aMatches))
  1430. {
  1431. $sPatternFound = $sPatName;
  1432. break;
  1433. }
  1434. }
  1435. switch($sPatternFound)
  1436. {
  1437. case 'between':
  1438. $sParamName1 = $oField->GetParent().'_'.$oField->GetName().'_1';
  1439. $oRightExpr = new VariableExpression($sParamName1);
  1440. $aParams[$sParamName1] = $aMatches[1];
  1441. $oCondition1 = new BinaryExpression($oField, '>=', $oRightExpr);
  1442. $sParamName2 = $oField->GetParent().'_'.$oField->GetName().'_2';
  1443. $oRightExpr = new VariableExpression($sParamName2);
  1444. $sOperator = $this->GetBasicFilterLooseOperator();
  1445. $aParams[$sParamName2] = $aMatches[2];
  1446. $oCondition2 = new BinaryExpression($oField, '<=', $oRightExpr);
  1447. $oNewCondition = new BinaryExpression($oCondition1, 'AND', $oCondition2);
  1448. break;
  1449. case 'greater than':
  1450. case 'greater than or equal':
  1451. case 'less than':
  1452. case 'less than or equal':
  1453. $sSQLOperator = $aPatterns[$sPatternFound]['operator'];
  1454. $sParamName = $oField->GetParent().'_'.$oField->GetName();
  1455. $oRightExpr = new VariableExpression($sParamName);
  1456. $aParams[$sParamName] = $aMatches[1];
  1457. $oNewCondition = new BinaryExpression($oField, $sSQLOperator, $oRightExpr);
  1458. break;
  1459. default:
  1460. $oNewCondition = parent::GetSmartConditionExpression($sSearchText, $oField, $aParams);
  1461. }
  1462. return $oNewCondition;
  1463. }
  1464. }
  1465. /**
  1466. * Store a duration as a number of seconds
  1467. *
  1468. * @package iTopORM
  1469. */
  1470. class AttributeDuration extends AttributeInteger
  1471. {
  1472. public function GetEditClass() {return "Duration";}
  1473. protected function GetSQLCol() {return "INT(11) UNSIGNED";}
  1474. public function GetNullValue() {return '0';}
  1475. public function GetDefaultValue()
  1476. {
  1477. return 0;
  1478. }
  1479. public function MakeRealValue($proposedValue)
  1480. {
  1481. if (is_null($proposedValue)) return null;
  1482. if (!is_numeric($proposedValue)) return null;
  1483. if ( ((int)$proposedValue) < 0) return null;
  1484. return (int)$proposedValue;
  1485. }
  1486. public function ScalarToSQL($value)
  1487. {
  1488. if (is_null($value))
  1489. {
  1490. return null;
  1491. }
  1492. return $value;
  1493. }
  1494. public function GetAsHTML($value)
  1495. {
  1496. return Str::pure2html(self::FormatDuration($value));
  1497. }
  1498. static function FormatDuration($duration)
  1499. {
  1500. $aDuration = self::SplitDuration($duration);
  1501. $sResult = '';
  1502. if ($duration < 60)
  1503. {
  1504. // Less than 1 min
  1505. $sResult = Dict::Format('Core:Duration_Seconds', $aDuration['seconds']);
  1506. }
  1507. else if ($duration < 3600)
  1508. {
  1509. // less than 1 hour, display it in minutes/seconds
  1510. $sResult = Dict::Format('Core:Duration_Minutes_Seconds', $aDuration['minutes'], $aDuration['seconds']);
  1511. }
  1512. else if ($duration < 86400)
  1513. {
  1514. // Less than 1 day, display it in hours/minutes/seconds
  1515. $sResult = Dict::Format('Core:Duration_Hours_Minutes_Seconds', $aDuration['hours'], $aDuration['minutes'], $aDuration['seconds']);
  1516. }
  1517. else
  1518. {
  1519. // more than 1 day, display it in days/hours/minutes/seconds
  1520. $sResult = Dict::Format('Core:Duration_Days_Hours_Minutes_Seconds', $aDuration['days'], $aDuration['hours'], $aDuration['minutes'], $aDuration['seconds']);
  1521. }
  1522. return $sResult;
  1523. }
  1524. static function SplitDuration($duration)
  1525. {
  1526. $days = floor($duration / 86400);
  1527. $hours = floor(($duration - (86400*$days)) / 3600);
  1528. $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
  1529. $seconds = ($duration % 60); // modulo
  1530. return array( 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds );
  1531. }
  1532. }
  1533. /**
  1534. * Map a date+time column to an attribute
  1535. *
  1536. * @package iTopORM
  1537. */
  1538. class AttributeDate extends AttributeDateTime
  1539. {
  1540. const MYDATEFORMAT = "Y-m-d";
  1541. static protected function GetDateFormat()
  1542. {
  1543. return "Y-m-d";
  1544. }
  1545. static public function InitStatics()
  1546. {
  1547. // Nothing to do...
  1548. }
  1549. static protected function ListExpectedParams()
  1550. {
  1551. return parent::ListExpectedParams();
  1552. //return array_merge(parent::ListExpectedParams(), array());
  1553. }
  1554. public function GetEditClass() {return "Date";}
  1555. protected function GetSQLCol() {return "DATE";}
  1556. public function GetValidationPattern()
  1557. {
  1558. return "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
  1559. }
  1560. }
  1561. // Init static constant once for all (remove when PHP allows real static const)
  1562. AttributeDate::InitStatics();
  1563. /**
  1564. * A dead line stored as a date & time
  1565. * The only difference with the DateTime attribute is the display:
  1566. * relative to the current time
  1567. */
  1568. class AttributeDeadline extends AttributeDateTime
  1569. {
  1570. public function GetAsHTML($value)
  1571. {
  1572. $sResult = '';
  1573. if ($value !== null)
  1574. {
  1575. $value = AttributeDateTime::GetAsUnixSeconds($value);
  1576. $difference = $value - time();
  1577. if ($difference >= 0)
  1578. {
  1579. $sResult = self::FormatDuration($difference);
  1580. }
  1581. else
  1582. {
  1583. $sResult = Dict::Format('UI:DeadlineMissedBy_duration', self::FormatDuration(-$difference));
  1584. }
  1585. }
  1586. return $sResult;
  1587. }
  1588. static function FormatDuration($duration)
  1589. {
  1590. $days = floor($duration / 86400);
  1591. $hours = floor(($duration - (86400*$days)) / 3600);
  1592. $minutes = floor(($duration - (86400*$days + 3600*$hours)) / 60);
  1593. $sResult = '';
  1594. if ($duration < 60)
  1595. {
  1596. // Less than 1 min
  1597. $sResult =Dict::S('UI:Deadline_LessThan1Min');
  1598. }
  1599. else if ($duration < 3600)
  1600. {
  1601. // less than 1 hour, display it in minutes
  1602. $sResult =Dict::Format('UI:Deadline_Minutes', $minutes);
  1603. }
  1604. else if ($duration < 86400)
  1605. {
  1606. // Less that 1 day, display it in hours/minutes
  1607. $sResult =Dict::Format('UI:Deadline_Hours_Minutes', $hours, $minutes);
  1608. }
  1609. else
  1610. {
  1611. // Less that 1 day, display it in hours/minutes
  1612. $sResult =Dict::Format('UI:Deadline_Days_Hours_Minutes', $days, $hours, $minutes);
  1613. }
  1614. return $sResult;
  1615. }
  1616. }
  1617. // Init static constant once for all (remove when PHP allows real static const)
  1618. AttributeDateTime::InitStatics();
  1619. /**
  1620. * Map a foreign key to an attribute
  1621. * AttributeExternalKey and AttributeExternalField may be an external key
  1622. * the difference is that AttributeExternalKey corresponds to a column into the defined table
  1623. * where an AttributeExternalField corresponds to a column into another table (class)
  1624. *
  1625. * @package iTopORM
  1626. */
  1627. class AttributeExternalKey extends AttributeDBFieldVoid
  1628. {
  1629. static protected function ListExpectedParams()
  1630. {
  1631. return array_merge(parent::ListExpectedParams(), array("targetclass", "is_null_allowed", "on_target_delete"));
  1632. }
  1633. public function GetEditClass() {return "ExtKey";}
  1634. protected function GetSQLCol() {return "INT(11)";}
  1635. public function RequiresIndex()
  1636. {
  1637. return true;
  1638. }
  1639. public function IsExternalKey($iType = EXTKEY_RELATIVE) {return true;}
  1640. public function GetTargetClass($iType = EXTKEY_RELATIVE) {return $this->Get("targetclass");}
  1641. public function GetKeyAttDef($iType = EXTKEY_RELATIVE){return $this;}
  1642. public function GetKeyAttCode() {return $this->GetCode();}
  1643. public function GetDefaultValue() {return 0;}
  1644. public function IsNullAllowed() {return $this->Get("is_null_allowed");}
  1645. public function GetBasicFilterOperators()
  1646. {
  1647. return parent::GetBasicFilterOperators();
  1648. }
  1649. public function GetBasicFilterLooseOperator()
  1650. {
  1651. return parent::GetBasicFilterLooseOperator();
  1652. }
  1653. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1654. {
  1655. return parent::GetBasicFilterSQLExpr($sOpCode, $value);
  1656. }
  1657. // overloaded here so that an ext key always have the answer to
  1658. // "what are your possible values?"
  1659. public function GetValuesDef()
  1660. {
  1661. $oValSetDef = $this->Get("allowed_values");
  1662. if (!$oValSetDef)
  1663. {
  1664. // Let's propose every existing value
  1665. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1666. }
  1667. return $oValSetDef;
  1668. }
  1669. public function GetAllowedValues($aArgs = array(), $sContains = '')
  1670. {
  1671. try
  1672. {
  1673. return parent::GetAllowedValues($aArgs, $sContains);
  1674. }
  1675. catch (MissingQueryArgument $e)
  1676. {
  1677. // Some required arguments could not be found, enlarge to any existing value
  1678. $oValSetDef = new ValueSetObjects('SELECT '.$this->GetTargetClass());
  1679. return $oValSetDef->GetValues($aArgs, $sContains);
  1680. }
  1681. }
  1682. public function GetDeletionPropagationOption()
  1683. {
  1684. return $this->Get("on_target_delete");
  1685. }
  1686. public function GetNullValue()
  1687. {
  1688. return 0;
  1689. }
  1690. public function IsNull($proposedValue)
  1691. {
  1692. return ($proposedValue == 0);
  1693. }
  1694. public function MakeRealValue($proposedValue)
  1695. {
  1696. if (is_null($proposedValue)) return 0;
  1697. if ($proposedValue === '') return 0;
  1698. if (MetaModel::IsValidObject($proposedValue)) return $proposedValue->GetKey();
  1699. return (int)$proposedValue;
  1700. }
  1701. public function GetMaximumComboLength()
  1702. {
  1703. return $this->GetOptional('max_combo_length', MetaModel::GetConfig()->Get('max_combo_length'));
  1704. }
  1705. public function GetMinAutoCompleteChars()
  1706. {
  1707. return $this->GetOptional('min_autocomplete_chars', MetaModel::GetConfig()->Get('min_autocomplete_chars'));
  1708. }
  1709. public function AllowTargetCreation()
  1710. {
  1711. return $this->GetOptional('allow_target_creation', MetaModel::GetConfig()->Get('allow_target_creation'));
  1712. }
  1713. }
  1714. /**
  1715. * An attribute which corresponds to an external key (direct or indirect)
  1716. *
  1717. * @package iTopORM
  1718. */
  1719. class AttributeExternalField extends AttributeDefinition
  1720. {
  1721. static protected function ListExpectedParams()
  1722. {
  1723. return array_merge(parent::ListExpectedParams(), array("extkey_attcode", "target_attcode"));
  1724. }
  1725. public function GetEditClass() {return "ExtField";}
  1726. protected function GetSQLCol()
  1727. {
  1728. // throw new CoreException("external attribute: does it make any sense to request its type ?");
  1729. $oExtAttDef = $this->GetExtAttDef();
  1730. return $oExtAttDef->GetSQLCol();
  1731. }
  1732. public function GetSQLExpressions($sPrefix = '')
  1733. {
  1734. if ($sPrefix == '')
  1735. {
  1736. return array('' => $this->GetCode());
  1737. }
  1738. else
  1739. {
  1740. return $sPrefix;
  1741. }
  1742. }
  1743. public function GetLabel()
  1744. {
  1745. $oRemoteAtt = $this->GetExtAttDef();
  1746. $sDefault = $oRemoteAtt->GetLabel();
  1747. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode, $sDefault);
  1748. }
  1749. public function GetDescription()
  1750. {
  1751. $oRemoteAtt = $this->GetExtAttDef();
  1752. $sDefault = $oRemoteAtt->GetDescription();
  1753. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'+', $sDefault);
  1754. }
  1755. public function GetHelpOnEdition()
  1756. {
  1757. $oRemoteAtt = $this->GetExtAttDef();
  1758. $sDefault = $oRemoteAtt->GetHelpOnEdition();
  1759. return Dict::S('Class:'.$this->m_sHostClass.'/Attribute:'.$this->m_sCode.'?', $sDefault);
  1760. }
  1761. public function IsExternalKey($iType = EXTKEY_RELATIVE)
  1762. {
  1763. switch($iType)
  1764. {
  1765. case EXTKEY_ABSOLUTE:
  1766. // see further
  1767. $oRemoteAtt = $this->GetExtAttDef();
  1768. return $oRemoteAtt->IsExternalKey($iType);
  1769. case EXTKEY_RELATIVE:
  1770. return false;
  1771. default:
  1772. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1773. }
  1774. }
  1775. public function GetTargetClass($iType = EXTKEY_RELATIVE)
  1776. {
  1777. return $this->GetKeyAttDef($iType)->GetTargetClass();
  1778. }
  1779. public function IsExternalField() {return true;}
  1780. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  1781. public function GetExtAttCode() {return $this->Get("target_attcode");}
  1782. public function GetKeyAttDef($iType = EXTKEY_RELATIVE)
  1783. {
  1784. switch($iType)
  1785. {
  1786. case EXTKEY_ABSOLUTE:
  1787. // see further
  1788. $oRemoteAtt = $this->GetExtAttDef();
  1789. if ($oRemoteAtt->IsExternalField())
  1790. {
  1791. return $oRemoteAtt->GetKeyAttDef(EXTKEY_ABSOLUTE);
  1792. }
  1793. else if ($oRemoteAtt->IsExternalKey())
  1794. {
  1795. return $oRemoteAtt;
  1796. }
  1797. return $this->GetKeyAttDef(EXTKEY_RELATIVE); // which corresponds to the code hereafter !
  1798. case EXTKEY_RELATIVE:
  1799. return MetaModel::GetAttributeDef($this->GetHostClass(), $this->Get("extkey_attcode"));
  1800. default:
  1801. throw new CoreException("Unexpected value for argument iType: '$iType'");
  1802. }
  1803. }
  1804. public function GetExtAttDef()
  1805. {
  1806. $oKeyAttDef = $this->GetKeyAttDef();
  1807. $oExtAttDef = MetaModel::GetAttributeDef($oKeyAttDef->Get("targetclass"), $this->Get("target_attcode"));
  1808. if (!is_object($oExtAttDef)) throw new CoreException("Invalid external field ".$this->GetCode()." in class ".$this->GetHostClass().". The class ".$oKeyAttDef->Get("targetclass")." has no attribute ".$this->Get("target_attcode"));
  1809. return $oExtAttDef;
  1810. }
  1811. public function GetSQLExpr()
  1812. {
  1813. $oExtAttDef = $this->GetExtAttDef();
  1814. return $oExtAttDef->GetSQLExpr();
  1815. }
  1816. public function GetDefaultValue()
  1817. {
  1818. $oExtAttDef = $this->GetExtAttDef();
  1819. return $oExtAttDef->GetDefaultValue();
  1820. }
  1821. public function IsNullAllowed()
  1822. {
  1823. $oExtAttDef = $this->GetExtAttDef();
  1824. return $oExtAttDef->IsNullAllowed();
  1825. }
  1826. public function IsScalar()
  1827. {
  1828. $oExtAttDef = $this->GetExtAttDef();
  1829. return $oExtAttDef->IsScalar();
  1830. }
  1831. public function GetFilterDefinitions()
  1832. {
  1833. return array($this->GetCode() => new FilterFromAttribute($this));
  1834. }
  1835. public function GetBasicFilterOperators()
  1836. {
  1837. $oExtAttDef = $this->GetExtAttDef();
  1838. return $oExtAttDef->GetBasicFilterOperators();
  1839. }
  1840. public function GetBasicFilterLooseOperator()
  1841. {
  1842. $oExtAttDef = $this->GetExtAttDef();
  1843. return $oExtAttDef->GetBasicFilterLooseOperator();
  1844. }
  1845. public function GetBasicFilterSQLExpr($sOpCode, $value)
  1846. {
  1847. $oExtAttDef = $this->GetExtAttDef();
  1848. return $oExtAttDef->GetBasicFilterSQLExpr($sOpCode, $value);
  1849. }
  1850. public function GetNullValue()
  1851. {
  1852. $oExtAttDef = $this->GetExtAttDef();
  1853. return $oExtAttDef->GetNullValue();
  1854. }
  1855. public function IsNull($proposedValue)
  1856. {
  1857. $oExtAttDef = $this->GetExtAttDef();
  1858. return $oExtAttDef->IsNull($proposedValue);
  1859. }
  1860. public function MakeRealValue($proposedValue)
  1861. {
  1862. $oExtAttDef = $this->GetExtAttDef();
  1863. return $oExtAttDef->MakeRealValue($proposedValue);
  1864. }
  1865. public function ScalarToSQL($value)
  1866. {
  1867. // This one could be used in case of filtering only
  1868. $oExtAttDef = $this->GetExtAttDef();
  1869. return $oExtAttDef->ScalarToSQL($value);
  1870. }
  1871. // Do not overload GetSQLExpression here because this is handled in the joins
  1872. //public function GetSQLExpressions($sPrefix = '') {return array();}
  1873. // Here, we get the data...
  1874. public function FromSQLToValue($aCols, $sPrefix = '')
  1875. {
  1876. $oExtAttDef = $this->GetExtAttDef();
  1877. return $oExtAttDef->FromSQLToValue($aCols, $sPrefix);
  1878. }
  1879. public function GetAsHTML($value)
  1880. {
  1881. $oExtAttDef = $this->GetExtAttDef();
  1882. return $oExtAttDef->GetAsHTML($value);
  1883. }
  1884. public function GetAsXML($value)
  1885. {
  1886. $oExtAttDef = $this->GetExtAttDef();
  1887. return $oExtAttDef->GetAsXML($value);
  1888. }
  1889. public function GetAsCSV($value, $sSeparator = ',', $sTestQualifier = '"')
  1890. {
  1891. $oExtAttDef = $this->GetExtAttDef();
  1892. return $oExtAttDef->GetAsCSV($value, $sSeparator, $sTestQualifier);
  1893. }
  1894. }
  1895. /**
  1896. * Map a varchar column to an URL (formats the ouput in HMTL)
  1897. *
  1898. * @package iTopORM
  1899. */
  1900. class AttributeURL extends AttributeString
  1901. {
  1902. static protected function ListExpectedParams()
  1903. {
  1904. //return parent::ListExpectedParams();
  1905. return array_merge(parent::ListExpectedParams(), array("target"));
  1906. }
  1907. public function GetEditClass() {return "String";}
  1908. public function GetAsHTML($sValue)
  1909. {
  1910. $sTarget = $this->Get("target");
  1911. if (empty($sTarget)) $sTarget = "_blank";
  1912. $sLabel = Str::pure2html($sValue);
  1913. if (strlen($sLabel) > 40)
  1914. {
  1915. // Truncate the length to about 40 characters, by removing the middle
  1916. $sLabel = substr($sLabel, 0, 25).'...'.substr($sLabel, -15);
  1917. }
  1918. return "<a target=\"$sTarget\" href=\"$sValue\">$sLabel</a>";
  1919. }
  1920. public function GetValidationPattern()
  1921. {
  1922. return "^(http|https|ftp)\://[a-zA-Z0-9\-\.]+(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$";
  1923. }
  1924. }
  1925. /**
  1926. * A blob is an ormDocument, it is stored as several columns in the database
  1927. *
  1928. * @package iTopORM
  1929. */
  1930. class AttributeBlob extends AttributeDefinition
  1931. {
  1932. static protected function ListExpectedParams()
  1933. {
  1934. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  1935. }
  1936. public function GetEditClass() {return "Document";}
  1937. public function IsDirectField() {return true;}
  1938. public function IsScalar() {return true;}
  1939. public function IsWritable() {return true;}
  1940. public function GetDefaultValue() {return "";}
  1941. public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
  1942. // Facilitate things: allow the user to Set the value from a string
  1943. public function MakeRealValue($proposedValue)
  1944. {
  1945. if (!is_object($proposedValue))
  1946. {
  1947. return new ormDocument($proposedValue, 'text/plain');
  1948. }
  1949. return $proposedValue;
  1950. }
  1951. public function GetSQLExpressions($sPrefix = '')
  1952. {
  1953. if ($sPrefix == '')
  1954. {
  1955. $sPrefix = $this->GetCode();
  1956. }
  1957. $aColumns = array();
  1958. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  1959. $aColumns[''] = $sPrefix.'_mimetype';
  1960. $aColumns['_data'] = $sPrefix.'_data';
  1961. $aColumns['_filename'] = $sPrefix.'_filename';
  1962. return $aColumns;
  1963. }
  1964. public function FromSQLToValue($aCols, $sPrefix = '')
  1965. {
  1966. if (!isset($aCols[$sPrefix]))
  1967. {
  1968. $sAvailable = implode(', ', array_keys($aCols));
  1969. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  1970. }
  1971. $sMimeType = $aCols[$sPrefix];
  1972. if (!isset($aCols[$sPrefix.'_data']))
  1973. {
  1974. $sAvailable = implode(', ', array_keys($aCols));
  1975. throw new MissingColumnException("Missing column '".$sPrefix."_data' from {$sAvailable}");
  1976. }
  1977. $data = $aCols[$sPrefix.'_data'];
  1978. if (!isset($aCols[$sPrefix.'_filename']))
  1979. {
  1980. $sAvailable = implode(', ', array_keys($aCols));
  1981. throw new MissingColumnException("Missing column '".$sPrefix."_filename' from {$sAvailable}");
  1982. }
  1983. $sFileName = $aCols[$sPrefix.'_filename'];
  1984. $value = new ormDocument($data, $sMimeType, $sFileName);
  1985. return $value;
  1986. }
  1987. public function GetSQLValues($value)
  1988. {
  1989. // #@# Optimization: do not load blobs anytime
  1990. // As per mySQL doc, selecting blob columns will prevent mySQL from
  1991. // using memory in case a temporary table has to be created
  1992. // (temporary tables created on disk)
  1993. // We will have to remove the blobs from the list of attributes when doing the select
  1994. // then the use of Get() should finalize the load
  1995. if ($value instanceOf ormDocument)
  1996. {
  1997. $aValues = array();
  1998. $aValues[$this->GetCode().'_data'] = $value->GetData();
  1999. $aValues[$this->GetCode().'_mimetype'] = $value->GetMimeType();
  2000. $aValues[$this->GetCode().'_filename'] = $value->GetFileName();
  2001. }
  2002. else
  2003. {
  2004. $aValues = array();
  2005. $aValues[$this->GetCode().'_data'] = '';
  2006. $aValues[$this->GetCode().'_mimetype'] = '';
  2007. $aValues[$this->GetCode().'_filename'] = '';
  2008. }
  2009. return $aValues;
  2010. }
  2011. public function GetSQLColumns()
  2012. {
  2013. $aColumns = array();
  2014. $aColumns[$this->GetCode().'_data'] = 'LONGBLOB'; // 2^32 (4 Gb)
  2015. $aColumns[$this->GetCode().'_mimetype'] = 'VARCHAR(255)';
  2016. $aColumns[$this->GetCode().'_filename'] = 'VARCHAR(255)';
  2017. return $aColumns;
  2018. }
  2019. public function GetFilterDefinitions()
  2020. {
  2021. return array();
  2022. // still not working... see later...
  2023. return array(
  2024. $this->GetCode().'->filename' => new FilterFromAttribute($this, '_filename'),
  2025. $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype'),
  2026. $this->GetCode().'_mimetype' => new FilterFromAttribute($this, '_mimetype')
  2027. );
  2028. }
  2029. public function GetBasicFilterOperators()
  2030. {
  2031. return array();
  2032. }
  2033. public function GetBasicFilterLooseOperator()
  2034. {
  2035. return '=';
  2036. }
  2037. public function GetBasicFilterSQLExpr($sOpCode, $value)
  2038. {
  2039. return 'true';
  2040. }
  2041. public function GetAsHTML($value)
  2042. {
  2043. if (is_object($value))
  2044. {
  2045. return $value->GetAsHTML();
  2046. }
  2047. }
  2048. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  2049. {
  2050. return ''; // Not exportable in CSV !
  2051. }
  2052. public function GetAsXML($value)
  2053. {
  2054. return ''; // Not exportable in XML, or as CDATA + some subtags ??
  2055. }
  2056. }
  2057. /**
  2058. * One way encrypted (hashed) password
  2059. */
  2060. class AttributeOneWayPassword extends AttributeDefinition
  2061. {
  2062. static protected function ListExpectedParams()
  2063. {
  2064. return array_merge(parent::ListExpectedParams(), array("depends_on"));
  2065. }
  2066. public function GetEditClass() {return "One Way Password";}
  2067. public function IsDirectField() {return true;}
  2068. public function IsScalar() {return true;}
  2069. public function IsWritable() {return true;}
  2070. public function GetDefaultValue() {return "";}
  2071. public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
  2072. // Facilitate things: allow the user to Set the value from a string or from an ormPassword (already encrypted)
  2073. public function MakeRealValue($proposedValue)
  2074. {
  2075. $oPassword = $proposedValue;
  2076. if (!is_object($oPassword))
  2077. {
  2078. $oPassword = new ormPassword('', '');
  2079. $oPassword->SetPassword($proposedValue);
  2080. }
  2081. return $oPassword;
  2082. }
  2083. public function GetSQLExpressions($sPrefix = '')
  2084. {
  2085. if ($sPrefix == '')
  2086. {
  2087. $sPrefix = $this->GetCode();
  2088. }
  2089. $aColumns = array();
  2090. // Note: to optimize things, the existence of the attribute is determined by the existence of one column with an empty suffix
  2091. $aColumns[''] = $sPrefix.'_hash';
  2092. $aColumns['_salt'] = $sPrefix.'_salt';
  2093. return $aColumns;
  2094. }
  2095. public function FromSQLToValue($aCols, $sPrefix = '')
  2096. {
  2097. if (!isset($aCols[$sPrefix]))
  2098. {
  2099. $sAvailable = implode(', ', array_keys($aCols));
  2100. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  2101. }
  2102. $hashed = $aCols[$sPrefix];
  2103. if (!isset($aCols[$sPrefix.'_salt']))
  2104. {
  2105. $sAvailable = implode(', ', array_keys($aCols));
  2106. throw new MissingColumnException("Missing column '".$sPrefix."_salt' from {$sAvailable}");
  2107. }
  2108. $sSalt = $aCols[$sPrefix.'_salt'];
  2109. $value = new ormPassword($hashed, $sSalt);
  2110. return $value;
  2111. }
  2112. public function GetSQLValues($value)
  2113. {
  2114. // #@# Optimization: do not load blobs anytime
  2115. // As per mySQL doc, selecting blob columns will prevent mySQL from
  2116. // using memory in case a temporary table has to be created
  2117. // (temporary tables created on disk)
  2118. // We will have to remove the blobs from the list of attributes when doing the select
  2119. // then the use of Get() should finalize the load
  2120. if ($value instanceOf ormPassword)
  2121. {
  2122. $aValues = array();
  2123. $aValues[$this->GetCode().'_hash'] = $value->GetHash();
  2124. $aValues[$this->GetCode().'_salt'] = $value->GetSalt();
  2125. }
  2126. else
  2127. {
  2128. $aValues = array();
  2129. $aValues[$this->GetCode().'_hash'] = '';
  2130. $aValues[$this->GetCode().'_salt'] = '';
  2131. }
  2132. return $aValues;
  2133. }
  2134. public function GetSQLColumns()
  2135. {
  2136. $aColumns = array();
  2137. $aColumns[$this->GetCode().'_hash'] = 'TINYBLOB';
  2138. $aColumns[$this->GetCode().'_salt'] = 'TINYBLOB';
  2139. return $aColumns;
  2140. }
  2141. public function GetImportColumns()
  2142. {
  2143. $aColumns = array();
  2144. $aColumns[$this->GetCode()] = 'TINYTEXT';
  2145. return $aColumns;
  2146. }
  2147. public function FromImportToValue($aCols, $sPrefix = '')
  2148. {
  2149. if (!isset($aCols[$sPrefix]))
  2150. {
  2151. $sAvailable = implode(', ', array_keys($aCols));
  2152. throw new MissingColumnException("Missing column '$sPrefix' from {$sAvailable}");
  2153. }
  2154. $sClearPwd = $aCols[$sPrefix];
  2155. $oPassword = new ormPassword('', '');
  2156. $oPassword->SetPassword($sClearPwd);
  2157. return $oPassword;
  2158. }
  2159. public function GetFilterDefinitions()
  2160. {
  2161. return array();
  2162. // still not working... see later...
  2163. }
  2164. public function GetBasicFilterOperators()
  2165. {
  2166. return array();
  2167. }
  2168. public function GetBasicFilterLooseOperator()
  2169. {
  2170. return '=';
  2171. }
  2172. public function GetBasicFilterSQLExpr($sOpCode, $value)
  2173. {
  2174. return 'true';
  2175. }
  2176. public function GetAsHTML($value)
  2177. {
  2178. if (is_object($value))
  2179. {
  2180. return $value->GetAsHTML();
  2181. }
  2182. }
  2183. public function GetAsCSV($sValue, $sSeparator = ',', $sTextQualifier = '"')
  2184. {
  2185. return ''; // Not exportable in CSV
  2186. }
  2187. public function GetAsXML($value)
  2188. {
  2189. return ''; // Not exportable in XML
  2190. }
  2191. }
  2192. // Indexed array having two dimensions
  2193. class AttributeTable extends AttributeText
  2194. {
  2195. public function GetEditClass() {return "Text";}
  2196. protected function GetSQLCol() {return "TEXT";}
  2197. public function GetMaxSize()
  2198. {
  2199. return null;
  2200. }
  2201. // Facilitate things: allow the user to Set the value from a string
  2202. public function MakeRealValue($proposedValue)
  2203. {
  2204. if (!is_array($proposedValue))
  2205. {
  2206. return array(0 => array(0 => $proposedValue));
  2207. }
  2208. return $proposedValue;
  2209. }
  2210. public function FromSQLToValue($aCols, $sPrefix = '')
  2211. {
  2212. try
  2213. {
  2214. $value = @unserialize($aCols[$sPrefix.'']);
  2215. if ($value === false)
  2216. {
  2217. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  2218. }
  2219. }
  2220. catch(Exception $e)
  2221. {
  2222. $value = $this->MakeRealValue($aCols[$sPrefix.'']);
  2223. }
  2224. return $value;
  2225. }
  2226. public function GetSQLValues($value)
  2227. {
  2228. $aValues = array();
  2229. $aValues[$this->Get("sql")] = serialize($value);
  2230. return $aValues;
  2231. }
  2232. public function GetAsHTML($value)
  2233. {
  2234. if (!is_array($value))
  2235. {
  2236. throw new CoreException('Expecting an array', array('found' => get_class($value)));
  2237. }
  2238. if (count($value) == 0)
  2239. {
  2240. return "";
  2241. }
  2242. $sRes = "<TABLE class=\"listResults\">";
  2243. $sRes .= "<TBODY>";
  2244. foreach($value as $iRow => $aRawData)
  2245. {
  2246. $sRes .= "<TR>";
  2247. foreach ($aRawData as $iCol => $cell)
  2248. {
  2249. $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$cell));
  2250. $sRes .= "<TD>$sCell</TD>";
  2251. }
  2252. $sRes .= "</TR>";
  2253. }
  2254. $sRes .= "</TBODY>";
  2255. $sRes .= "</TABLE>";
  2256. return $sRes;
  2257. }
  2258. }
  2259. // The PHP value is a hash array, it is stored as a TEXT column
  2260. class AttributePropertySet extends AttributeTable
  2261. {
  2262. public function GetEditClass() {return "Text";}
  2263. protected function GetSQLCol() {return "TEXT";}
  2264. // Facilitate things: allow the user to Set the value from a string
  2265. public function MakeRealValue($proposedValue)
  2266. {
  2267. if (!is_array($proposedValue))
  2268. {
  2269. return array('?' => (string)$proposedValue);
  2270. }
  2271. return $proposedValue;
  2272. }
  2273. public function GetAsHTML($value)
  2274. {
  2275. if (!is_array($value))
  2276. {
  2277. throw new CoreException('Expecting an array', array('found' => get_class($value)));
  2278. }
  2279. if (count($value) == 0)
  2280. {
  2281. return "";
  2282. }
  2283. $sRes = "<TABLE class=\"listResults\">";
  2284. $sRes .= "<TBODY>";
  2285. foreach($value as $sProperty => $sValue)
  2286. {
  2287. $sRes .= "<TR>";
  2288. $sCell = str_replace("\n", "<br>\n", Str::pure2html((string)$sValue));
  2289. $sRes .= "<TD class=\"label\">$sProperty</TD><TD>$sCell</TD>";
  2290. $sRes .= "</TR>";
  2291. }
  2292. $sRes .= "</TBODY>";
  2293. $sRes .= "</TABLE>";
  2294. return $sRes;
  2295. }
  2296. }
  2297. /**
  2298. * The attribute dedicated to the friendly name automatic attribute (not written)
  2299. *
  2300. * @package iTopORM
  2301. */
  2302. class AttributeComputedFieldVoid extends AttributeDefinition
  2303. {
  2304. static protected function ListExpectedParams()
  2305. {
  2306. return array_merge(parent::ListExpectedParams(), array());
  2307. }
  2308. public function GetEditClass() {return "";}
  2309. public function GetValuesDef() {return null;}
  2310. public function GetPrerequisiteAttributes() {return $this->Get("depends_on");}
  2311. public function IsDirectField() {return true;}
  2312. public function IsScalar() {return true;}
  2313. public function IsWritable() {return false;}
  2314. public function GetSQLExpr() {return null;}
  2315. public function GetDefaultValue() {return $this->MakeRealValue("");}
  2316. public function IsNullAllowed() {return false;}
  2317. //
  2318. // protected function ScalarToSQL($value) {return $value;} // format value as a valuable SQL literal (quoted outside)
  2319. public function GetSQLExpressions($sPrefix = '')
  2320. {
  2321. if ($sPrefix == '')
  2322. {
  2323. $sPrefix = $this->GetCode();
  2324. }
  2325. return array('' => $sPrefix);
  2326. }
  2327. public function FromSQLToValue($aCols, $sPrefix = '')
  2328. {
  2329. return null;
  2330. }
  2331. public function GetSQLValues($value)
  2332. {
  2333. return array();
  2334. }
  2335. public function GetSQLColumns()
  2336. {
  2337. return array();
  2338. }
  2339. public function GetFilterDefinitions()
  2340. {
  2341. return array($this->GetCode() => new FilterFromAttribute($this));
  2342. }
  2343. public function GetBasicFilterOperators()
  2344. {
  2345. return array();
  2346. }
  2347. public function GetBasicFilterLooseOperator()
  2348. {
  2349. return "=";
  2350. }
  2351. public function GetBasicFilterSQLExpr($sOpCode, $value)
  2352. {
  2353. $sQValue = CMDBSource::Quote($value);
  2354. switch ($sOpCode)
  2355. {
  2356. case '!=':
  2357. return $this->GetSQLExpr()." != $sQValue";
  2358. break;
  2359. case '=':
  2360. default:
  2361. return $this->GetSQLExpr()." = $sQValue";
  2362. }
  2363. }
  2364. }
  2365. /**
  2366. * The attribute dedicated to the friendly name automatic attribute (not written)
  2367. *
  2368. * @package iTopORM
  2369. */
  2370. class AttributeFriendlyName extends AttributeComputedFieldVoid
  2371. {
  2372. public function __construct($sCode, $sExtKeyAttCode)
  2373. {
  2374. $this->m_sCode = $sCode;
  2375. $aParams = array();
  2376. // $aParams["is_null_allowed"] = false,
  2377. $aParams["default_value"] = '';
  2378. $aParams["extkey_attcode"] = $sExtKeyAttCode;
  2379. parent::__construct($sCode, $aParams);
  2380. $this->m_sValue = $this->Get("default_value");
  2381. }
  2382. public function GetKeyAttCode() {return $this->Get("extkey_attcode");}
  2383. // n/a, the friendly name is made of a complex expression (see GetNameSpec)
  2384. protected function GetSQLCol() {return "";}
  2385. public function FromSQLToValue($aCols, $sPrefix = '')
  2386. {
  2387. $sValue = $aCols[$sPrefix];
  2388. return $sValue;
  2389. }
  2390. /**
  2391. * Encrypt the value before storing it in the database
  2392. */
  2393. public function GetSQLValues($value)
  2394. {
  2395. return array();
  2396. }
  2397. public function IsWritable()
  2398. {
  2399. return false;
  2400. }
  2401. public function SetFixedValue($sValue)
  2402. {
  2403. $this->m_sValue = $sValue;
  2404. }
  2405. public function GetDefaultValue()
  2406. {
  2407. return $this->m_sValue;
  2408. }
  2409. public function GetAsHTML($sValue)
  2410. {
  2411. return Str::pure2html((string)$sValue);
  2412. }
  2413. }
  2414. ?>