move aaSorting to markup and few fixes
[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 {
39 /**
40 * Get list of options.
41 *
42 */
43 public static function getOptionList() {
44 $params = $_REQUEST;
45
57c9c217 46 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
47 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
48 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
57c9c217 49
50 $params['page'] = ($offset / $rowCount) + 1;
51 $params['rp'] = $rowCount;
52
53 $options = CRM_Core_BAO_CustomOption::getOptionListSelector($params);
54
55 $iFilteredTotal = $iTotal = $params['total'];
56 $selectorElements = array(
57 'label',
58 'value',
59 'is_default',
57c9c217 60 'is_active',
61 'links',
62 'class',
63 );
64
d42a224c 65 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
57c9c217 66 echo CRM_Utils_JSON::encodeDataTableSelector($options, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
67 CRM_Utils_System::civiExit();
68 }
69
46227b8d 70 /**
71 * Fix Ordering of options
72 *
73 */
46227b8d 74 public static function fixOrdering() {
75 $params = $_REQUEST;
76
77 $queryParams = array(
78 1 => array($params['start'], 'Integer'),
79 2 => array($params['end'], 'Integer'),
80 3 => array($params['gid'], 'Integer'),
81 );
82 $dao = "SELECT id FROM civicrm_option_value WHERE weight = %1 AND option_group_id = %3";
83 $startid = CRM_Core_DAO::singleValueQuery($dao, $queryParams);
84
85 $dao2 = "SELECT id FROM civicrm_option_value WHERE weight = %2 AND option_group_id = %3";
86 $endid = CRM_Core_DAO::singleValueQuery($dao2, $queryParams);
87
88 $query = "UPDATE civicrm_option_value SET weight = %2 WHERE id = $startid";
89 CRM_Core_DAO::executeQuery($query, $queryParams);
90
91 // increment or decrement the rest by one
92 if ($params['start'] < $params['end']) {
93 $updateRows = "UPDATE civicrm_option_value
94 SET weight = weight - 1
95 WHERE weight > %1 AND weight < %2 AND option_group_id = %3
96 OR id = $endid";
97 }
98 else {
99 $updateRows = "UPDATE civicrm_option_value
100 SET weight = weight + 1
101 WHERE weight < %1 AND weight > %2 AND option_group_id = %3
102 OR id = $endid";
103 }
104 CRM_Core_DAO::executeQuery($updateRows, $queryParams);
c1cd77e2 105 CRM_Utils_JSON::output(TRUE);
46227b8d 106 }
107
5a2c7bb9 108 /**
109 * Get list of Multi Record Fields.
110 *
111 */
112 public static function getMultiRecordFieldList() {
c693f065 113 $params = $_GET;
5a2c7bb9 114
c693f065 115 $offset = isset($_GET['start']) ? CRM_Utils_Type::escape($_GET['start'], 'Integer') : 0;
116 $rowCount = isset($_GET['length']) ? CRM_Utils_Type::escape($_GET['length'], 'Integer') : 10;
117 $sortMapper = array();
118 foreach ($_GET['columns'] as $key => $value) {
119 $sortMapper[$key] = $value['data'];
120 };
121 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
122 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type::escape($_GET['order'][0]['dir'], 'String') : 'asc';
5a2c7bb9 123
124 $params['page'] = ($offset / $rowCount) + 1;
125 $params['rp'] = $rowCount;
126 $contactType = CRM_Contact_BAO_Contact::getContactType($params['cid']);
127
128 $obj = new CRM_Profile_Page_MultipleRecordFieldsListing();
129 $obj->_pageViewType = 'customDataView';
130 $obj->_contactId = $params['cid'];
131 $obj->_customGroupId = $params['cgid'];
132 $obj->_contactType = $contactType;
c693f065 133 $obj->_DTparams['offset'] = ($params['page'] - 1) * $params['rp'];
134 $obj->_DTparams['rowCount'] = $params['rp'];
135 if ($sort && $sortOrder) {
136 $sort = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $sort, 'column_name', 'label');
137 $obj->_DTparams['sort'] = $sort . ' ' . $sortOrder;
138 }
5a2c7bb9 139
c693f065 140 list($fields, $attributes) = $obj->browse();
5a2c7bb9 141
f5eda27f 142 // format params and add class attributes
c693f065 143 $fieldList = array();
144 foreach ($fields as $id => $value) {
145 $field = array();
d51e02d3 146 foreach ($value as $fieldId => &$fieldName) {
147 if (!empty($attributes[$fieldId][$id]['class'])) {
148 $fieldName = array('data' => $fieldName, 'cssClass' => $attributes[$fieldId][$id]['class']);
149 }
c693f065 150 if (is_numeric($fieldId)) {
d51e02d3 151 $fName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $fieldId, 'label');
152 CRM_Utils_Array::crmReplaceKey($value, $fieldId, $fName);
5a2c7bb9 153 }
154 }
c693f065 155 $field = $value;
156 array_push($fieldList, $field);
5a2c7bb9 157 }
f5eda27f 158 $totalRecords = !empty($obj->_total) ? $obj->_total : 0;
c693f065 159 $fieldList = array_map('array_merge', $fieldList);
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}