First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,338 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Choose include / exclude groups and mass sms.
*/
class CRM_SMS_Form_Group extends CRM_Contact_Form_Task {
/**
* Set variables up before form is built.
*/
public function preProcess() {
if (!CRM_SMS_BAO_Provider::activeProviderCount()) {
CRM_Core_Error::fatal(ts('The <a href="%1">SMS Provider</a> has not been configured or is not active.', array(1 => CRM_Utils_System::url('civicrm/admin/sms/provider', 'reset=1'))));
}
$session = CRM_Core_Session::singleton();
$session->replaceUserContext(CRM_Utils_System::url('civicrm/mailing/browse', 'reset=1&sms=1'));
}
/**
* Set default values for the form.
* The default values are retrieved from the database.
*/
public function setDefaultValues() {
$mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
$continue = CRM_Utils_Request::retrieve('continue', 'String', $this, FALSE, NULL);
$defaults = array();
if ($mailingID) {
$mailing = new CRM_Mailing_DAO_Mailing();
$mailing->id = $mailingID;
$mailing->addSelect('name');
$mailing->find(TRUE);
$defaults['name'] = $mailing->name;
if (!$continue) {
$defaults['name'] = ts('Copy of %1', array(1 => $mailing->name));
}
else {
// CRM-7590, reuse same mailing ID if we are continuing
$this->set('mailing_id', $mailingID);
}
$dao = new CRM_Mailing_DAO_MailingGroup();
$mailingGroups = array();
$dao->mailing_id = $mailingID;
$dao->find();
while ($dao->fetch()) {
$mailingGroups[$dao->entity_table][$dao->group_type][] = $dao->entity_id;
}
$defaults['includeGroups'] = $mailingGroups['civicrm_group']['Include'];
$defaults['excludeGroups'] = CRM_Utils_Array::value('Exclude', $mailingGroups['civicrm_group']);
$defaults['includeMailings'] = CRM_Utils_Array::value('Include', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups));
$defaults['excludeMailings'] = CRM_Utils_Array::value('Exclude', CRM_Utils_Array::value('civicrm_mailing', $mailingGroups));
}
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
// Get the context.
$context = $this->get('context');
$this->assign('context', $context);
$this->add('text', 'name', ts('Name Your SMS'),
CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'name'),
TRUE
);
// Get the mailing groups.
$groups = CRM_Core_PseudoConstant::nestedGroup('Mailing');
// Get the sms mailing list.
$mailings = CRM_Mailing_PseudoConstant::completed('sms');
if (!$mailings) {
$mailings = array();
}
// run the groups through a hook so users can trim it if needed
CRM_Utils_Hook::mailingGroups($this, $groups, $mailings);
$select2style = array(
'multiple' => TRUE,
'style' => 'width: 100%; max-width: 60em;',
'class' => 'crm-select2',
'placeholder' => ts('- select -'),
);
$this->add('select', 'includeGroups',
ts('Include Group(s)'),
$groups,
TRUE,
$select2style
);
$this->add('select', 'excludeGroups',
ts('Exclude Group(s)'),
$groups,
FALSE,
$select2style
);
$this->add('select', 'includeMailings',
ts('INCLUDE Recipients of These Message(s)'),
$mailings,
FALSE,
$select2style
);
$this->add('select', 'excludeMailings',
ts('EXCLUDE Recipients of These Message(s)'),
$mailings,
FALSE,
$select2style
);
$this->addFormRule(array('CRM_SMS_Form_Group', 'formRule'));
$buttons = array(
array(
'type' => 'next',
'name' => ts('Next'),
'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
);
$this->addButtons($buttons);
$this->assign('groupCount', count($groups));
$this->assign('mailingCount', count($mailings));
}
public function postProcess() {
$values = $this->controller->exportValues($this->_name);
$groups = array();
foreach (array(
'name',
'group_id',
'is_sms',
) as $n) {
if (!empty($values[$n])) {
$params[$n] = $values[$n];
}
}
$qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
$this->set('name', $params['name']);
$inGroups = $values['includeGroups'];
$outGroups = $values['excludeGroups'];
$inMailings = $values['includeMailings'];
$outMailings = $values['excludeMailings'];
if (is_array($inGroups)) {
foreach ($inGroups as $key => $id) {
if ($id) {
$groups['include'][] = $id;
}
}
}
if (is_array($outGroups)) {
foreach ($outGroups as $key => $id) {
if ($id) {
$groups['exclude'][] = $id;
}
}
}
$mailings = array();
if (is_array($inMailings)) {
foreach ($inMailings as $key => $id) {
if ($id) {
$mailings['include'][] = $id;
}
}
}
if (is_array($outMailings)) {
foreach ($outMailings as $key => $id) {
if ($id) {
$mailings['exclude'][] = $id;
}
}
}
$session = CRM_Core_Session::singleton();
$params['groups'] = $groups;
$params['mailings'] = $mailings;
$ids = array();
if ($this->get('mailing_id')) {
// don't create a new mass sms if already exists
$ids['mailing_id'] = $this->get('mailing_id');
$groupTableName = CRM_Contact_BAO_Group::getTableName();
$mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
// delete previous includes/excludes, if mailing already existed
foreach (array(
'groups',
'mailings',
) as $entity) {
$mg = new CRM_Mailing_DAO_MailingGroup();
$mg->mailing_id = $ids['mailing_id'];
$mg->entity_table = ($entity == 'groups') ? $groupTableName : $mailingTableName;
$mg->find();
while ($mg->fetch()) {
$mg->delete();
}
}
}
else {
// new mailing, so lets set the created_id
$session = CRM_Core_Session::singleton();
$params['created_id'] = $session->get('userID');
$params['created_date'] = date('YmdHis');
}
$mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
$this->set('mailing_id', $mailing->id);
// also compute the recipients and store them in the mailing recipients table
CRM_Mailing_BAO_Mailing::getRecipients($mailing->id,
$mailing->id,
TRUE,
FALSE,
'sms'
);
$count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id);
$this->set('count', $count);
$this->assign('count', $count);
$this->set('groups', $groups);
$this->set('mailings', $mailings);
if ($qf_Group_submit) {
$status = ts("Your Mass SMS has been saved.");
CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
$url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1');
return $this->controller->setDestination($url);
}
}
/**
* Display Name of the form.
*
*
* @return string
*/
public function getTitle() {
return ts('Select Recipients');
}
/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($fields) {
$errors = array();
if (isset($fields['includeGroups']) &&
is_array($fields['includeGroups']) &&
isset($fields['excludeGroups']) &&
is_array($fields['excludeGroups'])
) {
$checkGroups = array();
$checkGroups = array_intersect($fields['includeGroups'], $fields['excludeGroups']);
if (!empty($checkGroups)) {
$errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
}
}
if (isset($fields['includeMailings']) &&
is_array($fields['includeMailings']) &&
isset($fields['excludeMailings']) &&
is_array($fields['excludeMailings'])
) {
$checkMailings = array();
$checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
if (!empty($checkMailings)) {
$errors['excludeMailings'] = ts('Cannot have same sms in Include mailing(s) and Exclude mailing(s).');
}
}
return empty($errors) ? TRUE : $errors;
}
}

