PageRenderTime 88ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Vtiger/models/Link.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 278 lines | 155 code | 33 blank | 90 comment | 24 complexity | 29634579a67499ad60d90feb2a4650fb MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, LGPL-2.1, GPL-2.0, GPL-3.0
  1. <?php
  2. /*+***********************************************************************************
  3. * The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. *************************************************************************************/
  10. include_once 'vtlib/Vtiger/Link.php';
  11. /**
  12. * Vtiger Link Model Class
  13. */
  14. class Vtiger_Link_Model extends Vtiger_Link {
  15. // Class variable to store the child links
  16. protected $childlinks = array();
  17. /**
  18. * Function to get the value of a given property
  19. * @param <String> $propertyName
  20. * @return <Object>
  21. * @throws Exception
  22. */
  23. public function get($propertyName) {
  24. if(property_exists($this,$propertyName)){
  25. return $this->$propertyName;
  26. }
  27. }
  28. /**
  29. * Function to set the value of a given property
  30. * @param <String> $propertyName
  31. * @param <Object> $propertyValue
  32. * @return Vtiger_Link_Model instance
  33. */
  34. public function set($propertyName, $propertyValue) {
  35. $this->$propertyName = $propertyValue;
  36. return $this;
  37. }
  38. /**
  39. * Function to get the link url
  40. * @return <String>
  41. */
  42. public function getUrl() {
  43. return $this->convertToNativeLink();
  44. }
  45. /**
  46. * Function to get the link label
  47. * @return <String>
  48. */
  49. public function getLabel() {
  50. return $this->linklabel;
  51. }
  52. /**
  53. * Function to get the link type
  54. * @return <String>
  55. */
  56. public function getType() {
  57. return $this->linktype;
  58. }
  59. /**
  60. * Function to get the link icon name
  61. * @return <String>
  62. */
  63. public function getIcon() {
  64. return $this->linkicon;
  65. }
  66. /**
  67. * Function to check whether link has icon or not
  68. * @return <Boolean> true/false
  69. */
  70. public function isIconExists() {
  71. $linkIcon = $this->getIcon();
  72. if(empty($linkIcon)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. /**
  78. * Function to retrieve the icon path for the link icon
  79. * @return <String/Boolean> - returns image path if icon exits
  80. * else returns false;
  81. */
  82. public function getIconPath() {
  83. if(!$this->isIconExists()) {
  84. return false;
  85. }
  86. return Vtiger_Theme::getImagePath($this->getIcon());
  87. }
  88. /**
  89. * Function to get the link id
  90. * @return <Number>
  91. */
  92. public function getId() {
  93. return $this->linkid;
  94. }
  95. /**
  96. * Function to Add link to the child link list
  97. * @param Vtiger_Link_Model $link - link model
  98. * @result Vtiger_Link_Model - current Instance;
  99. */
  100. public function addChildLink(Vtiger_Link_Model $link) {
  101. $this->childlinks[] = $link;
  102. return $this;
  103. }
  104. /**
  105. * Function to get all the child links
  106. * @result <array> - list of Vtiger_Link_Model instances
  107. */
  108. public function getChildLinks() {
  109. //See if indexing is need depending only user selection
  110. return $this->childlinks;
  111. }
  112. /**
  113. * Function to check whether the link model has any child links
  114. * @return <Boolean> true/false
  115. */
  116. public function hasChild() {
  117. (count($this->childlinks) > 0)? true : false;
  118. }
  119. public function isPageLoadLink() {
  120. $url = $this->get('linkurl');
  121. if(strpos($url, 'index') === 0){
  122. return true;
  123. }
  124. return false;
  125. }
  126. public function convertToNativeLink() {
  127. $url = $this->get('linkurl');
  128. if(empty($url)){
  129. return $url;
  130. }
  131. //Check if the link is not javascript
  132. if(!$this->isPageLoadLink()){
  133. //To convert single quotes and double quotes
  134. $url = Vtiger_Util_Helper::toSafeHTML($url);
  135. return $url;
  136. }
  137. $module = false;
  138. $sourceModule = false;
  139. $sourceRecord = false;
  140. $parametersParts = explode('&',$url);
  141. foreach($parametersParts as $index => $keyValue){
  142. $urlParts = explode('=', $keyValue);
  143. $key = $urlParts[0];
  144. $value = $urlParts[1];
  145. if(strcmp($key, 'module')== 0){
  146. $module = $value;
  147. }
  148. if(strcmp($key,'action')== 0) {
  149. if(strpos($value,'View')) {
  150. $value = str_replace('View', '', $value);
  151. $key = 'view';
  152. }
  153. }
  154. if(strcmp($key, 'return_module')== 0) {
  155. $key = 'sourceModule';
  156. //Indicating that it is an relation operation
  157. $parametersParts[] = 'relationOperation=true';
  158. }
  159. if(strcmp($key, 'return_id')== 0) {
  160. $key = 'sourceRecord';
  161. }
  162. if(strcmp($key, 'sourceRecord') == 0) {
  163. $sourceRecord = $value;
  164. }
  165. if(strcmp($key, 'sourceModule') == 0) {
  166. $sourceModule = $value;
  167. }
  168. $newUrlParts = array();
  169. array_push($newUrlParts, $key);
  170. array_push($newUrlParts, $value);
  171. $parametersParts[$index] = implode('=', $newUrlParts);
  172. }
  173. //to append the reference field in one to many relation
  174. if(!empty($module) && !empty ($sourceModule) && !empty($sourceRecord)) {
  175. $sourceModuleModel = Vtiger_Module_Model::getInstance($sourceModule);
  176. $relatedModuleModel = Vtiger_Module_Model::getInstance($module);
  177. $relationModel = Vtiger_Relation_Model::getInstance($sourceModuleModel, $relatedModuleModel);
  178. if($relationModel->isDirectRelation()){
  179. $fieldList = $relatedModuleModel->getFields();
  180. foreach($fieldList as $fieldName=>$fieldModel) {
  181. if($fieldModel->getFieldDataType() == Vtiger_Field_Model::REFERENCE_TYPE) {
  182. $referenceList = $fieldModel->getReferenceList();
  183. if(in_array($sourceModuleModel->get('name'), $referenceList)) {
  184. $parametersParts[] = $fieldModel->get('name').'='.$sourceRecord;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. $url = implode('&', $parametersParts);
  191. //To convert single quotes and double quotes
  192. $url = Vtiger_Util_Helper::toSafeHTML($url);
  193. return $url;
  194. }
  195. /**
  196. * Function to get the instance of Vtiger Link Model from the given array of key-value mapping
  197. * @param <Array> $valueMap
  198. * @return Vtiger_Link_Model instance
  199. */
  200. public static function getInstanceFromValues($valueMap) {
  201. $linkModel = new self();
  202. $linkModel->initialize($valueMap);
  203. // To set other properties for Link Model
  204. foreach($valueMap as $property => $value) {
  205. if(!isset($linkModel->$property)) {
  206. $linkModel->$property = $value;
  207. }
  208. }
  209. return $linkModel;
  210. }
  211. /**
  212. * Function to get the instance of Vtiger Link Model from a given Vtiger_Link object
  213. * @param Vtiger_Link $linkObj
  214. * @return Vtiger_Link_Model instance
  215. */
  216. public static function getInstanceFromLinkObject (Vtiger_Link $linkObj) {
  217. $objectProperties = get_object_vars($linkObj);
  218. $linkModel = new self();
  219. foreach($objectProperties as $properName=>$propertyValue) {
  220. $linkModel->$properName = $propertyValue;
  221. }
  222. return $linkModel;
  223. }
  224. /**
  225. * Function to get all the Vtiger Link Models for a module of the given list of link types
  226. * @param <Number> $tabid
  227. * @param <Array> $type
  228. * @param <Array> $parameters
  229. * @return <Array> - List of Vtiger_Link_Model instances
  230. */
  231. public static function getAllByType($tabid, $type = false, $parameters = false) {
  232. $links = Vtiger_Cache::get('links-'.$tabid, $type);
  233. if(!$links) {
  234. $links = parent::getAllByType($tabid, $type, $parameters);
  235. Vtiger_Cache::set('links-'.$tabid, $type, $links);
  236. }
  237. $linkModels = array();
  238. foreach($links as $linkType => $linkObjects) {
  239. foreach($linkObjects as $linkObject) {
  240. $linkModels[$linkType][] = self::getInstanceFromLinkObject($linkObject);
  241. }
  242. }
  243. return $linkModels;
  244. }
  245. }