| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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 | * This class introduces component to the system and provides all the |
| 30 | * information about it. It needs to extend CRM_Core_Component_Info |
| 31 | * abstract class. |
| 32 | * |
| 33 | * @package CRM |
| 34 | * @copyright CiviCRM LLC (c) 2004-2017 |
| 35 | */ |
| 36 | class CRM_Mailing_Info extends CRM_Core_Component_Info { |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | */ |
| 41 | protected $keyword = 'mailing'; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * @inheritDoc |
| 46 | * @return array |
| 47 | */ |
| 48 | public function getInfo() { |
| 49 | return array( |
| 50 | 'name' => 'CiviMail', |
| 51 | 'translatedName' => ts('CiviMail'), |
| 52 | 'title' => ts('CiviCRM Mailing Engine'), |
| 53 | 'search' => 1, |
| 54 | 'showActivitiesInCore' => 1, |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get AngularJS modules and their dependencies. |
| 60 | * |
| 61 | * @return array |
| 62 | * list of modules; same format as CRM_Utils_Hook::angularModules(&$angularModules) |
| 63 | * @see CRM_Utils_Hook::angularModules |
| 64 | */ |
| 65 | public function getAngularModules() { |
| 66 | // load angular files only if valid permissions are granted to the user |
| 67 | if (!CRM_Core_Permission::check('access CiviMail') |
| 68 | && !CRM_Core_Permission::check('create mailings') |
| 69 | && !CRM_Core_Permission::check('schedule mailings') |
| 70 | && !CRM_Core_Permission::check('approve mailings') |
| 71 | ) { |
| 72 | return array(); |
| 73 | } |
| 74 | global $civicrm_root; |
| 75 | |
| 76 | $reportIds = array(); |
| 77 | $reportTypes = array('detail', 'opened', 'bounce', 'clicks'); |
| 78 | foreach ($reportTypes as $report) { |
| 79 | $result = civicrm_api3('ReportInstance', 'get', array( |
| 80 | 'sequential' => 1, |
| 81 | 'report_id' => 'mailing/' . $report)); |
| 82 | $reportIds[$report] = $result['values'][0]['id']; |
| 83 | } |
| 84 | $result = array(); |
| 85 | $result['crmMailing'] = include "$civicrm_root/ang/crmMailing.ang.php"; |
| 86 | $result['crmMailingAB'] = include "$civicrm_root/ang/crmMailingAB.ang.php"; |
| 87 | $result['crmD3'] = include "$civicrm_root/ang/crmD3.ang.php"; |
| 88 | |
| 89 | $config = CRM_Core_Config::singleton(); |
| 90 | $session = CRM_Core_Session::singleton(); |
| 91 | $contactID = $session->get('userID'); |
| 92 | |
| 93 | // Generic params. |
| 94 | $params = array( |
| 95 | 'options' => array('limit' => 0), |
| 96 | 'sequential' => 1, |
| 97 | ); |
| 98 | $groupNames = civicrm_api3('Group', 'get', $params + array( |
| 99 | 'is_active' => 1, |
| 100 | 'check_permissions' => TRUE, |
| 101 | 'return' => array('title', 'visibility', 'group_type', 'is_hidden'), |
| 102 | )); |
| 103 | $headerfooterList = civicrm_api3('MailingComponent', 'get', $params + array( |
| 104 | 'is_active' => 1, |
| 105 | 'return' => array('name', 'component_type', 'is_default', 'body_html', 'body_text'), |
| 106 | )); |
| 107 | |
| 108 | $emailAdd = civicrm_api3('Email', 'get', array( |
| 109 | 'sequential' => 1, |
| 110 | 'return' => "email", |
| 111 | 'contact_id' => $contactID, |
| 112 | )); |
| 113 | |
| 114 | $mesTemplate = civicrm_api3('MessageTemplate', 'get', $params + array( |
| 115 | 'sequential' => 1, |
| 116 | 'is_active' => 1, |
| 117 | 'return' => array("id", "msg_title"), |
| 118 | 'workflow_id' => array('IS NULL' => ""), |
| 119 | )); |
| 120 | $mailTokens = civicrm_api3('Mailing', 'gettokens', array( |
| 121 | 'entity' => array('contact', 'mailing'), |
| 122 | 'sequential' => 1, |
| 123 | )); |
| 124 | $fromAddress = civicrm_api3('OptionValue', 'get', $params + array( |
| 125 | 'option_group_id' => "from_email_address", |
| 126 | 'domain_id' => CRM_Core_Config::domainID(), |
| 127 | )); |
| 128 | $enabledLanguages = CRM_Core_I18n::languages(TRUE); |
| 129 | $isMultiLingual = (count($enabledLanguages) > 1); |
| 130 | CRM_Core_Resources::singleton() |
| 131 | ->addSetting(array( |
| 132 | 'crmMailing' => array( |
| 133 | 'templateTypes' => CRM_Mailing_BAO_Mailing::getTemplateTypes(), |
| 134 | 'civiMails' => array(), |
| 135 | 'campaignEnabled' => in_array('CiviCampaign', $config->enableComponents), |
| 136 | 'groupNames' => array(), |
| 137 | // @todo see if we can remove this by dynamically generating the test group list |
| 138 | 'testGroupNames' => $groupNames['values'], |
| 139 | 'headerfooterList' => $headerfooterList['values'], |
| 140 | 'mesTemplate' => $mesTemplate['values'], |
| 141 | 'emailAdd' => $emailAdd['values'], |
| 142 | 'mailTokens' => $mailTokens['values'], |
| 143 | 'contactid' => $contactID, |
| 144 | 'requiredTokens' => CRM_Utils_Token::getRequiredTokens(), |
| 145 | 'enableReplyTo' => (int) Civi::settings()->get('replyTo'), |
| 146 | 'disableMandatoryTokensCheck' => (int) Civi::settings()->get('disable_mandatory_tokens_check'), |
| 147 | 'fromAddress' => $fromAddress['values'], |
| 148 | 'defaultTestEmail' => civicrm_api3('Contact', 'getvalue', array( |
| 149 | 'id' => 'user_contact_id', |
| 150 | 'return' => 'email', |
| 151 | )), |
| 152 | 'visibility' => CRM_Utils_Array::makeNonAssociative(CRM_Core_SelectValues::groupVisibility()), |
| 153 | 'workflowEnabled' => CRM_Mailing_Info::workflowEnabled(), |
| 154 | 'reportIds' => $reportIds, |
| 155 | 'enabledLanguages' => $enabledLanguages, |
| 156 | 'isMultiLingual' => $isMultiLingual, |
| 157 | ), |
| 158 | )) |
| 159 | ->addPermissions(array( |
| 160 | 'view all contacts', |
| 161 | 'edit all contacts', |
| 162 | 'access CiviMail', |
| 163 | 'create mailings', |
| 164 | 'schedule mailings', |
| 165 | 'approve mailings', |
| 166 | 'delete in CiviMail', |
| 167 | 'edit message templates', |
| 168 | )); |
| 169 | |
| 170 | return $result; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @return bool |
| 175 | */ |
| 176 | public static function workflowEnabled() { |
| 177 | $config = CRM_Core_Config::singleton(); |
| 178 | |
| 179 | // early exit, since not true for most |
| 180 | if (!$config->userSystem->is_drupal || |
| 181 | !function_exists('module_exists') |
| 182 | ) { |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | if (!module_exists('rules')) { |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
| 190 | $enableWorkflow = Civi::settings()->get('civimail_workflow'); |
| 191 | |
| 192 | return ($enableWorkflow && |
| 193 | $config->userSystem->is_drupal |
| 194 | ) ? TRUE : FALSE; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @inheritDoc |
| 199 | * @param bool $getAllUnconditionally |
| 200 | * @param bool $descriptions |
| 201 | * Whether to return permission descriptions |
| 202 | * |
| 203 | * @return array |
| 204 | */ |
| 205 | public function getPermissions($getAllUnconditionally = FALSE, $descriptions = FALSE) { |
| 206 | $permissions = array( |
| 207 | 'access CiviMail' => array( |
| 208 | ts('access CiviMail'), |
| 209 | ), |
| 210 | 'access CiviMail subscribe/unsubscribe pages' => array( |
| 211 | ts('access CiviMail subscribe/unsubscribe pages'), |
| 212 | ts('Subscribe/unsubscribe from mailing list group'), |
| 213 | ), |
| 214 | 'delete in CiviMail' => array( |
| 215 | ts('delete in CiviMail'), |
| 216 | ts('Delete Mailing'), |
| 217 | ), |
| 218 | 'view public CiviMail content' => array( |
| 219 | ts('view public CiviMail content'), |
| 220 | ), |
| 221 | ); |
| 222 | |
| 223 | if (self::workflowEnabled() || $getAllUnconditionally) { |
| 224 | $permissions['create mailings'] = array( |
| 225 | ts('create mailings'), |
| 226 | ); |
| 227 | $permissions['schedule mailings'] = array( |
| 228 | ts('schedule mailings'), |
| 229 | ); |
| 230 | $permissions['approve mailings'] = array( |
| 231 | ts('approve mailings'), |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | if (!$descriptions) { |
| 236 | foreach ($permissions as $name => $attr) { |
| 237 | $permissions[$name] = array_shift($attr); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return $permissions; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * @inheritDoc |
| 247 | * @return null |
| 248 | */ |
| 249 | public function getUserDashboardElement() { |
| 250 | // no dashboard element for this component |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @return null |
| 256 | */ |
| 257 | public function getUserDashboardObject() { |
| 258 | // no dashboard element for this component |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @inheritDoc |
| 264 | * @return array |
| 265 | */ |
| 266 | public function registerTab() { |
| 267 | return array( |
| 268 | 'title' => ts('Mailings'), |
| 269 | 'id' => 'mailing', |
| 270 | 'url' => 'mailing', |
| 271 | 'weight' => 45, |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @inheritDoc |
| 277 | * @return array |
| 278 | */ |
| 279 | public function registerAdvancedSearchPane() { |
| 280 | return array( |
| 281 | 'title' => ts('Mailings'), |
| 282 | 'weight' => 20, |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @inheritDoc |
| 288 | * @return null |
| 289 | */ |
| 290 | public function getActivityTypes() { |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * add shortcut to Create New. |
| 296 | * @param $shortCuts |
| 297 | */ |
| 298 | public function creatNewShortcut(&$shortCuts) { |
| 299 | } |
| 300 | |
| 301 | } |