View file

@ -0,0 +1,182 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* SMS Form.
*/
class CRM_SMS_Form_Provider extends CRM_Core_Form {
protected $_id = NULL;
public function preProcess() {
$this->_id = $this->get('id');
$this->setPageTitle(ts('SMS Provider'));
if ($this->_id) {
$refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
"reset=1&action=update&id={$this->_id}",
FALSE, NULL, FALSE
);
}
else {
$refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
"reset=1&action=add",
FALSE, NULL, FALSE
);
}
$this->assign('refreshURL', $refreshURL);
}
/**
* Build the form object.
*/
public function buildQuickForm() {
parent::buildQuickForm();
$this->addButtons(array(
array(
'type' => 'next',
'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
if ($this->_action & CRM_Core_Action::DELETE) {
return;
}
$attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider');
$providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
$apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
$this->add('select', 'name', ts('Name'), array('' => '- select -') + $providerNames, TRUE);
$this->add('text', 'title', ts('Title'),
$attributes['title'], TRUE
);
$this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', array(
'CRM_SMS_DAO_Provider',
$this->_id,
));
$this->add('text', 'username', ts('Username'),
$attributes['username'], TRUE
);
$this->add('password', 'password', ts('Password'),
$attributes['password'], TRUE
);
$this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
$this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE);
$this->add('textarea', 'api_params', ts('API Parameters'),
"cols=50 rows=6", TRUE
);
$this->add('checkbox', 'is_active', ts('Is this provider active?'));
$this->add('checkbox', 'is_default', ts('Is this a default provider?'));
}
/**
* Set the default values of various form elements.
*
* @return array
*/
public function setDefaultValues() {
$defaults = array();
$name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
if ($name) {
$defaults['name'] = $name;
$provider = CRM_SMS_Provider::singleton(array('provider' => $name));
$defaults['api_url'] = $provider->_apiURL;
}
if (!$this->_id) {
$defaults['is_active'] = $defaults['is_default'] = 1;
return $defaults;
}
$dao = new CRM_SMS_DAO_Provider();
$dao->id = $this->_id;
if ($name) {
$dao->name = $name;
}
if (!$dao->find(TRUE)) {
return $defaults;
}
CRM_Core_DAO::storeValues($dao, $defaults);
return $defaults;
}
/**
* Process the form submission.
*/
public function postProcess() {
CRM_Utils_System::flushCache('CRM_SMS_DAO_Provider');
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_SMS_BAO_Provider::del($this->_id);
CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
return;
}
$recData = $values = $this->controller->exportValues($this->_name);
$recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
$recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
if ($this->_action && (CRM_Core_Action::UPDATE || CRM_Core_Action::ADD)) {
if ($this->_id) {
$recData['id'] = $this->_id;
}
civicrm_api3('SmsProvider', 'create', $recData);
}
}
}

