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