| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 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 | */ |
| 38 | class CRM_Custom_Page_AJAX { |
| 39 | /** |
| 40 | * Get list of options. |
| 41 | * |
| 42 | */ |
| 43 | public static function getOptionList() { |
| 44 | $params = $_REQUEST; |
| 45 | |
| 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; |
| 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', |
| 60 | 'is_active', |
| 61 | 'links', |
| 62 | 'class', |
| 63 | ); |
| 64 | |
| 65 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
| 66 | echo CRM_Utils_JSON::encodeDataTableSelector($options, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
| 67 | CRM_Utils_System::civiExit(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Fix Ordering of options |
| 72 | * |
| 73 | */ |
| 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); |
| 105 | CRM_Utils_JSON::output(TRUE); |
| 106 | } |
| 107 | |
| 108 | } |