View file

@ -0,0 +1,196 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_SMS_Form_Schedule extends CRM_Core_Form {
/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_mailingID = $this->get('mailing_id');
if (!$this->_mailingID) {
$this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
}
}
/**
* Set default values for the form.
*/
public function setDefaultValues() {
$defaults = array();
$count = $this->get('count');
$this->assign('count', $count);
$defaults['now'] = 1;
return $defaults;
}
/**
* Build the form object for the last step of the sms wizard.
*/
public function buildQuickform() {
$this->addDateTime('start_date', ts('Schedule SMS'), FALSE, array('formatType' => 'mailing'));
$this->addElement('checkbox', 'now', ts('Send Immediately'));
$this->addFormRule(array('CRM_SMS_Form_Schedule', 'formRule'), $this);
$buttons = array(
array(
'type' => 'back',
'name' => ts('Previous'),
),
array(
'type' => 'next',
'name' => ts('Submit Mass SMS'),
'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
'isDefault' => TRUE,
'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"),
),
array(
'type' => 'cancel',
'name' => ts('Continue Later'),
),
);
$this->addButtons($buttons);
$preview = array();
$preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
$preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
$this->assign_by_ref('preview', $preview);
}
/**
* Form rule to validate the date selector and/or if we should deliver
* immediately.
*
* Warning: if you make changes here, be sure to also make them in
* Retry.php
*
* @param array $params
* The form values.
*
* @param $files
* @param $self
*
* @return bool
* True if either we deliver immediately, or the date is properly
* set.
*/
public static function formRule($params, $files, $self) {
if (!empty($params['_qf_Schedule_submit'])) {
CRM_Core_Session::setStatus(ts("Your Mass SMS has been saved. Click the 'Continue' action to resume working on it."), ts('Saved'), 'success');
$url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1&sms=1');
CRM_Utils_System::redirect($url);
}
if (isset($params['now']) || CRM_Utils_Array::value('_qf_Schedule_back', $params) == ts('Previous')) {
return TRUE;
}
if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($params['start_date'],
$params['start_date_time']
)) < CRM_Utils_Date::format(date('YmdHi00'))
) {
return array(
'start_date' => ts('Start date cannot be earlier than the current time.'),
);
}
return TRUE;
}
/**
* Process the posted form values. Create and schedule a Mass SMS.
*/
public function postProcess() {
$params = array();
$params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID;
if (empty($params['mailing_id'])) {
CRM_Core_Error::fatal(ts('Could not find a mailing id'));
}
foreach (array('now', 'start_date', 'start_date_time') as $parameter) {
$params[$parameter] = $this->controller->exportValue($this->_name, $parameter);
}
if ($params['now']) {
$params['scheduled_date'] = date('YmdHis');
}
else {
$params['scheduled_date'] = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
}
$session = CRM_Core_Session::singleton();
// set the scheduled_id
$params['scheduled_id'] = $session->get('userID');
$params['scheduled_date'] = date('YmdHis');
// set approval details if workflow is not enabled
if (!CRM_Mailing_Info::workflowEnabled()) {
$params['approver_id'] = $session->get('userID');
$params['approval_date'] = date('YmdHis');
$params['approval_status_id'] = 1;
}
if ($params['now']) {
$params['scheduled_date'] = date('YmdHis');
}
else {
$params['scheduled_date'] = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
}
// Build the mailing object.
CRM_Mailing_BAO_Mailing::create($params, $ids);
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
'reset=1&scheduled=true&sms=1'
));
}
/**
* Display Name of the form.
*
*
* @return string
*/
public function getTitle() {
return ts('Schedule or Send');
}
}

