PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/sites/all/modules/contrib/civicrm/CRM/Pledge/Selector/Search.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 466 lines | 236 code | 45 blank | 185 comment | 12 complexity | 48250a7cee345a9dbc2fe98fc1effab1 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2019
  31. */
  32. /**
  33. * This class is used to retrieve and display a range of
  34. * contacts that match the given criteria (specifically for
  35. * results of advanced search options.
  36. */
  37. class CRM_Pledge_Selector_Search extends CRM_Core_Selector_Base {
  38. /**
  39. * This defines two actions- View and Edit.
  40. *
  41. * @var array
  42. */
  43. public static $_links = NULL;
  44. /**
  45. * We use desc to remind us what that column is, name is used in the tpl
  46. *
  47. * @var array
  48. */
  49. public static $_columnHeaders;
  50. /**
  51. * Properties of contact we're interested in displaying
  52. *
  53. * @var array
  54. */
  55. public static $_properties = [
  56. 'contact_id',
  57. 'sort_name',
  58. 'display_name',
  59. 'pledge_id',
  60. 'pledge_amount',
  61. 'pledge_create_date',
  62. 'pledge_total_paid',
  63. 'pledge_next_pay_date',
  64. 'pledge_next_pay_amount',
  65. 'pledge_outstanding_amount',
  66. 'pledge_status_id',
  67. 'pledge_status',
  68. 'pledge_is_test',
  69. 'pledge_contribution_page_id',
  70. 'pledge_financial_type',
  71. 'pledge_campaign_id',
  72. 'pledge_currency',
  73. ];
  74. /**
  75. * Are we restricting ourselves to a single contact
  76. *
  77. * @var bool
  78. */
  79. protected $_single = FALSE;
  80. /**
  81. * Are we restricting ourselves to a single contact
  82. *
  83. * @var bool
  84. */
  85. protected $_limit = NULL;
  86. /**
  87. * What context are we being invoked from
  88. *
  89. * @var string
  90. */
  91. protected $_context = NULL;
  92. /**
  93. * QueryParams is the array returned by exportValues called on
  94. * the HTML_QuickForm_Controller for that page.
  95. *
  96. * @var array
  97. */
  98. public $_queryParams;
  99. /**
  100. * Represent the type of selector
  101. *
  102. * @var int
  103. */
  104. protected $_action;
  105. /**
  106. * The additional clause that we restrict the search with
  107. *
  108. * @var string
  109. */
  110. protected $_additionalClause = NULL;
  111. /**
  112. * The query object
  113. *
  114. * @var string
  115. */
  116. protected $_query;
  117. /**
  118. * Class constructor.
  119. *
  120. * @param array $queryParams
  121. * Array of parameters for query.
  122. * @param \const|int $action - action of search basic or advanced.
  123. * @param string $additionalClause
  124. * If the caller wants to further restrict the search (used in participations).
  125. * @param bool $single
  126. * Are we dealing only with one contact?.
  127. * @param int $limit
  128. * How many signers do we want returned.
  129. *
  130. * @param string $context
  131. *
  132. * @return \CRM_Pledge_Selector_Search
  133. */
  134. public function __construct(
  135. &$queryParams,
  136. $action = CRM_Core_Action::NONE,
  137. $additionalClause = NULL,
  138. $single = FALSE,
  139. $limit = NULL,
  140. $context = 'search'
  141. ) {
  142. // submitted form values
  143. $this->_queryParams = &$queryParams;
  144. $this->_single = $single;
  145. $this->_limit = $limit;
  146. $this->_context = $context;
  147. $this->_additionalClause = $additionalClause;
  148. // type of selector
  149. $this->_action = $action;
  150. $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
  151. CRM_Contact_BAO_Query::MODE_PLEDGE
  152. );
  153. $this->_query->_distinctComponentClause = "civicrm_pledge.id";
  154. $this->_query->_groupByComponentClause = " GROUP BY civicrm_pledge.id ";
  155. }
  156. /**
  157. * This method returns the links that are given for each search row.
  158. *
  159. * Currently the links added for each row are:
  160. * - View
  161. * - Edit
  162. *
  163. * @return array
  164. */
  165. public static function &links() {
  166. $args = func_get_args();
  167. $hideOption = CRM_Utils_Array::value(0, $args);
  168. $key = CRM_Utils_Array::value(1, $args);
  169. $extraParams = ($key) ? "&key={$key}" : NULL;
  170. $cancelExtra = ts('Cancelling this pledge will also cancel any scheduled (and not completed) pledge payments.') . ' ' . ts('This action cannot be undone.') . ' ' . ts('Do you want to continue?');
  171. self::$_links = [
  172. CRM_Core_Action::VIEW => [
  173. 'name' => ts('View'),
  174. 'url' => 'civicrm/contact/view/pledge',
  175. 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams,
  176. 'title' => ts('View Pledge'),
  177. ],
  178. CRM_Core_Action::UPDATE => [
  179. 'name' => ts('Edit'),
  180. 'url' => 'civicrm/contact/view/pledge',
  181. 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
  182. 'title' => ts('Edit Pledge'),
  183. ],
  184. CRM_Core_Action::DETACH => [
  185. 'name' => ts('Cancel'),
  186. 'url' => 'civicrm/contact/view/pledge',
  187. 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
  188. 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"',
  189. 'title' => ts('Cancel Pledge'),
  190. ],
  191. CRM_Core_Action::DELETE => [
  192. 'name' => ts('Delete'),
  193. 'url' => 'civicrm/contact/view/pledge',
  194. 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
  195. 'title' => ts('Delete Pledge'),
  196. ],
  197. ];
  198. if (in_array('Cancel', $hideOption)) {
  199. unset(self::$_links[CRM_Core_Action::DETACH]);
  200. }
  201. return self::$_links;
  202. }
  203. /**
  204. * Getter for array of the parameters required for creating pager.
  205. *
  206. * @param $action
  207. * @param array $params
  208. */
  209. public function getPagerParams($action, &$params) {
  210. $params['status'] = ts('Pledge') . ' %%StatusMessage%%';
  211. $params['csvString'] = NULL;
  212. if ($this->_limit) {
  213. $params['rowCount'] = $this->_limit;
  214. }
  215. else {
  216. $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
  217. }
  218. $params['buttonTop'] = 'PagerTopButton';
  219. $params['buttonBottom'] = 'PagerBottomButton';
  220. }
  221. /**
  222. * Returns total number of rows for the query.
  223. *
  224. * @param int $action
  225. *
  226. * @return int
  227. * Total number of rows
  228. */
  229. public function getTotalCount($action) {
  230. return $this->_query->searchQuery(0, 0, NULL,
  231. TRUE, FALSE,
  232. FALSE, FALSE,
  233. FALSE,
  234. $this->_additionalClause
  235. );
  236. }
  237. /**
  238. * Returns all the rows in the given offset and rowCount.
  239. *
  240. * @param string $action
  241. * The action being performed.
  242. * @param int $offset
  243. * The row number to start from.
  244. * @param int $rowCount
  245. * The number of rows to return.
  246. * @param string $sort
  247. * The sql string that describes the sort order.
  248. * @param string $output
  249. * What should the result set include (web/email/csv).
  250. *
  251. * @return int
  252. * the total number of rows for this action
  253. */
  254. public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
  255. $result = $this->_query->searchQuery($offset, $rowCount, $sort,
  256. FALSE, FALSE,
  257. FALSE, FALSE,
  258. FALSE,
  259. $this->_additionalClause
  260. );
  261. // process the result of the query
  262. $rows = [];
  263. // get all pledge status
  264. $pledgeStatuses = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
  265. // get all campaigns.
  266. $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
  267. // CRM-4418 check for view, edit and delete
  268. $permissions = [CRM_Core_Permission::VIEW];
  269. if (CRM_Core_Permission::check('edit pledges')) {
  270. $permissions[] = CRM_Core_Permission::EDIT;
  271. }
  272. if (CRM_Core_Permission::check('delete in CiviPledge')) {
  273. $permissions[] = CRM_Core_Permission::DELETE;
  274. }
  275. $mask = CRM_Core_Action::mask($permissions);
  276. while ($result->fetch()) {
  277. $row = [];
  278. // the columns we are interested in
  279. foreach (self::$_properties as $property) {
  280. if (isset($result->$property)) {
  281. $row[$property] = $result->$property;
  282. }
  283. }
  284. // carry campaign on selectors.
  285. $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns);
  286. $row['campaign_id'] = $result->pledge_campaign_id;
  287. // add pledge status name
  288. if (!empty($row['pledge_status_id'])) {
  289. $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'],
  290. $pledgeStatuses
  291. );
  292. }
  293. // append (test) to status label
  294. if (!empty($row['pledge_is_test'])) {
  295. $row['pledge_status'] = CRM_Core_TestEntity::appendTestText($row['pledge_status']);
  296. }
  297. $hideOption = [];
  298. if (CRM_Utils_Array::key('Cancelled', $row) ||
  299. CRM_Utils_Array::key('Completed', $row)
  300. ) {
  301. $hideOption[] = 'Cancel';
  302. }
  303. $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id;
  304. $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key),
  305. $mask,
  306. [
  307. 'id' => $result->pledge_id,
  308. 'cid' => $result->contact_id,
  309. 'cxt' => $this->_context,
  310. ],
  311. ts('more'),
  312. FALSE,
  313. 'pledge.selector.row',
  314. 'Pledge',
  315. $result->pledge_id
  316. );
  317. $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
  318. );
  319. $rows[] = $row;
  320. }
  321. return $rows;
  322. }
  323. /**
  324. * Get qill (display what was searched on).
  325. *
  326. * @inheritDoc
  327. */
  328. public function getQILL() {
  329. return $this->_query->qill();
  330. }
  331. /**
  332. * Returns the column headers as an array of tuples.
  333. *
  334. * Keys are name, sortName, key to the sort array
  335. *
  336. * @param string $action
  337. * The action being performed.
  338. * @param string $output
  339. * What should the result set include (web/email/csv).
  340. *
  341. * @return array
  342. * the column headers that need to be displayed
  343. */
  344. public function &getColumnHeaders($action = NULL, $output = NULL) {
  345. if (!isset(self::$_columnHeaders)) {
  346. self::$_columnHeaders = [
  347. [
  348. 'name' => ts('Pledged'),
  349. 'sort' => 'pledge_amount',
  350. 'direction' => CRM_Utils_Sort::DONTCARE,
  351. ],
  352. [
  353. 'name' => ts('Total Paid'),
  354. 'sort' => 'pledge_total_paid',
  355. 'direction' => CRM_Utils_Sort::DONTCARE,
  356. ],
  357. [
  358. 'name' => ts('Balance'),
  359. ],
  360. [
  361. 'name' => ts('Pledged For'),
  362. 'sort' => 'pledge_financial_type',
  363. 'direction' => CRM_Utils_Sort::DONTCARE,
  364. ],
  365. [
  366. 'name' => ts('Pledge Made'),
  367. 'sort' => 'pledge_create_date',
  368. 'direction' => CRM_Utils_Sort::DESCENDING,
  369. ],
  370. [
  371. 'name' => ts('Next Pay Date'),
  372. 'sort' => 'pledge_next_pay_date',
  373. 'direction' => CRM_Utils_Sort::DONTCARE,
  374. ],
  375. [
  376. 'name' => ts('Next Amount'),
  377. 'sort' => 'pledge_next_pay_amount',
  378. 'direction' => CRM_Utils_Sort::DONTCARE,
  379. ],
  380. [
  381. 'name' => ts('Status'),
  382. 'sort' => 'pledge_status',
  383. 'direction' => CRM_Utils_Sort::DONTCARE,
  384. ],
  385. ['desc' => ts('Actions')],
  386. ];
  387. if (!$this->_single) {
  388. $pre = [
  389. ['desc' => ts('Contact ID')],
  390. [
  391. 'name' => ts('Name'),
  392. 'sort' => 'sort_name',
  393. 'direction' => CRM_Utils_Sort::DONTCARE,
  394. ],
  395. ];
  396. self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
  397. }
  398. }
  399. return self::$_columnHeaders;
  400. }
  401. /**
  402. * Get sql query string.
  403. *
  404. * @return string
  405. */
  406. public function &getQuery() {
  407. return $this->_query;
  408. }
  409. /**
  410. * Name of export file.
  411. *
  412. * @param string $output
  413. * Type of output.
  414. *
  415. * @return string
  416. * name of the file
  417. */
  418. public function getExportFileName($output = 'csv') {
  419. return ts('Pledge Search');
  420. }
  421. }