Merge pull request #8006 from yashodha/CRM-18263
[civicrm-core.git] / CRM / Custom / Page / AJAX.php
CommitLineData
57c9c217 1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
57c9c217 5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 *
33 */
34
35/**
36 * This class contains the functions that are called using AJAX (jQuery)
37 */
38class CRM_Custom_Page_AJAX {
9d311c8f 39
57c9c217 40 /**
9d311c8f
CW
41 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
42 * @deprecated
57c9c217 43 */
44 public static function getOptionList() {
45 $params = $_REQUEST;
46
57c9c217 47 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
48 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
49 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
57c9c217 50
51 $params['page'] = ($offset / $rowCount) + 1;
52 $params['rp'] = $rowCount;
53
54 $options = CRM_Core_BAO_CustomOption::getOptionListSelector($params);
55
56 $iFilteredTotal = $iTotal = $params['total'];
57 $selectorElements = array(
58 'label',
59 'value',
60 'is_default',
57c9c217 61 'is_active',
62 'links',
63 'class',
64 );
65
d42a224c 66 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
57c9c217 67 echo CRM_Utils_JSON::encodeDataTableSelector($options, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
68 CRM_Utils_System::civiExit();
69 }
70
46227b8d 71 /**
72 * Fix Ordering of options
73 *
74 */
46227b8d 75 public static function fixOrdering() {
76 $params = $_REQUEST;
77
78 $queryParams = array(
79 1 => array($params['start'], 'Integer'),
80 2 => array($params['end'], 'Integer'),
81 3 => array($params['gid'], 'Integer'),
82 );
83 $dao = "SELECT id FROM civicrm_option_value WHERE weight = %1 AND option_group_id = %3";
84 $startid = CRM_Core_DAO::singleValueQuery($dao, $queryParams);
85
86 $dao2 = "SELECT id FROM civicrm_option_value WHERE weight = %2 AND option_group_id = %3";
87 $endid = CRM_Core_DAO::singleValueQuery($dao2, $queryParams);
88
89 $query = "UPDATE civicrm_option_value SET weight = %2 WHERE id = $startid";
90 CRM_Core_DAO::executeQuery($query, $queryParams);
91
92 // increment or decrement the rest by one
93 if ($params['start'] < $params['end']) {
94 $updateRows = "UPDATE civicrm_option_value
95 SET weight = weight - 1
96 WHERE weight > %1 AND weight < %2 AND option_group_id = %3
97 OR id = $endid";
98 }
99 else {
100 $updateRows = "UPDATE civicrm_option_value
101 SET weight = weight + 1
102 WHERE weight < %1 AND weight > %2 AND option_group_id = %3
103 OR id = $endid";
104 }
105 CRM_Core_DAO::executeQuery($updateRows, $queryParams);
c1cd77e2 106 CRM_Utils_JSON::output(TRUE);
46227b8d 107 }
108
5a2c7bb9 109 /**
110 * Get list of Multi Record Fields.
111 *
112 */
113 public static function getMultiRecordFieldList() {
c693f065 114 $params = $_GET;
5a2c7bb9 115
c693f065 116 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
117 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 10;
118 $sortMapper = array();
119 foreach ($_GET['columns'] as $key => $value) {
120 $sortMapper[$key] = $value['data'];
121 };
122 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
123 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
5a2c7bb9 124
125 $params['page'] = ($offset / $rowCount) + 1;
126 $params['rp'] = $rowCount;
127 $contactType = CRM_Contact_BAO_Contact::getContactType($params['cid']);
128
129 $obj = new CRM_Profile_Page_MultipleRecordFieldsListing();
130 $obj->_pageViewType = 'customDataView';
131 $obj->_contactId = $params['cid'];
132 $obj->_customGroupId = $params['cgid'];
133 $obj->_contactType = $contactType;
c693f065 134 $obj->_DTparams['offset'] = ($params['page'] - 1) * $params['rp'];
135 $obj->_DTparams['rowCount'] = $params['rp'];
136 if ($sort && $sortOrder) {
137 $sort = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $sort, 'column_name', 'label');
138 $obj->_DTparams['sort'] = $sort . ' ' . $sortOrder;
139 }
5a2c7bb9 140
c693f065 141 list($fields, $attributes) = $obj->browse();
5a2c7bb9 142
f5eda27f 143 // format params and add class attributes
c693f065 144 $fieldList = array();
145 foreach ($fields as $id => $value) {
146 $field = array();
d51e02d3 147 foreach ($value as $fieldId => &$fieldName) {
148 if (!empty($attributes[$fieldId][$id]['class'])) {
56a7bb7c 149 $fieldName = array('data' => $fieldName, 'cellClass' => $attributes[$fieldId][$id]['class']);
d51e02d3 150 }
c693f065 151 if (is_numeric($fieldId)) {
d51e02d3 152 $fName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $fieldId, 'label');
153 CRM_Utils_Array::crmReplaceKey($value, $fieldId, $fName);
5a2c7bb9 154 }
155 }
c693f065 156 $field = $value;
157 array_push($fieldList, $field);
5a2c7bb9 158 }
f5eda27f 159 $totalRecords = !empty($obj->_total) ? $obj->_total : 0;
5a2c7bb9 160
c693f065 161 $multiRecordFields = array();
162 $multiRecordFields['data'] = $fieldList;
f5eda27f 163 $multiRecordFields['recordsTotal'] = $totalRecords;
164 $multiRecordFields['recordsFiltered'] = $totalRecords;
5a2c7bb9 165
c693f065 166 CRM_Utils_JSON::output($multiRecordFields);
5a2c7bb9 167 }
168
57c9c217 169}