PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vtlib/Vtiger/Link.php

https://bitbucket.org/thomashii/vtigercrm-5.4-for-postgresql
PHP | 267 lines | 174 code | 21 blank | 72 comment | 28 complexity | 9a3328e90376437fa8c319257890e6f8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.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/Utils.php');
  11. include_once('vtlib/Vtiger/Utils/StringTemplate.php');
  12. include_once 'vtlib/Vtiger/LinkData.php';
  13. /**
  14. * Provides API to handle custom links
  15. * @package vtlib
  16. */
  17. class Vtiger_Link {
  18. var $tabid;
  19. var $linkid;
  20. var $linktype;
  21. var $linklabel;
  22. var $linkurl;
  23. var $linkicon;
  24. var $sequence;
  25. var $status = false;
  26. var $handler_path;
  27. var $handler_class;
  28. var $handler;
  29. // Ignore module while selection
  30. const IGNORE_MODULE = -1;
  31. /**
  32. * Constructor
  33. */
  34. function __construct() {
  35. }
  36. /**
  37. * Initialize this instance.
  38. */
  39. function initialize($valuemap) {
  40. $this->tabid = $valuemap['tabid'];
  41. $this->linkid = $valuemap['linkid'];
  42. $this->linktype=$valuemap['linktype'];
  43. $this->linklabel=$valuemap['linklabel'];
  44. $this->linkurl =decode_html($valuemap['linkurl']);
  45. $this->linkicon =decode_html($valuemap['linkicon']);
  46. $this->sequence =$valuemap['sequence'];
  47. $this->status =$valuemap['status'];
  48. $this->handler_path =$valuemap['handler_path'];
  49. $this->handler_class=$valuemap['handler_class'];
  50. $this->handler =$valuemap['handler'];
  51. }
  52. /**
  53. * Get module name.
  54. */
  55. function module() {
  56. if(!empty($this->tabid)) {
  57. return getTabModuleName($this->tabid);
  58. }
  59. return false;
  60. }
  61. /**
  62. * Get unique id for the insertion
  63. */
  64. static function __getUniqueId() {
  65. global $adb;
  66. return $adb->getUniqueID('vtiger_links');
  67. }
  68. /** Cache (Record) the schema changes to improve performance */
  69. static $__cacheSchemaChanges = Array();
  70. /**
  71. * Initialize the schema (tables)
  72. */
  73. static function __initSchema() {
  74. if(empty(self::$__cacheSchemaChanges['vtiger_links'])) {
  75. if(!Vtiger_Utils::CheckTable('vtiger_links')) {
  76. Vtiger_Utils::CreateTable(
  77. 'vtiger_links',
  78. '(linkid INT NOT NULL PRIMARY KEY,
  79. tabid INT, linktype VARCHAR(20), linklabel VARCHAR(30), linkurl VARCHAR(255), linkicon VARCHAR(100), sequence INT, status INT(1) NOT NULL DEFAULT 1)',
  80. true);
  81. Vtiger_Utils::ExecuteQuery(
  82. 'CREATE INDEX link_tabidtype_idx on vtiger_links(tabid,linktype)');
  83. }
  84. self::$__cacheSchemaChanges['vtiger_links'] = true;
  85. }
  86. }
  87. /**
  88. * Add link given module
  89. * @param Integer Module ID
  90. * @param String Link Type (like DETAILVIEW). Useful for grouping based on pages.
  91. * @param String Label to display
  92. * @param String HREF value or URL to use for the link
  93. * @param String ICON to use on the display
  94. * @param Integer Order or sequence of displaying the link
  95. */
  96. static function addLink($tabid, $type, $label, $url, $iconpath='',$sequence=0, $handlerInfo=null) {
  97. global $adb;
  98. self::__initSchema();
  99. $checkres = $adb->pquery('SELECT linkid FROM vtiger_links WHERE tabid=? AND linktype=? AND linkurl=? AND linkicon=? AND linklabel=?',
  100. Array($tabid, $type, $url, $iconpath, $label));
  101. if(!$adb->num_rows($checkres)) {
  102. $uniqueid = self::__getUniqueId();
  103. $sql = 'INSERT INTO vtiger_links (linkid,tabid,linktype,linklabel,linkurl,linkicon,'.
  104. 'sequence';
  105. $params = Array($uniqueid, $tabid, $type, $label, $url, $iconpath, $sequence);
  106. if(!empty($handlerInfo)) {
  107. $sql .= (', handler_path, handler_class, handler');
  108. $params[] = $handlerInfo['path'];
  109. $params[] = $handlerInfo['class'];
  110. $params[] = $handlerInfo['method'];
  111. }
  112. $sql .= (') VALUES ('.generateQuestionMarks($params).')');
  113. $adb->pquery($sql, $params);
  114. self::log("Adding Link ($type - $label) ... DONE");
  115. }
  116. }
  117. /**
  118. * Delete link of the module
  119. * @param Integer Module ID
  120. * @param String Link Type (like DETAILVIEW). Useful for grouping based on pages.
  121. * @param String Display label
  122. * @param String URL of link to lookup while deleting
  123. */
  124. static function deleteLink($tabid, $type, $label, $url=false) {
  125. global $adb;
  126. self::__initSchema();
  127. if($url) {
  128. $adb->pquery('DELETE FROM vtiger_links WHERE tabid=? AND linktype=? AND linklabel=? AND linkurl=?',
  129. Array($tabid, $type, $label, $url));
  130. self::log("Deleting Link ($type - $label - $url) ... DONE");
  131. } else {
  132. $adb->pquery('DELETE FROM vtiger_links WHERE tabid=? AND linktype=? AND linklabel=?',
  133. Array($tabid, $type, $label));
  134. self::log("Deleting Link ($type - $label) ... DONE");
  135. }
  136. }
  137. /**
  138. * Delete all links related to module
  139. * @param Integer Module ID.
  140. */
  141. static function deleteAll($tabid) {
  142. global $adb;
  143. self::__initSchema();
  144. $adb->pquery('DELETE FROM vtiger_links WHERE tabid=?', Array($tabid));
  145. self::log("Deleting Links ... DONE");
  146. }
  147. /**
  148. * Get all the links related to module
  149. * @param Integer Module ID.
  150. */
  151. static function getAll($tabid) {
  152. return self::getAllByType($tabid);
  153. }
  154. /**
  155. * Get all the link related to module based on type
  156. * @param Integer Module ID
  157. * @param mixed String or List of types to select
  158. * @param Map Key-Value pair to use for formating the link url
  159. */
  160. static function getAllByType($tabid, $type=false, $parameters=false) {
  161. global $adb, $current_user;
  162. self::__initSchema();
  163. $multitype = false;
  164. if($type) {
  165. // Multiple link type selection?
  166. if(is_array($type)) {
  167. $multitype = true;
  168. if($tabid === self::IGNORE_MODULE) {
  169. $sql = 'SELECT * FROM vtiger_links WHERE linktype IN ('.
  170. Vtiger_Utils::implodestr('?', count($type), ',') .') ';
  171. $params = $type;
  172. $permittedTabIdList = getPermittedModuleIdList();
  173. if(count($permittedTabIdList) > 0 && $current_user->is_admin !== 'on') {
  174. $sql .= ' and tabid IN ('.
  175. Vtiger_Utils::implodestr('?', count($permittedTabIdList), ',').')';
  176. $params[] = $permittedTabIdList;
  177. }
  178. $result = $adb->pquery($sql, Array($adb->flatten_array($params)));
  179. } else {
  180. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=? AND linktype IN ('.
  181. Vtiger_Utils::implodestr('?', count($type), ',') .')',
  182. Array($tabid, $adb->flatten_array($type)));
  183. }
  184. } else {
  185. // Single link type selection
  186. if($tabid === self::IGNORE_MODULE) {
  187. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE linktype=?', Array($type));
  188. } else {
  189. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=? AND linktype=?', Array($tabid, $type));
  190. }
  191. }
  192. } else {
  193. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=?', Array($tabid));
  194. }
  195. $strtemplate = new Vtiger_StringTemplate();
  196. if($parameters) {
  197. foreach($parameters as $key=>$value) $strtemplate->assign($key, $value);
  198. }
  199. $instances = Array();
  200. if($multitype) {
  201. foreach($type as $t) $instances[$t] = Array();
  202. }
  203. while($row = $adb->fetch_array($result)){
  204. $instance = new self();
  205. $instance->initialize($row);
  206. if(!empty($row['handler_path']) && isFileAccessible($row['handler_path'])) {
  207. require_once $row['handler_path'];
  208. $linkData = new Vtiger_LinkData($instance, $current_user);
  209. $ignore = call_user_func(array($row['handler_class'], $row['handler']), $linkData);
  210. if(!$ignore) {
  211. self::log("Ignoring Link ... ".var_export($row, true));
  212. continue;
  213. }
  214. }
  215. if($parameters) {
  216. $instance->linkurl = $strtemplate->merge($instance->linkurl);
  217. $instance->linkicon= $strtemplate->merge($instance->linkicon);
  218. }
  219. if($multitype) {
  220. $instances[$instance->linktype][] = $instance;
  221. } else {
  222. $instances[] = $instance;
  223. }
  224. }
  225. return $instances;
  226. }
  227. /**
  228. * Helper function to log messages
  229. * @param String Message to log
  230. * @param Boolean true appends linebreak, false to avoid it
  231. * @access private
  232. */
  233. static function log($message, $delimit=true) {
  234. Vtiger_Utils::Log($message, $delimit);
  235. }
  236. /**
  237. * Checks whether the user is admin or not
  238. * @param Vtiger_LinkData $linkData
  239. * @return Boolean
  240. */
  241. static function isAdmin($linkData) {
  242. $user = $linkData->getUser();
  243. return $user->is_admin == 'on' || $user->column_fields['is_admin'] == 'on';
  244. }
  245. }
  246. ?>