View file

@ -0,0 +1,440 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* This file is used to build the form configuring mass sms details.
*/
class CRM_SMS_Form_Upload extends CRM_Core_Form {
public $_mailingID;
public function preProcess() {
$this->_mailingID = $this->get('mailing_id');
if (CRM_Core_Permission::check('administer CiviCRM')) {
$this->assign('isAdmin', 1);
}
}
/**
* Set default values for the form.
*/
public function setDefaultValues() {
$mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
// Need to differentiate new/reuse mailing, CRM-2873.
$reuseMailing = FALSE;
if ($mailingID) {
$reuseMailing = TRUE;
}
else {
$mailingID = $this->_mailingID;
}
$count = $this->get('count');
$this->assign('count', $count);
$this->set('skipTextFile', FALSE);
$defaults = array();
if ($mailingID) {
$dao = new CRM_Mailing_DAO_Mailing();
$dao->id = $mailingID;
$dao->find(TRUE);
$dao->storeValues($dao, $defaults);
// We don't want to retrieve template details once it is
// set in session.
$templateId = $this->get('template');
$this->assign('templateSelected', $templateId ? $templateId : 0);
if (isset($defaults['msg_template_id']) && !$templateId) {
$defaults['SMStemplate'] = $defaults['msg_template_id'];
$messageTemplate = new CRM_Core_DAO_MessageTemplate();
$messageTemplate->id = $defaults['msg_template_id'];
$messageTemplate->selectAdd();
$messageTemplate->selectAdd('msg_text');
$messageTemplate->find(TRUE);
$defaults['sms_text_message'] = $messageTemplate->msg_text;
}
if (isset($defaults['body_text'])) {
$defaults['sms_text_message'] = $defaults['body_text'];
$this->set('textFile', $defaults['body_text']);
$this->set('skipTextFile', TRUE);
}
}
// Fix for CRM-2873.
if (!$reuseMailing) {
$textFilePath = $this->get('textFilePath');
if ($textFilePath &&
file_exists($textFilePath)
) {
$defaults['sms_text_message'] = file_get_contents($textFilePath);
if (strlen($defaults['sms_text_message']) > 0) {
$this->set('skipTextFile', TRUE);
}
}
}
$defaults['upload_type'] = 1;
return $defaults;
}
/**
* Build the form object.
*/
public function buildQuickForm() {
$session = CRM_Core_Session::singleton();
$config = CRM_Core_Config::singleton();
$options = array();
$tempVar = FALSE;
$this->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR);
// this seems so hacky, not sure what we are doing here and why. Need to investigate and fix
$session->getVars($options,
"CRM_SMS_Controller_Send_{$this->controller->_key}"
);
$providers = CRM_SMS_BAO_Provider::getProviders(array('id', 'title'));
if (empty($providers)) {
//redirect user to configure sms provider.
$url = CRM_Utils_System::url('civicrm/admin/sms/provider', 'action=add&reset=1');
$status = ts("There is no SMS Provider Configured. You can add here <a href='%1'>Add SMS Provider</a>", array(1 => $url));
$session->setStatus($status);
}
else {
$providerSelect[''] = '- select -';
foreach ($providers as $provider) {
$providerSelect[$provider['id']] = $provider['title'];
}
}
$this->add('select', 'sms_provider_id',
ts('SMS Provider'), $providerSelect, TRUE
);
$attributes = array('onclick' => "showHideUpload();");
$options = array(ts('Upload Content'), ts('Compose On-screen'));
$this->addRadio('upload_type', ts('I want to'), $options, $attributes, "&nbsp;&nbsp;");
CRM_Mailing_BAO_Mailing::commonCompose($this);
$this->addElement('file', 'textFile', ts('Upload TEXT Message'), 'size=30 maxlength=60');
$this->setMaxFileSize(1024 * 1024);
$this->addRule('textFile', ts('File size should be less than 1 MByte'), 'maxfilesize', 1024 * 1024);
$this->addRule('textFile', ts('File must be in UTF-8 encoding'), 'utf8File');
$this->addFormRule(array('CRM_SMS_Form_Upload', 'formRule'), $this);
$buttons = array(
array(
'type' => 'back',
'name' => ts('Previous'),
),
array(
'type' => 'upload',
'name' => ts('Next'),
'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
);
$this->addButtons($buttons);
}
public function postProcess() {
$params = $ids = array();
$uploadParams = array('from_name');
$formValues = $this->controller->exportValues($this->_name);
foreach ($uploadParams as $key) {
if (!empty($formValues[$key])) {
$params[$key] = $formValues[$key];
$this->set($key, $formValues[$key]);
}
}
if (!$formValues['upload_type']) {
$contents = NULL;
if (isset($formValues['textFile']) &&
!empty($formValues['textFile'])
) {
$contents = file_get_contents($formValues['textFile']['name']);
$this->set($key, $formValues['textFile']['name']);
}
if ($contents) {
$params['body_text'] = $contents;
}
else {
$params['body_text'] = 'NULL';
}
}
else {
$text_message = $formValues['sms_text_message'];
$params['body_text'] = $text_message;
$this->set('textFile', $params['body_text']);
$this->set('text_message', $params['body_text']);
}
$params['name'] = $this->get('name');
$session = CRM_Core_Session::singleton();
$params['contact_id'] = $session->get('userID');
$composeFields = array(
'SMStemplate',
'SMSsaveTemplate',
'SMSupdateTemplate',
'SMSsaveTemplateName',
);
$msgTemplate = NULL;
// Mail template is composed.
if ($formValues['upload_type']) {
$composeParams = array();
foreach ($composeFields as $key) {
if (!empty($formValues[$key])) {
$composeParams[$key] = $formValues[$key];
$this->set($key, $formValues[$key]);
}
}
if (!empty($composeParams['SMSupdateTemplate'])) {
$templateParams = array(
'msg_text' => $text_message,
'is_active' => TRUE,
'is_sms' => TRUE,
);
$templateParams['id'] = $formValues['SMStemplate'];
$msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
}
if (!empty($composeParams['SMSsaveTemplate'])) {
$templateParams = array(
'msg_text' => $text_message,
'is_active' => TRUE,
'is_sms' => TRUE,
);
$templateParams['msg_title'] = $composeParams['SMSsaveTemplateName'];
$msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
}
if (isset($msgTemplate->id)) {
$params['msg_template_id'] = $msgTemplate->id;
}
else {
$params['msg_template_id'] = CRM_Utils_Array::value('SMStemplate', $formValues);
}
$this->set('template', $params['msg_template_id']);
}
$ids['mailing_id'] = $this->_mailingID;
// Get the from email address.
$params['sms_provider_id'] = $formValues['sms_provider_id'];
// Get the from Name.
$params['from_name'] = CRM_Core_DAO::getFieldValue('CRM_SMS_DAO_Provider', $params['sms_provider_id'], 'username');
// Build SMS in mailing table.
CRM_Mailing_BAO_Mailing::create($params, $ids);
}
/**
* Validation.
*
* @param array $params
* (ref.) an assoc array of name/value pairs.
*
* @param $files
* @param $self
*
* @return bool|array
* mixed true or array of errors
*/
public static function formRule($params, $files, $self) {
if (!empty($_POST['_qf_Import_refresh'])) {
return TRUE;
}
$errors = array();
$template = CRM_Core_Smarty::singleton();
$domain = CRM_Core_BAO_Domain::getDomain();
$mailing = new CRM_Mailing_BAO_Mailing();
$mailing->id = $self->_mailingID;
$mailing->find(TRUE);
$session = CRM_Core_Session::singleton();
$values = array(
'contact_id' => $session->get('userID'),
'version' => 3,
);
require_once 'api/api.php';
$contact = civicrm_api('contact', 'get', $values);
// CRM-4524.
$contact = reset($contact['values']);
$verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner'));
foreach ($verp as $key => $value) {
$verp[$key]++;
}
$urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl'));
foreach ($urls as $key => $value) {
$urls[$key]++;
}
$skipTextFile = $self->get('skipTextFile');
if (!$params['upload_type']) {
if ((!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name']))) {
if (!($skipTextFile)) {
$errors['textFile'] = ts('Please provide a Text');
}
}
}
else {
if (empty($params['sms_text_message'])) {
$errors['sms_text_message'] = ts('Please provide a Text');
}
else {
if (!empty($params['text_message'])) {
$messageCheck = CRM_Utils_Array::value('text_message', $params);
if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR)) {
$errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider::MAX_SMS_CHAR));
}
}
}
if (!empty($params['SMSsaveTemplate']) && empty($params['SMSsaveTemplateName'])) {
$errors['SMSsaveTemplateName'] = ts('Please provide a Template Name.');
}
}
if (($params['upload_type'] || file_exists(CRM_Utils_Array::value('tmp_name', $files['textFile']))) ||
(!$params['upload_type'] && $params['text_message'])
) {
if (!$params['upload_type']) {
$str = file_get_contents($files['textFile']['tmp_name']);
$name = $files['textFile']['name'];
}
else {
$str = $params['sms_text_message'];
$name = 'text message';
}
$dataErrors = array();
// Do a full token replacement on a dummy verp, the current
// contact and domain, and the first organization.
// here we make a dummy mailing object so that we
// can retrieve the tokens that we need to replace
// so that we do get an invalid token error
// this is qute hacky and I hope that there might
// be a suggestion from someone on how to
// make it a bit more elegant
$dummy_mail = new CRM_Mailing_BAO_Mailing();
$mess = "body_text";
$dummy_mail->$mess = $str;
$tokens = $dummy_mail->getTokens();
$str = CRM_Utils_Token::replaceSubscribeInviteTokens($str);
$str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens['text']);
$str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens['text']);
$str = CRM_Utils_Token::replaceOrgTokens($str, $org);
$str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']);
$str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens['text']);
$unmatched = CRM_Utils_Token::unmatchedTokens($str);
$contentCheck = CRM_Utils_String::htmlToText($str);
if (!empty($unmatched) && 0) {
foreach ($unmatched as $token) {
$dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
}
}
if (strlen($contentCheck) > CRM_SMS_Provider::MAX_SMS_CHAR) {
$dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)) . '</li>';
}
if (!empty($dataErrors)) {
$errors['textFile'] = ts('The following errors were detected in %1:', array(
1 => $name,
)) . ' <ul>' . implode('', $dataErrors) . '</ul>';
}
}
$templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates();
if (!empty($params['SMSsaveTemplate']) && in_array(CRM_Utils_Array::value('SMSsaveTemplateName', $params), $templateName)
) {
$errors['SMSsaveTemplate'] = ts('Duplicate Template Name.');
}
return empty($errors) ? TRUE : $errors;
}
/**
* Display Name of the form.
*
*
* @return string
*/
public function getTitle() {
return ts('SMS Content');
}
/**
* List available tokens for this form.
*
* @return array
*/
public function listTokens() {
$tokens = CRM_Core_SelectValues::contactTokens();
return $tokens;
}
}