PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vtiger6/modules/Vtiger/models/Link.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 259 lines | 143 code | 29 blank | 87 comment | 22 complexity | dceb590ed6f9fc858e457490a0cf24b2 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 dirname(__FILE__).'/../../../../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. return $url;
  134. }
  135. $module = false;
  136. $sourceModule = false;
  137. $sourceRecord = false;
  138. $parametersParts = explode('&',$url);
  139. foreach($parametersParts as $index => $keyValue){
  140. $urlParts = explode('=', $keyValue);
  141. $key = $urlParts[0];
  142. $value = $urlParts[1];
  143. if(strcmp($key, 'module')== 0){
  144. $module = $value;
  145. }
  146. if(strcmp($key,'action')== 0) {
  147. if(strpos($value,'View')) {
  148. $value = str_replace('View', '', $value);
  149. $key = 'view';
  150. }
  151. }
  152. if(strcmp($key, 'return_module')== 0) {
  153. $key = 'sourceModule';
  154. //Indicating that it is an relation operation
  155. $parametersParts[] = 'relationOperation=true';
  156. }
  157. if(strcmp($key, 'return_id')== 0) {
  158. $key = 'sourceRecord';
  159. }
  160. if(strcmp($key, 'sourceRecord') == 0) {
  161. $sourceRecord = $value;
  162. }
  163. if(strcmp($key, 'sourceModule') == 0) {
  164. $sourceModule = $value;
  165. }
  166. $newUrlParts = array();
  167. array_push($newUrlParts, $key);
  168. array_push($newUrlParts, $value);
  169. $parametersParts[$index] = implode('=', $newUrlParts);
  170. }
  171. //to append the reference field in one to many relation
  172. if(!empty($module) && !empty ($sourceModule) && !empty($sourceRecord)) {
  173. $sourceModuleModel = Vtiger_Module_Model::getInstance($sourceModule);
  174. $relatedModuleModel = Vtiger_Module_Model::getInstance($module);
  175. $relationModel = Vtiger_Relation_Model::getInstance($sourceModuleModel, $relatedModuleModel);
  176. if($relationModel->isDirectRelation()){
  177. $fieldList = $relatedModuleModel->getFields();
  178. foreach($fieldList as $fieldName=>$fieldModel) {
  179. if($fieldModel->getFieldDataType() == Vtiger_Field_Model::REFERENCE_TYPE) {
  180. $referenceList = $fieldModel->getReferenceList();
  181. if(in_array($sourceModuleModel->get('name'), $referenceList)) {
  182. $parametersParts[] = $fieldModel->get('name').'='.$sourceRecord;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. return implode('&', $parametersParts);
  189. }
  190. /**
  191. * Function to get the instance of Vtiger Link Model from the given array of key-value mapping
  192. * @param <Array> $valueMap
  193. * @return Vtiger_Link_Model instance
  194. */
  195. public static function getInstanceFromValues($valueMap) {
  196. $linkModel = new self();
  197. $linkModel->initialize($valueMap);
  198. return $linkModel;
  199. }
  200. /**
  201. * Function to get the instance of Vtiger Link Model from a given Vtiger_Link object
  202. * @param Vtiger_Link $linkObj
  203. * @return Vtiger_Link_Model instance
  204. */
  205. public static function getInstanceFromLinkObject (Vtiger_Link $linkObj) {
  206. $objectProperties = get_object_vars($linkObj);
  207. $linkModel = new self();
  208. foreach($objectProperties as $properName=>$propertyValue) {
  209. $linkModel->$properName = $propertyValue;
  210. }
  211. return $linkModel;
  212. }
  213. /**
  214. * Function to get all the Vtiger Link Models for a module of the given list of link types
  215. * @param <Number> $tabid
  216. * @param <Array> $type
  217. * @param <Array> $parameters
  218. * @return <Array> - List of Vtiger_Link_Model instances
  219. */
  220. public static function getAllByType($tabid, $type = false, $parameters = false) {
  221. $links = parent::getAllByType($tabid, $type, $parameters);
  222. $linkModels = array();
  223. foreach($links as $linkType => $linkObjects) {
  224. foreach($linkObjects as $linkObject) {
  225. $linkModels[$linkType][] = self::getInstanceFromLinkObject($linkObject);
  226. }
  227. }
  228. return $linkModels;
  229. }
  230. }