PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vtlib/Vtiger/Link.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 284 lines | 174 code | 22 blank | 88 comment | 26 complexity | e59f4ac022b09f043e99d7f9bcb920bc 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/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. /* vtiger_links is already core product table */
  75. /*if(empty(self::$__cacheSchemaChanges['vtiger_links'])) {
  76. if(!Vtiger_Utils::CheckTable('vtiger_links')) {
  77. Vtiger_Utils::CreateTable(
  78. 'vtiger_links',
  79. '(linkid INT NOT NULL PRIMARY KEY,
  80. tabid INT, linktype VARCHAR(20), linklabel VARCHAR(30), linkurl VARCHAR(255), linkicon VARCHAR(100), sequence INT, status INT(1) NOT NULL DEFAULT 1)',
  81. true);
  82. Vtiger_Utils::ExecuteQuery(
  83. 'CREATE INDEX link_tabidtype_idx on vtiger_links(tabid,linktype)');
  84. }
  85. self::$__cacheSchemaChanges['vtiger_links'] = true;
  86. }*/
  87. }
  88. /**
  89. * Add link given module
  90. * @param Integer Module ID
  91. * @param String Link Type (like DETAILVIEW). Useful for grouping based on pages.
  92. * @param String Label to display
  93. * @param String HREF value or URL to use for the link
  94. * @param String ICON to use on the display
  95. * @param Integer Order or sequence of displaying the link
  96. */
  97. static function addLink($tabid, $type, $label, $url, $iconpath='',$sequence=0, $handlerInfo=null) {
  98. global $adb;
  99. self::__initSchema();
  100. $checkres = $adb->pquery('SELECT linkid FROM vtiger_links WHERE tabid=? AND linktype=? AND linkurl=? AND linkicon=? AND linklabel=?',
  101. Array($tabid, $type, $url, $iconpath, $label));
  102. if(!$adb->num_rows($checkres)) {
  103. $uniqueid = self::__getUniqueId();
  104. $sql = 'INSERT INTO vtiger_links (linkid,tabid,linktype,linklabel,linkurl,linkicon,'.
  105. 'sequence';
  106. $params = Array($uniqueid, $tabid, $type, $label, $url, $iconpath, $sequence);
  107. if(!empty($handlerInfo)) {
  108. $sql .= (', handler_path, handler_class, handler');
  109. $params[] = $handlerInfo['path'];
  110. $params[] = $handlerInfo['class'];
  111. $params[] = $handlerInfo['method'];
  112. }
  113. $sql .= (') VALUES ('.generateQuestionMarks($params).')');
  114. $adb->pquery($sql, $params);
  115. self::log("Adding Link ($type - $label) ... DONE");
  116. }
  117. }
  118. /**
  119. * Delete link of the module
  120. * @param Integer Module ID
  121. * @param String Link Type (like DETAILVIEW). Useful for grouping based on pages.
  122. * @param String Display label
  123. * @param String URL of link to lookup while deleting
  124. */
  125. static function deleteLink($tabid, $type, $label, $url=false) {
  126. global $adb;
  127. self::__initSchema();
  128. if($url) {
  129. $adb->pquery('DELETE FROM vtiger_links WHERE tabid=? AND linktype=? AND linklabel=? AND linkurl=?',
  130. Array($tabid, $type, $label, $url));
  131. self::log("Deleting Link ($type - $label - $url) ... DONE");
  132. } else {
  133. $adb->pquery('DELETE FROM vtiger_links WHERE tabid=? AND linktype=? AND linklabel=?',
  134. Array($tabid, $type, $label));
  135. self::log("Deleting Link ($type - $label) ... DONE");
  136. }
  137. }
  138. /**
  139. * Delete all links related to module
  140. * @param Integer Module ID.
  141. */
  142. static function deleteAll($tabid) {
  143. global $adb;
  144. self::__initSchema();
  145. $adb->pquery('DELETE FROM vtiger_links WHERE tabid=?', Array($tabid));
  146. self::log("Deleting Links ... DONE");
  147. }
  148. /**
  149. * Get all the links related to module
  150. * @param Integer Module ID.
  151. */
  152. static function getAll($tabid) {
  153. return self::getAllByType($tabid);
  154. }
  155. /**
  156. * Get all the link related to module based on type
  157. * @param Integer Module ID
  158. * @param mixed String or List of types to select
  159. * @param Map Key-Value pair to use for formating the link url
  160. */
  161. static function getAllByType($tabid, $type=false, $parameters=false) {
  162. global $adb, $current_user;
  163. self::__initSchema();
  164. $multitype = false;
  165. if($type) {
  166. // Multiple link type selection?
  167. if(is_array($type)) {
  168. $multitype = true;
  169. if($tabid === self::IGNORE_MODULE) {
  170. $sql = 'SELECT * FROM vtiger_links WHERE linktype IN ('.
  171. Vtiger_Utils::implodestr('?', count($type), ',') .') ';
  172. $params = $type;
  173. $permittedTabIdList = getPermittedModuleIdList();
  174. if(count($permittedTabIdList) > 0 && $current_user->is_admin !== 'on') {
  175. $sql .= ' and tabid IN ('.
  176. Vtiger_Utils::implodestr('?', count($permittedTabIdList), ',').')';
  177. $params[] = $permittedTabIdList;
  178. }
  179. $result = $adb->pquery($sql, Array($adb->flatten_array($params)));
  180. } else {
  181. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=? AND linktype IN ('.
  182. Vtiger_Utils::implodestr('?', count($type), ',') .')',
  183. Array($tabid, $adb->flatten_array($type)));
  184. }
  185. } else {
  186. // Single link type selection
  187. if($tabid === self::IGNORE_MODULE) {
  188. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE linktype=?', Array($type));
  189. } else {
  190. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=? AND linktype=?', Array($tabid, $type));
  191. }
  192. }
  193. } else {
  194. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=?', Array($tabid));
  195. }
  196. $strtemplate = new Vtiger_StringTemplate();
  197. if($parameters) {
  198. foreach($parameters as $key=>$value) $strtemplate->assign($key, $value);
  199. }
  200. $instances = Array();
  201. if($multitype) {
  202. foreach($type as $t) $instances[$t] = Array();
  203. }
  204. while($row = $adb->fetch_array($result)){
  205. $instance = new self();
  206. $instance->initialize($row);
  207. if(!empty($row['handler_path']) && isFileAccessible($row['handler_path'])) {
  208. checkFileAccessForInclusion($row['handler_path']);
  209. require_once $row['handler_path'];
  210. $linkData = new Vtiger_LinkData($instance, $current_user);
  211. $ignore = call_user_func(array($row['handler_class'], $row['handler']), $linkData);
  212. if(!$ignore) {
  213. self::log("Ignoring Link ... ".var_export($row, true));
  214. continue;
  215. }
  216. }
  217. if($parameters) {
  218. $instance->linkurl = $strtemplate->merge($instance->linkurl);
  219. $instance->linkicon= $strtemplate->merge($instance->linkicon);
  220. }
  221. if($multitype) {
  222. $instances[$instance->linktype][] = $instance;
  223. } else {
  224. $instances[] = $instance;
  225. }
  226. }
  227. return $instances;
  228. }
  229. /**
  230. * Extract the links of module for export.
  231. */
  232. static function getAllForExport($tabid) {
  233. global $adb;
  234. $result = $adb->pquery('SELECT * FROM vtiger_links WHERE tabid=?', array($tabid));
  235. $links = array();
  236. while($row = $adb->fetch_array($result)) {
  237. $instance = new self();
  238. $instance->initialize($row);
  239. $links[] = $instance;
  240. }
  241. return $links;
  242. }
  243. /**
  244. * Helper function to log messages
  245. * @param String Message to log
  246. * @param Boolean true appends linebreak, false to avoid it
  247. * @access private
  248. */
  249. static function log($message, $delimit=true) {
  250. Vtiger_Utils::Log($message, $delimit);
  251. }
  252. /**
  253. * Checks whether the user is admin or not
  254. * @param Vtiger_LinkData $linkData
  255. * @return Boolean
  256. */
  257. static function isAdmin($linkData) {
  258. $user = $linkData->getUser();
  259. return $user->is_admin == 'on' || $user->column_fields['is_admin'] == 'on';
  260. }
  261. }
  262. ?>