/** * File for the CiviCRM APIv3 group functions * * @package CiviCRM_APIv3 * @subpackage API_pcpteams * @copyright CiviCRM LLC (c) 2004-2014 */ function civicrm_api3_pcpteams_create($params) { // since we are allowing html input from the user // we also need to purify it, so lets clean it up // $params['pcp_title'] = $pcp['title']; // $params['pcp_contact_id'] = $pcp['contact_id']; $htmlFields = array('intro_text', 'page_text', 'title'); foreach ($htmlFields as $field) { if (!empty($params[$field])) { $params[$field] = CRM_Utils_String::purifyHTML($params[$field]); } } $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($params['page_type']); $pcpBlock = new CRM_PCP_DAO_PCPBlock(); $pcpBlock->entity_table = $entity_table; $pcpBlock->entity_id = $params['page_id']; $pcpBlock->find(TRUE); $params['pcp_block_id'] = $pcpBlock->id; $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']); // 1 -> waiting review // 2 -> active / approved (default for now) $params['status_id'] = CRM_Utils_Array::value('status_id', $params, 2); // active by default for now $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 1); $pcp = CRM_Pcpteams_BAO_PCP::create($params, FALSE); //Custom Set $customFields = CRM_Core_BAO_CustomField::getFields('PCP', FALSE, FALSE, NULL, NULL, TRUE); $isCustomValueSet = FALSE; foreach ($customFields as $fieldID => $fieldValue) { list($tableName, $columnName, $cgId) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); if (!empty($params[$columnName]) || !empty($params["custom_{$fieldID}"])) { $isCustomValueSet = TRUE; //FIXME: to find out the custom value exists, set -1 as default now $params["custom_{$fieldID}_-1"] = !empty($params[$columnName]) ? $params[$columnName] : $params["custom_{$fieldID}"]; } } if ($isCustomValueSet) { $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $pcp->id, 'PCP'); CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pcp', $pcp->id); } //end custom set $values = array(); @_civicrm_api3_object_to_array_unique_fields($pcp, $values[$pcp->id]); return civicrm_api3_create_success($values, $params, 'Pcpteams', 'create'); }
/** * Run the page. * * This method is called after the page is created. It checks for the * type of action and executes that action. * Finally it calls the parent's run method. * * @return void */ public function run() { $session = CRM_Core_Session::singleton(); $config = CRM_Core_Config::singleton(); $permissionCheck = FALSE; $statusMessage = ''; if ($config->userFramework != 'Joomla') { $permissionCheck = CRM_Core_Permission::check('administer CiviCRM'); } //get the pcp id. $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); $prms = array('id' => $this->_id); CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo); $this->_component = $pcpInfo['page_type']; if (empty($pcpInfo)) { $statusMessage = ts('The personal campaign page you requested is currently unavailable.'); CRM_Core_Error::statusBounce($statusMessage, $config->userFrameworkBaseURL); } CRM_Utils_System::setTitle($pcpInfo['title']); $this->assign('pcp', $pcpInfo); $pcpStatus = CRM_Core_OptionGroup::values("pcp_status"); $approvedId = CRM_Core_OptionGroup::getValue('pcp_status', 'Approved', 'name'); // check if PCP is created by anonymous user $anonymousPCP = CRM_Utils_Request::retrieve('ap', 'Boolean', $this); if ($anonymousPCP) { $loginURL = $config->userSystem->getLoginURL(); $anonMessage = ts('Once you\'ve received your new account welcome email, you can <a href=%1>click here</a> to login and promote your campaign page.', array(1 => $loginURL)); CRM_Core_Session::setStatus($anonMessage, ts('Success'), 'success'); } else { $statusMessage = ts('The personal campaign page you requested is currently unavailable. However you can still support the campaign by making a contribution here.'); } $pcpBlock = new CRM_PCP_DAO_PCPBlock(); $pcpBlock->entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($pcpInfo['page_type']); $pcpBlock->entity_id = $pcpInfo['page_id']; $pcpBlock->find(TRUE); // Redirect back to source page in case of error. if ($pcpInfo['page_type'] == 'contribute') { $urlBase = 'civicrm/contribute/transact'; } elseif ($pcpInfo['page_type'] == 'event') { $urlBase = 'civicrm/event/register'; } if ($pcpInfo['status_id'] != $approvedId || !$pcpInfo['is_active']) { if ($pcpInfo['contact_id'] != $session->get('userID') && !$permissionCheck) { CRM_Core_Error::statusBounce($statusMessage, CRM_Utils_System::url($urlBase, "reset=1&id=" . $pcpInfo['page_id'], FALSE, NULL, FALSE, TRUE)); } } else { $getStatus = CRM_PCP_BAO_PCP::getStatus($this->_id, $this->_component); if (!$getStatus) { // PCP not enabled for this contribution page. Forward everyone to source page CRM_Core_Error::statusBounce($statusMessage, CRM_Utils_System::url($urlBase, "reset=1&id=" . $pcpInfo['page_id'], FALSE, NULL, FALSE, TRUE)); } } $default = array(); if ($pcpBlock->target_entity_type == 'contribute') { $urlBase = 'civicrm/contribute/transact'; } elseif ($pcpBlock->target_entity_type == 'event') { $urlBase = 'civicrm/event/register'; } if ($pcpBlock->entity_table == 'civicrm_event') { $page_class = 'CRM_Event_DAO_Event'; $this->assign('pageName', CRM_Event_PseudoConstant::event($pcpInfo['page_id'])); CRM_Core_DAO::commonRetrieveAll($page_class, 'id', $pcpInfo['page_id'], $default, array('start_date', 'end_date', 'registration_start_date', 'registration_end_date')); } elseif ($pcpBlock->entity_table == 'civicrm_contribution_page') { $page_class = 'CRM_Contribute_DAO_ContributionPage'; $this->assign('pageName', CRM_Contribute_PseudoConstant::contributionPage($pcpInfo['page_id'], TRUE)); CRM_Core_DAO::commonRetrieveAll($page_class, 'id', $pcpInfo['page_id'], $default, array('start_date', 'end_date')); } $pageInfo = $default[$pcpInfo['page_id']]; if ($pcpInfo['contact_id'] == $session->get('userID')) { $owner = $pageInfo; $owner['status'] = CRM_Utils_Array::value($pcpInfo['status_id'], $pcpStatus); $this->assign('owner', $owner); $link = CRM_PCP_BAO_PCP::pcpLinks(); $hints = array(CRM_Core_Action::UPDATE => ts('Change the content and appearance of your page'), CRM_Core_Action::DETACH => ts('Send emails inviting your friends to support your campaign!'), CRM_Core_Action::VIEW => ts('Copy this link to share directly with your network!'), CRM_Core_Action::BROWSE => ts('Update your personal contact information'), CRM_Core_Action::DISABLE => ts('De-activate the page (you can re-activate it later)'), CRM_Core_Action::ENABLE => ts('Activate the page (you can de-activate it later)'), CRM_Core_Action::DELETE => ts('Remove the page (this cannot be undone!)')); $replace = array('id' => $this->_id, 'block' => $pcpBlock->id, 'pageComponent' => $this->_component); if (!$pcpBlock->is_tellfriend_enabled || CRM_Utils_Array::value('status_id', $pcpInfo) != $approvedId) { unset($link['all'][CRM_Core_Action::DETACH]); } switch ($pcpInfo['is_active']) { case 1: unset($link['all'][CRM_Core_Action::ENABLE]); break; case 0: unset($link['all'][CRM_Core_Action::DISABLE]); break; } $this->assign('links', $link['all']); $this->assign('hints', $hints); $this->assign('replace', $replace); } $honor = CRM_PCP_BAO_PCP::honorRoll($this->_id); $entityFile = CRM_Core_BAO_File::getEntityFile('civicrm_pcp', $this->_id); if (!empty($entityFile)) { $fileInfo = reset($entityFile); $fileId = $fileInfo['fileID']; $image = '<img src="' . CRM_Utils_System::url('civicrm/file', "reset=1&id={$fileId}&eid={$this->_id}") . '" />'; $this->assign('image', $image); } $totalAmount = CRM_PCP_BAO_PCP::thermoMeter($this->_id); $achieved = round($totalAmount / $pcpInfo['goal_amount'] * 100, 2); if ($pcpBlock->is_active == 1) { $linkTextUrl = CRM_Utils_System::url('civicrm/contribute/campaign', "action=add&reset=1&pageId={$pcpInfo['page_id']}&component={$pcpInfo['page_type']}", TRUE, NULL, TRUE, TRUE); $this->assign('linkTextUrl', $linkTextUrl); $this->assign('linkText', $pcpBlock->link_text); } $this->assign('honor', $honor); $this->assign('total', $totalAmount ? $totalAmount : '0.0'); $this->assign('achieved', $achieved <= 100 ? $achieved : 100); if ($achieved <= 100) { $this->assign('remaining', 100 - $achieved); } // make sure that we are between contribution page start and end dates OR registration start date and end dates if they are set if ($pcpBlock->entity_table == 'civicrm_event') { $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('registration_start_date', $pageInfo)); $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('registration_end_date', $pageInfo)); } else { $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('start_date', $pageInfo)); $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $pageInfo)); } $now = time(); $validDate = TRUE; if ($startDate && $startDate >= $now) { $validDate = FALSE; } if ($endDate && $endDate < $now) { $validDate = FALSE; } $this->assign('validDate', $validDate); // form parent page url if ($action == CRM_Core_Action::PREVIEW) { $parentUrl = CRM_Utils_System::url($urlBase, "id={$pcpInfo['page_id']}&reset=1&action=preview", TRUE, NULL, TRUE, TRUE); } else { $parentUrl = CRM_Utils_System::url($urlBase, "id={$pcpInfo['page_id']}&reset=1", TRUE, NULL, TRUE, TRUE); } $this->assign('parentURL', $parentUrl); if ($validDate) { $contributionText = ts('Contribute Now'); if (!empty($pcpInfo['donate_link_text'])) { $contributionText = $pcpInfo['donate_link_text']; } $this->assign('contributionText', $contributionText); // we always generate urls for the front end in joomla if ($action == CRM_Core_Action::PREVIEW) { $url = CRM_Utils_System::url($urlBase, "id=" . $pcpBlock->target_entity_id . "&pcpId={$this->_id}&reset=1&action=preview", TRUE, NULL, TRUE, TRUE); } else { $url = CRM_Utils_System::url($urlBase, "id=" . $pcpBlock->target_entity_id . "&pcpId={$this->_id}&reset=1", TRUE, NULL, TRUE, TRUE); } $this->assign('contributeURL', $url); } // we do not want to display recently viewed items, so turn off $this->assign('displayRecent', FALSE); $single = $permission = FALSE; switch ($action) { case CRM_Core_Action::BROWSE: $subForm = 'PCPAccount'; $form = "CRM_PCP_Form_{$subForm}"; $single = TRUE; break; case CRM_Core_Action::UPDATE: $subForm = 'Campaign'; $form = "CRM_PCP_Form_{$subForm}"; $single = TRUE; break; } $userID = $session->get('userID'); //make sure the user has "administer CiviCRM" permission //OR has created the PCP if (CRM_Core_Permission::check('administer CiviCRM') || $userID && CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id') == $userID) { $permission = TRUE; } if ($single && $permission) { $controller = new CRM_Core_Controller_Simple($form, $subForm, $action); $controller->set('id', $this->_id); $controller->set('single', TRUE); $controller->process(); return $controller->run(); } $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&id=' . $this->_id)); parent::run(); }
/** * Process the form submission. * * * @return void */ public function postProcess() { $params = $this->controller->exportValues($this->_name); $checkBoxes = array('is_thermometer', 'is_honor_roll', 'is_active', 'is_notify'); foreach ($checkBoxes as $key) { if (!isset($params[$key])) { $params[$key] = 0; } } $session = CRM_Core_Session::singleton(); $contactID = isset($this->_contactID) ? $this->_contactID : $session->get('userID'); if (!$contactID) { $contactID = $this->get('contactID'); } $params['title'] = $params['pcp_title']; $params['intro_text'] = $params['pcp_intro_text']; $params['contact_id'] = $contactID; $params['page_id'] = $this->get('component_page_id') ? $this->get('component_page_id') : $this->_contriPageId; $params['page_type'] = $this->_component; // since we are allowing html input from the user // we also need to purify it, so lets clean it up $htmlFields = array('intro_text', 'page_text', 'title'); foreach ($htmlFields as $field) { if (!empty($params[$field])) { $params[$field] = CRM_Utils_String::purifyHTML($params[$field]); } } $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($params['page_type']); $pcpBlock = new CRM_PCP_DAO_PCPBlock(); $pcpBlock->entity_table = $entity_table; $pcpBlock->entity_id = $params['page_id']; $pcpBlock->find(TRUE); $params['pcp_block_id'] = $pcpBlock->id; $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']); $approval_needed = $pcpBlock->is_approval_needed; $approvalMessage = NULL; if ($this->get('action') & CRM_Core_Action::ADD) { $params['status_id'] = $approval_needed ? 1 : 2; $approvalMessage = $approval_needed ? ts('but requires administrator review before you can begin promoting your campaign. You will receive an email confirmation shortly which includes a link to return to this page.') : ts('and is ready to use.'); } $params['id'] = $this->_pageId; $pcp = CRM_PCP_BAO_PCP::add($params, FALSE); //create page in wordpress create_wp_campaign($pcp->title, $pcp->contact_id, $pcp->id); CRM_Core_Error::debug_log_message("Calling create_wp_campaign.... Params title: {$pcp->title}, contact_id: {$pcp->contact_id}, pcp_id: {$pcp->id} "); // add attachments as needed CRM_Core_BAO_File::formatAttachment($params, $params, 'civicrm_pcp', $pcp->id); $pageStatus = isset($this->_pageId) ? ts('updated') : ts('created'); $statusId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcp->id, 'status_id'); //send notification of PCP create/update. $pcpParams = array('entity_table' => $entity_table, 'entity_id' => $pcp->page_id); $notifyParams = array(); $notifyStatus = ""; CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $pcpParams, $notifyParams, array('notify_email')); if ($emails = $pcpBlock->notify_email) { $this->assign('pcpTitle', $pcp->title); if ($this->_pageId) { $this->assign('mode', 'Update'); } else { $this->assign('mode', 'Add'); } $pcpStatus = CRM_Core_OptionGroup::getLabel('pcp_status', $statusId); $this->assign('pcpStatus', $pcpStatus); $this->assign('pcpId', $pcp->id); $supporterUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$pcp->contact_id}", TRUE, NULL, FALSE, FALSE); $this->assign('supporterUrl', $supporterUrl); $supporterName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $pcp->contact_id, 'display_name'); $this->assign('supporterName', $supporterName); if ($this->_component == 'contribute') { $pageUrl = CRM_Utils_System::url('civicrm/contribute/transact', "reset=1&id={$pcpBlock->entity_id}", TRUE, NULL, FALSE, TRUE); $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $pcpBlock->entity_id, 'title'); } elseif ($this->_component == 'event') { $pageUrl = CRM_Utils_System::url('civicrm/event', "reset=1&id={$pcpBlock->entity_id}", TRUE, NULL, FALSE, TRUE); $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $pcpBlock->entity_id, 'title'); } $this->assign('contribPageUrl', $pageUrl); $this->assign('contribPageTitle', $contribPageTitle); $managePCPUrl = CRM_Utils_System::url('civicrm/admin/pcp', "reset=1", TRUE, NULL, FALSE, FALSE); $this->assign('managePCPUrl', $managePCPUrl); //get the default domain email address. list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); if (!$domainEmailAddress || $domainEmailAddress == '*****@*****.**') { $fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1'); CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM » Communications » FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl))); } //if more than one email present for PCP notification , //first email take it as To and other as CC and First email //address should be sent in users email receipt for //support purpose. $emailArray = explode(',', $emails); $to = $emailArray[0]; unset($emailArray[0]); $cc = implode(',', $emailArray); list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(array('groupName' => 'msg_tpl_workflow_contribution', 'valueName' => 'pcp_notify', 'contactId' => $contactID, 'from' => "{$domainEmailName} <{$domainEmailAddress}>", 'toEmail' => $to, 'cc' => $cc)); if ($sent) { $notifyStatus = ts('A notification email has been sent to the site administrator.'); } } CRM_Core_BAO_File::processAttachment($params, 'civicrm_pcp', $pcp->id); // send email notification to supporter, if initial setup / add mode. if (!$this->_pageId) { CRM_PCP_BAO_PCP::sendStatusUpdate($pcp->id, $statusId, TRUE, $this->_component); if ($approvalMessage && CRM_Utils_Array::value('status_id', $params) == 1) { $notifyStatus .= ts(' You will receive a second email as soon as the review process is complete.'); } } //check if pcp created by anonymous user $anonymousPCP = 0; if (!$session->get('userID')) { $anonymousPCP = 1; } CRM_Core_Session::setStatus(ts("Your Personal Campaign Page has been %1 %2 %3", array(1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus)), '', 'info'); if (!$this->_pageId) { $session->pushUserContext(CRM_Utils_System::url('civicrm/pcp/info', "reset=1&id={$pcp->id}&ap={$anonymousPCP}")); } elseif ($this->_context == 'dashboard') { $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1')); } }
static function getPcpBlockId($eventId, $component = 'event') { if (empty($eventId)) { return null; } $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($component); $pcpBlock = new CRM_PCP_DAO_PCPBlock(); $pcpBlock->entity_table = $entity_table; $pcpBlock->entity_id = $eventId; $pcpBlock->find(TRUE); return $pcpBlock->id; }