| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for Component |
| 38 | */ |
| 39 | class CRM_Admin_Form_Setting_Component extends CRM_Admin_Form_Setting { |
| 40 | protected $_components; |
| 41 | |
| 42 | /** |
| 43 | * Function to build the form |
| 44 | * |
| 45 | * @return void |
| 46 | * @access public |
| 47 | */ |
| 48 | public function buildQuickForm() { |
| 49 | CRM_Utils_System::setTitle(ts('Settings - Enable Components')); |
| 50 | $components = $this->_getComponentSelectValues(); |
| 51 | $include = &$this->addElement('advmultiselect', 'enableComponents', |
| 52 | ts('Components') . ' ', $components, |
| 53 | array( |
| 54 | 'size' => 5, |
| 55 | 'style' => 'width:150px', |
| 56 | 'class' => 'advmultiselect', |
| 57 | ) |
| 58 | ); |
| 59 | |
| 60 | $include->setButtonAttributes('add', array('value' => ts('Enable >>'))); |
| 61 | $include->setButtonAttributes('remove', array('value' => ts('<< Disable'))); |
| 62 | |
| 63 | $this->addFormRule(array('CRM_Admin_Form_Setting_Component', 'formRule'), $this); |
| 64 | |
| 65 | parent::buildQuickForm(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * global form rule |
| 70 | * |
| 71 | * @param array $fields the input form values |
| 72 | * @param array $files the uploaded files if any |
| 73 | * @param array $options additional user data |
| 74 | * |
| 75 | * @return true if no errors, else array of errors |
| 76 | * @access public |
| 77 | * @static |
| 78 | */ |
| 79 | static function formRule($fields, $files, $options) { |
| 80 | $errors = array(); |
| 81 | |
| 82 | if (array_key_exists('enableComponents', $fields) && is_array($fields['enableComponents'])) { |
| 83 | if (in_array('CiviPledge', $fields['enableComponents']) && |
| 84 | !in_array('CiviContribute', $fields['enableComponents']) |
| 85 | ) { |
| 86 | $errors['enableComponents'] = ts('You need to enable CiviContribute before enabling CiviPledge.'); |
| 87 | } |
| 88 | if (in_array('CiviCase', $fields['enableComponents']) && |
| 89 | !CRM_Core_DAO::checkTriggerViewPermission(TRUE, FALSE) |
| 90 | ) { |
| 91 | $errors['enableComponents'] = ts('CiviCase requires CREATE VIEW and DROP VIEW permissions for the database.'); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return $errors; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return array |
| 100 | */ |
| 101 | private function _getComponentSelectValues() { |
| 102 | $ret = array(); |
| 103 | $this->_components = CRM_Core_Component::getComponents(); |
| 104 | foreach ($this->_components as $name => $object) { |
| 105 | $ret[$name] = $object->info['translatedName']; |
| 106 | } |
| 107 | |
| 108 | return $ret; |
| 109 | } |
| 110 | |
| 111 | public function postProcess() { |
| 112 | $params = $this->controller->exportValues($this->_name); |
| 113 | |
| 114 | CRM_Case_Info::onToggleComponents($this->_defaults['enableComponents'], $params['enableComponents'], NULL); |
| 115 | parent::commonProcess($params); |
| 116 | |
| 117 | // reset navigation when components are enabled / disabled |
| 118 | CRM_Core_BAO_Navigation::resetNavigation(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param $dsn |
| 123 | * @param $fileName |
| 124 | * @param bool $lineMode |
| 125 | */ |
| 126 | public static function loadCaseSampleData($dsn, $fileName, $lineMode = FALSE) { |
| 127 | global $crmPath; |
| 128 | |
| 129 | $db = &DB::connect($dsn); |
| 130 | if (PEAR::isError($db)) { |
| 131 | die("Cannot open $dsn: " . $db->getMessage()); |
| 132 | } |
| 133 | |
| 134 | $domain = new CRM_Core_DAO_Domain(); |
| 135 | $domain->find(TRUE); |
| 136 | $multiLingual = (bool) $domain->locales; |
| 137 | $smarty = CRM_Core_Smarty::singleton(); |
| 138 | $smarty->assign('multilingual', $multiLingual); |
| 139 | $smarty->assign('locales', explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales)); |
| 140 | |
| 141 | if (!$lineMode) { |
| 142 | |
| 143 | $string = $smarty->fetch($fileName); |
| 144 | // change \r\n to fix windows issues |
| 145 | $string = str_replace("\r\n", "\n", $string); |
| 146 | |
| 147 | //get rid of comments starting with # and -- |
| 148 | |
| 149 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); |
| 150 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); |
| 151 | |
| 152 | $queries = preg_split('/;$/m', $string); |
| 153 | foreach ($queries as $query) { |
| 154 | $query = trim($query); |
| 155 | if (!empty($query)) { |
| 156 | $res = &$db->query($query); |
| 157 | if (PEAR::isError($res)) { |
| 158 | die("Cannot execute $query: " . $res->getMessage()); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | else { |
| 164 | $fd = fopen($fileName, "r"); |
| 165 | while ($string = fgets($fd)) { |
| 166 | $string = preg_replace("/^#[^\n]*$/m", "\n", $string); |
| 167 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); |
| 168 | |
| 169 | $string = trim($string); |
| 170 | if (!empty($string)) { |
| 171 | $res = &$db->query($string); |
| 172 | if (PEAR::isError($res)) { |
| 173 | die("Cannot execute $string: " . $res->getMessage()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |