/**
  * Performs the search.
  *
  * Stashes the results in $this->searchResults.
  *
  * @return array $this->searchResults
  */
 public function search()
 {
     $projects = CRM_Volunteer_BAO_Project::retrieve($this->searchParams['project']);
     foreach ($projects as $project) {
         $results = array();
         $flexibleNeed = civicrm_api3('VolunteerNeed', 'getsingle', array('id' => $project->flexible_need_id));
         if ($flexibleNeed['visibility_id'] === CRM_Core_OptionGroup::getValue('visibility', 'public', 'name')) {
             $needId = $flexibleNeed['id'];
             $results[$needId] = $flexibleNeed;
         }
         $openNeeds = $project->open_needs;
         foreach ($openNeeds as $key => $need) {
             if ($this->needFitsSearchCriteria($need)) {
                 $results[$key] = $need;
             }
         }
         if (!empty($results)) {
             $this->projects[$project->id] = array();
         }
         $this->searchResults += $results;
     }
     $this->getSearchResultsProjectData();
     usort($this->searchResults, array($this, "usortDateAscending"));
     return $this->searchResults;
 }
 /**
  * Checks whether the logged in user has permission to perform an action
  * against a specified project.
  *
  * @param int $op
  *   See the constants in CRM_Core_Action.
  * @param int $projectId
  *   Required for some but not all operations.
  * @return boolean
  *   TRUE is the action is allowed; else FALSE.
  */
 public static function checkProjectPerms($op, $projectId = NULL)
 {
     $opsRequiringProjectId = array(CRM_Core_Action::UPDATE, CRM_Core_Action::DELETE);
     if (in_array($op, $opsRequiringProjectId) && empty($projectId)) {
         CRM_Core_Error::fatal('Missing required parameter Project ID');
     }
     $contactId = CRM_Core_Session::getLoggedInContactID();
     switch ($op) {
         case CRM_Core_Action::ADD:
             return self::check('create volunteer projects');
         case CRM_Core_Action::UPDATE:
             if (self::check('edit all volunteer projects')) {
                 return TRUE;
             }
             $projectOwners = CRM_Volunteer_BAO_Project::getContactsByRelationship($projectId, 'volunteer_owner');
             if (self::check('edit own volunteer projects') && in_array($contactId, $projectOwners)) {
                 return TRUE;
             }
             break;
         case CRM_Core_Action::DELETE:
             if (self::check('delete all volunteer projects')) {
                 return TRUE;
             }
             $projectOwners = CRM_Volunteer_BAO_Project::getContactsByRelationship($projectId, 'volunteer_owner');
             if (self::check('delete own volunteer projects') && in_array($contactId, $projectOwners)) {
                 return TRUE;
             }
             break;
         case CRM_Core_Action::VIEW:
             if (self::check('register to volunteer') || self::check('edit all volunteer projects')) {
                 return TRUE;
             }
     }
     return FALSE;
 }
 /**
  * create or update a Volunteer Commendation
  *
  * This function is invoked from within the web form layer
  *
  * @param array $params An assoc array of name/value pairs
  *  - aid: activity id of an existing commendation to update
  *  - cid: id of contact to be commended
  *  - vid: id of project for which contact is to be commended
  *  - details: text about the contact's exceptional volunteerism
  * @see self::requiredParamsArePresent for rules re required params
  * @return array Result of api.activity.create
  * @access public
  * @static
  */
 public static function create(array $params)
 {
     // check required params
     if (!self::requiredParamsArePresent($params)) {
         CRM_Core_Error::fatal('Not enough data to create commendation object.');
     }
     $activity_statuses = CRM_Activity_BAO_Activity::buildOptions('status_id', 'create');
     $api_params = array('activity_type_id' => self::getActivityTypeId(), 'status_id' => CRM_Utils_Array::key('Completed', $activity_statuses));
     $aid = CRM_Utils_Array::value('aid', $params);
     if ($aid) {
         $api_params['id'] = $aid;
     }
     $cid = CRM_Utils_Array::value('cid', $params);
     if ($cid) {
         $api_params['target_contact_id'] = $cid;
     }
     $vid = CRM_Utils_Array::value('vid', $params);
     if ($vid) {
         $project = CRM_Volunteer_BAO_Project::retrieveByID($vid);
         $api_params['subject'] = ts('Volunteer Commendation for %1', array('1' => $project->title, 'domain' => 'org.civicrm.volunteer'));
         $customFieldSpec = self::getCustomFields();
         $volunteer_project_id_field_name = $customFieldSpec['volunteer_project_id']['custom_n'];
         $api_params[$volunteer_project_id_field_name] = $vid;
     }
     if (array_key_exists('details', $params)) {
         $api_params['details'] = CRM_Utils_Array::value('details', $params);
     }
     return civicrm_api3('Activity', 'create', $api_params);
 }
 protected function getProject()
 {
     if ($this->_project === NULL) {
         $this->_project = current(CRM_Volunteer_BAO_Project::retrieve(array('entity_id' => $this->getEntityId(), 'entity_table' => CRM_Event_DAO_Event::$_tableName)));
     }
     return $this->_project;
 }
/**
 * Returns array of projects matching a set of one or more project properties
 *
 * @param array $params  Array of one or more valid
 *                       property_name=>value pairs. If $params is set
 *                       as null, all projects will be returned
 *
 * @return array  Array of matching projects
 * {@getfields volunteer_project_get}
 * @access public
 */
function civicrm_api3_volunteer_project_get($params)
{
    $result = CRM_Volunteer_BAO_Project::retrieve($params);
    foreach ($result as $k => $dao) {
        $result[$k] = $dao->toArray();
    }
    return civicrm_api3_create_success($result, $params, 'VolunteerProject', 'get');
}
示例#6
0
 function testProjectRetrieveByID()
 {
     $project = CRM_Core_DAO::createTestObject('CRM_Volunteer_BAO_Project');
     $this->assertObjectHasAttribute('id', $project, 'Failed to prepopulate Volunteer Project');
     $projectRetrieved = CRM_Volunteer_BAO_Project::retrieveByID($project->id);
     // note: a strict comparison doesn't work: the first value is an int and the
     // second is a string; not sure where this occurs, but seems worth a look...
     $this->assertTrue($project->id == $projectRetrieved->id, 'CRM_Volunteer_BAO_Project::retrieveByID failed');
 }
示例#7
0
 protected function saveProject($params)
 {
     $this->minimumProjectParams($params);
     $this->_project = CRM_Volunteer_BAO_Project::create($params);
     // if we created a project:
     if (!key_exists('id', $params)) {
         $form = $this->getSubmitValues();
         if (CRM_Utils_Array::value('is_active', $form, 0) === '1') {
             // create the flexible need
             $need = array('project_id' => $this->_project->id, 'is_flexible' => '1', 'visibility_id' => CRM_Core_OptionGroup::getValue('visibility', 'public', 'name'));
             CRM_Volunteer_BAO_Need::create($need);
         }
     }
     return $this->_project;
 }
 /**
  * Builds the page.
  */
 public function run()
 {
     $this->projectId = CRM_Utils_Request::retrieve('project_id', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
     $this->project = CRM_Volunteer_BAO_Project::retrieveByID($this->projectId);
     CRM_Utils_System::setTitle(ts('Volunteer Roster for %1', array(1 => $this->project->title, 'domain' => 'org.civicrm.volunteer')));
     $this->fetchAssignments();
     $sortedAssignments = $this->getAssignmentsGroupedByTime();
     $this->assign('sortedResults', $sortedAssignments);
     if (!count($sortedAssignments)) {
         CRM_Core_Session::setStatus(ts('No volunteers have been assigned to this project yet!', array('domain' => 'org.civicrm.volunteer')), '', 'no-popup');
     }
     $this->todaysDate = new DateTime();
     $this->todaysDate->setTime(0, 0, 0);
     // just the date.
     $this->assign('endDate', $this->todaysDate->format('Y-m-d'));
     CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.volunteer', 'js/roster.js', 0, 'html-header');
     parent::run();
 }
 /**
  * Performs the search.
  *
  * Stashes the results in $this->searchResults.
  *
  * @return array $this->searchResults
  */
 public function search()
 {
     $projects = CRM_Volunteer_BAO_Project::retrieve($this->searchParams['project']);
     foreach ($projects as $project) {
         $openNeeds = $project->open_needs;
         if (empty($openNeeds)) {
             continue;
         }
         foreach ($openNeeds as $key => $need) {
             if (!$this->needFitsSearchCriteria($need)) {
                 unset($openNeeds[$key]);
             } elseif (!array_key_exists($project->id, $this->projects)) {
                 $this->projects[$project->id] = array();
             }
         }
         $this->searchResults += $openNeeds;
     }
     $this->getSearchResultsProjectData();
     return $this->searchResults;
 }
示例#10
0
 /**
  * Function to process the form. Enables/disables Volunteer Project. If the
  * Project does not already exist, it is created, along with a "flexible" Need.
  *
  * @access public
  *
  * @return None
  */
 public function postProcess()
 {
     $form = $this->exportValues();
     $form['is_active'] = CRM_Utils_Array::value('is_active', $form, 0);
     $params = array('entity_id' => $this->_id, 'entity_table' => CRM_Event_DAO_Event::$_tableName);
     // see if this project already exists
     $projects = CRM_Volunteer_BAO_Project::retrieve($params);
     if (count($projects)) {
         // force an update rather than an insert
         $params['id'] = current($projects)->id;
     }
     // save the project record
     $params += array('is_active' => $form['is_active'], 'target_contact_id' => $form['target_contact_id']);
     $project = CRM_Volunteer_BAO_Project::create($params);
     // if the project doesn't already exist and the user enabled vol management,
     // create the flexible need
     if (count($projects) !== 1 && $form['is_active'] === '1') {
         $need = array('project_id' => $project->id, 'is_flexible' => '1', 'visibility_id' => CRM_Core_OptionGroup::getValue('visibility', 'public', 'name'));
         CRM_Volunteer_BAO_Need::create($need);
     }
     parent::endPostProcess();
 }
/**
 * Callback for core Activity view and form
 *
 * Display user-friendly label for Need ID rather than an integer, or hide
 * the field altogether, depending on context.
 */
function _volunteer_civicrm_buildForm_CRM_Activity_Form_Activity($formName, &$form)
{
    // determine name that the Volunteer Need ID field would be given in this form
    $custom_group = CRM_Volunteer_BAO_Assignment::getCustomGroup();
    $custom_fields = CRM_Volunteer_BAO_Assignment::getCustomFields();
    $group_id = $custom_group['id'];
    $field_id = $custom_fields['volunteer_need_id']['id'];
    // element name varies depending on context
    $possible_element_names = array('custom_' . $field_id . '_1', 'custom_' . $field_id . '_-1');
    $element_name = NULL;
    foreach ($possible_element_names as $name) {
        if ($form->elementExists($name)) {
            $element_name = $name;
            break;
        }
    }
    // If it contains the Volunteer Need ID field, this is an edit form
    if (isset($element_name)) {
        $field = $form->getElement($element_name);
        $form->removeElement($element_name);
        // If need_id isn't set, do not re-add need_id field as a dropdown.
        // See http://issues.civicrm.org/jira/browse/VOL-24?focusedCommentId=53836#comment-53836
        if ($need_id = $field->_attributes['value']) {
            $need = civicrm_api3('VolunteerNeed', 'getsingle', array('id' => $need_id, 'return' => 'project_id'));
            $Project = CRM_Volunteer_BAO_Project::retrieveByID($need['project_id']);
            $needs = array();
            foreach ($Project->needs as $key => $value) {
                $needs[$key] = $value['role_label'] . ': ' . $value['display_time'];
            }
            asort($needs);
            $form->add('select', $element_name, $field->_label, $needs, TRUE);
        }
    } elseif (isset($form->_activityTypeName) && $form->_activityTypeName == 'Volunteer') {
        $custom = $form->get_template_vars('viewCustomData');
        if (!empty($custom[$group_id])) {
            $index = key($custom[$group_id]);
            if (!empty($custom[$group_id][$index]['fields'][$field_id]['field_value'])) {
                $value =& $custom[$group_id][$index]['fields'][$field_id]['field_value'];
                $need = civicrm_api3('VolunteerNeed', 'getsingle', array('id' => $value));
                $value = $need['role_label'] . ': ' . $need['display_time'];
                $form->assign('viewCustomData', $custom);
            }
        }
    }
}
 function postProcess()
 {
     $values = $this->exportValues();
     //Compose the profiles before we save tem.
     $profiles = array();
     foreach (CRM_Volunteer_BAO_Project::getProjectProfileAudienceTypes() as $audience) {
         $profiles[$audience['type']] = CRM_Utils_Array::value('volunteer_project_default_profiles_' . $audience['type'], $values);
     }
     civicrm_api3('Setting', 'create', array("volunteer_project_default_profiles" => $profiles));
     civicrm_api3('Setting', 'create', array("volunteer_project_default_campaign" => CRM_Utils_Array::value('volunteer_project_default_campaign', $values)));
     civicrm_api3('Setting', 'create', array("volunteer_project_default_locblock" => CRM_Utils_Array::value('volunteer_project_default_locblock', $values)));
     civicrm_api3('Setting', 'create', array("volunteer_project_default_is_active" => CRM_Utils_Array::value('volunteer_project_default_is_active', $values, 0)));
     // Todo: Create Composite data structure like we do for profiles
     civicrm_api3('Setting', 'create', array("volunteer_project_default_contacts" => CRM_Utils_Array::value('volunteer_project_default_contacts', $values)));
     //Whitelist/Blacklist settings
     civicrm_api3('Setting', 'create', array("volunteer_general_campaign_filter_type" => CRM_Utils_Array::value('volunteer_general_campaign_filter_type', $values)));
     civicrm_api3('Setting', 'create', array("volunteer_general_campaign_filter_list" => CRM_Utils_Array::value('volunteer_general_campaign_filter_list', $values, array())));
     CRM_Core_Session::setStatus(ts("Changes Saved", array('domain' => 'org.civicrm.volunteer')), "Saved", "success");
     parent::postProcess();
 }
/**
 * This function returns supporting data for various JavaScript-driven interfaces.
 *
 * The purpose of this API is to provide limited access to general-use APIs to
 * facilitate building user interfaces without having to grant users access to
 * APIs they otherwise shouldn't be able to access.
 *
 * @param array $params
 *   @see _civicrm_api3_volunteer_util_getsupportingdata_spec()
 * @return array
 */
function civicrm_api3_volunteer_util_getsupportingdata($params)
{
    $results = array();
    $controller = CRM_Utils_Array::value('controller', $params);
    if ($controller === 'VolunteerProject') {
        $relTypes = civicrm_api3('OptionValue', 'get', array('option_group_id' => CRM_Volunteer_BAO_ProjectContact::RELATIONSHIP_OPTION_GROUP, 'options' => array('limit' => 0)));
        $results['relationship_types'] = $relTypes['values'];
        $results['phone_types'] = CRM_Core_OptionGroup::values("phone_type", FALSE, FALSE, TRUE);
        //Fetch the Defaults from saved settings.
        $defaults = CRM_Volunteer_BAO_Project::composeDefaultSettingsArray();
        //StopGap because the interface for contacts didn't fit into scope
        if (!array_key_exists("relationships", $defaults)) {
            $defaults['relationships'] = _volunteerGetProjectRelationshipDefaults();
        }
        //Allow other extensions to modify the defaults
        CRM_Volunteer_Hook::projectDefaultSettings($defaults);
        $results['defaults'] = $defaults;
    }
    if ($controller === 'VolOppsCtrl') {
        $results['roles'] = CRM_Core_OptionGroup::values('volunteer_role', FALSE, FALSE, TRUE);
    }
    $results['use_profile_editor'] = CRM_Volunteer_Permission::check(array("access CiviCRM", "profile listings and forms"));
    $results['profile_audience_types'] = CRM_Volunteer_BAO_Project::getProjectProfileAudienceTypes();
    if (!$results['use_profile_editor']) {
        $profiles = civicrm_api3('UFGroup', 'get', array("return" => "title", "sequential" => 1, 'options' => array('limit' => 0)));
        $results['profile_list'] = $profiles['values'];
    }
    return civicrm_api3_create_success($results, "VolunteerUtil", "getsupportingdata", $params);
}
 /**
  * Function to set variables up before form is built
  *
  * @access public
  */
 function preProcess()
 {
     // VOL-71: permissions check is moved from XML to preProcess function to support
     // permissions-challenged Joomla instances
     if (CRM_Core_Config::singleton()->userPermissionClass->isModulePermissionSupported() && !CRM_Volunteer_Permission::check('register to volunteer')) {
         CRM_Utils_System::permissionDenied();
     }
     $vid = CRM_Utils_Request::retrieve('vid', 'Positive', $this, TRUE);
     $this->_project = CRM_Volunteer_BAO_Project::retrieveByID($vid);
     $this->setDestination();
     $this->assign('vid', $this->_project->id);
     if (empty($this->_project->needs)) {
         CRM_Core_Error::fatal('Project has no public volunteer needs enabled');
     }
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
     // current mode
     $this->_mode = $this->_action == CRM_Core_Action::PREVIEW ? 'test' : 'live';
     // get profile id
     try {
         $this->_ufgroup_id = civicrm_api3('UFGroup', 'getvalue', array('name' => 'volunteer_sign_up', 'return' => 'id'));
     } catch (Exception $e) {
         CRM_Core_Error::fatal('CiviVolunteer custom profile could not be found');
     }
 }
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return None
  */
 public function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     $validParams = self::getCompletedRows($params['field']);
     $count = 0;
     foreach ($validParams as $value) {
         if (!empty($value['activity_id'])) {
             // update the activity record
             $volunteer = array('status_id' => $value['volunteer_status'], 'id' => $value['activity_id'], 'time_completed_minutes' => CRM_Utils_Array::value('actual_duration', $value), 'time_scheduled_minutes' => CRM_Utils_Array::value('scheduled_duration', $value));
             CRM_Volunteer_BAO_Assignment::createVolunteerActivity($volunteer);
         } else {
             $flexibleNeedId = CRM_Volunteer_BAO_Project::getFlexibleNeedID($this->_vid);
             // create new Volunteer activity records
             $volunteer = array('assignee_contact_id' => $value['contact_id'], 'status_id' => $value['volunteer_status'], 'subject' => $this->_title . ' Volunteering', 'volunteer_need_id' => $flexibleNeedId, 'volunteer_role_id' => CRM_Utils_Array::value('volunteer_role', $value), 'time_completed_minutes' => CRM_Utils_Array::value('actual_duration', $value), 'time_scheduled_minutes' => CRM_Utils_Array::value('scheduled_duration', $value));
             if (!empty($value['start_date'])) {
                 $volunteer['activity_date_time'] = CRM_Utils_Date::processDate($value['start_date'], $value['start_date_time'], TRUE);
             }
             CRM_Volunteer_BAO_Assignment::createVolunteerActivity($volunteer);
         }
         $count++;
     }
     $statusMsg = ts('Volunteer hours have been logged.', array('domain' => 'org.civicrm.volunteer'));
     CRM_Core_Session::setStatus($statusMsg, ts('Saved', array('domain' => 'org.civicrm.volunteer')), 'success');
 }
 /**
  * Creates a volunteer activity
  *
  * Wrapper around activity create API. Volunteer field names are translated
  * to the custom_n format expected by the API. Key volunteer_need_id is
  * required in the params array.
  *
  * @param array $params An assoc array of name/value pairs
  * @return mixed Boolean FALSE on failure; activity_id on success
  */
 public static function createVolunteerActivity(array $params)
 {
     if (empty($params['id']) && empty($params['volunteer_need_id'])) {
         CRM_Core_Error::fatal('Mandatory key missing from params array: volunteer_need_id');
     }
     // Set default date role & duration if need is specified
     if (!empty($params['volunteer_need_id'])) {
         $need = civicrm_api3('volunteer_need', 'getsingle', array('id' => $params['volunteer_need_id']));
         $params['volunteer_role_id'] = CRM_Utils_Array::value('volunteer_role_id', $params, CRM_Utils_Array::value('role_id', $need));
         $params['time_scheduled_minutes'] = CRM_Utils_Array::value('time_scheduled_minutes', $params, CRM_Utils_Array::value('duration', $need));
         $projectContact = civicrm_api3('VolunteerProjectContact', 'get', array('project_id' => $need['project_id'], 'relationship_type_id' => 'volunteer_beneficiary'));
         foreach ($projectContact['values'] as $pc) {
             $params['target_contact_id'][] = $pc['contact_id'];
         }
         // Look up the base entity (e.g. event) as a fallback default
         if ((empty($need['start_time']) || empty($params['subject'])) && empty($params['id'])) {
             $project = CRM_Volunteer_BAO_Project::retrieveByID($need['project_id']);
             if (empty($params['activity_date_time']) && empty($params['id'])) {
                 // if the related entity doesn't provide a good default, use tomorrow
                 $tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));
                 $params['activity_date_time'] = CRM_Utils_Array::value('start_time', $project->getEntityAttributes(), $tomorrow);
             }
             if (empty($params['subject']) && empty($params['id'])) {
                 $params['subject'] = $project->title;
             }
         }
     }
     // Might as well sync these, but seems redundant
     if (!isset($params['duration']) && isset($params['time_completed_minutes'])) {
         $params['duration'] = $params['time_completed_minutes'];
     }
     $params['activity_type_id'] = self::getActivityTypeId();
     foreach (self::getCustomFields() as $fieldName => $field) {
         if (isset($params[$fieldName])) {
             $params['custom_' . $field['id']] = $params[$fieldName];
             unset($params[$fieldName]);
         }
     }
     $activity = civicrm_api3('Activity', 'create', $params);
     if (!empty($activity['id'])) {
         return $activity['id'];
     }
     return FALSE;
 }
 /**
  * Set $this->_destination, the URL to which the user should be redirected
  * after successfully submitting the sign-up form
  */
 protected function setDestination()
 {
     $path = $query = $fragment = NULL;
     $dest = CRM_Utils_Request::retrieve('dest', 'String', $this, FALSE);
     switch ($dest) {
         case 'event':
             // If only one project is associated with the form, send the user back
             // to that event form; otherwise, default to the vol opps page.
             if (count($this->projectIds) === 1) {
                 $eventId = CRM_Volunteer_BAO_Project::retrieveByID($this->projectIds[0])->entity_id;
                 $path = 'civicrm/event/info';
                 $query = "reset=1&id={$eventId}";
                 break;
             }
         case 'list':
         default:
             $path = 'civicrm/a/';
             $fragment = '/volunteer/opportunities';
     }
     $this->_destination = CRM_Utils_System::url($path, $query, FALSE, $fragment);
 }
 function testGetContactsByRelationship()
 {
     $contactId = 1;
     $relType = CRM_Core_OptionGroup::getValue(CRM_Volunteer_BAO_ProjectContact::RELATIONSHIP_OPTION_GROUP, 'volunteer_owner', 'name');
     $project = CRM_Core_DAO::createTestObject('CRM_Volunteer_BAO_Project');
     $this->assertObjectHasAttribute('id', $project, 'Failed to prepopulate Volunteer Project');
     $projectContact = CRM_Core_DAO::createTestObject('CRM_Volunteer_BAO_ProjectContact', array('contact_id' => $contactId, 'project_id' => $project->id, 'relationship_type_id' => $relType));
     $this->assertObjectHasAttribute('id', $projectContact, 'Failed to prepopulate Volunteer Project Contact');
     $contacts = CRM_Volunteer_BAO_Project::getContactsByRelationship($project->id, $relType);
     $this->assertTrue(in_array($contactId, $contacts));
 }
 /**
  * VOL-154: Verifies that, when a project's campaign is updated, the campaign
  * for each associated activity is as well.
  */
 function testProjectCampaignUpdate()
 {
     $testObjects = $this->_createTestObjects();
     CRM_Volunteer_BAO_Project::create(array('campaign_id' => $testObjects['campaign']->id, 'id' => $testObjects['project']->id));
     $updatedActivity = CRM_Volunteer_BAO_Assignment::findById($testObjects['activity']['id']);
     $this->assertEquals($testObjects['campaign']->id, $updatedActivity->campaign_id, 'Activity campaign was not updated with project campaign');
     // Test unsetting campaign from a project.
     CRM_Volunteer_BAO_Project::create(array('campaign_id' => '', 'id' => $testObjects['project']->id));
     $updatedActivity = CRM_Volunteer_BAO_Assignment::findById($testObjects['activity']['id']);
     $this->assertEquals('', $updatedActivity->campaign_id, 'Activity campaign was not updated with empty project campaign');
 }
 /**
  * Set default values for the Activity about to be created/updated.
  *
  * Called from self::createVolunteerActivity(), which checks for the existence
  * of necessary params; thus, no such checks are performed here.
  *
  * @param array $params
  *   @see self::createVolunteerActivity()
  * @return array
  *   Default parameters to use for api.activity.create
  */
 private static function setActivityDefaults(array $params)
 {
     $defaults = array();
     $op = empty($params['id']) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE;
     $need = civicrm_api3('volunteer_need', 'getsingle', array('id' => $params['volunteer_need_id']));
     $project = CRM_Volunteer_BAO_Project::retrieveByID($need['project_id']);
     $defaults['campaign_id'] = $project ? $project->campaign_id : '';
     // Force NULL campaign ids to be empty strings, since the API ignores NULL values.
     if (empty($defaults['campaign_id'])) {
         $defaults['campaign_id'] = '';
     }
     if ($op === CRM_Core_Action::ADD) {
         $defaults['volunteer_role_id'] = CRM_Utils_Array::value('role_id', $need);
         $defaults['time_scheduled_minutes'] = CRM_Utils_Array::value('duration', $need);
         $defaults['target_contact_id'] = CRM_Volunteer_BAO_Project::getContactsByRelationship($project->id, 'volunteer_beneficiary');
         // If the related entity doesn't provide a good default, use tomorrow.
         if (empty($params['activity_date_time'])) {
             $tomorrow = date('Y-m-d H:i:s', strtotime('tomorrow'));
             $defaults['activity_date_time'] = CRM_Utils_Array::value('start_time', $project->getEntityAttributes(), $tomorrow);
         }
         if (empty($params['subject'])) {
             $defaults['subject'] = $project->title;
         }
     }
     return $defaults;
 }
示例#21
0
 /**
  * Delete a need, reassign its activities to the project's default flexible need
  * @param $id
  * @return bool
  */
 static function del($id)
 {
     $need = civicrm_api3('volunteer_need', 'getsingle', array('id' => $id));
     // TODO: What do we do with associated activities when deleting a flexible need?
     if (empty($need['is_flexible'])) {
         // Lookup the flexible need
         $flexibleNeedId = CRM_Volunteer_BAO_Project::getFlexibleNeedID($need['project_id']);
         // Reassign any activities back to the flexible need
         $acts = civicrm_api3('volunteer_assignment', 'get', array('volunteer_need_id' => $id));
         $status = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Available');
         foreach ($acts['values'] as $act) {
             civicrm_api3('volunteer_assignment', 'create', array('id' => $act['id'], 'volunteer_need_id' => $flexibleNeedId, 'status_id' => $status, 'time_scheduled_minutes' => 0));
         }
     }
     $dao = new CRM_Volunteer_DAO_Need();
     $dao->id = $id;
     if ($dao->find()) {
         while ($dao->fetch()) {
             $dao->delete();
         }
     } else {
         return FALSE;
     }
     return TRUE;
 }
 /**
  * Initialize this object with provided values. This override adds a little
  * data massaging prior to calling its parent.
  *
  * @param mixed $params
  *   An associative array of name/value pairs or a CRM_Core_DAO object
  *
  * @return boolean      did we copy all null values into the object
  * @access public
  */
 public function copyValues(&$params)
 {
     if (is_a($params, 'CRM_Core_DAO')) {
         $params = get_object_vars($params);
     }
     if (array_key_exists('is_active', $params)) {
         /*
          * don't force is_active to have a value if none was set, to allow searches
          * where the is_active state of Projects is irrelevant
          */
         $params['is_active'] = CRM_Volunteer_BAO_Project::isOff($params['is_active']) ? 0 : 1;
     }
     return parent::copyValues($params);
 }
/**
 * Returns array of projects matching a set of one or more project properties
 *
 * @param array $params  Array of one or more valid
 *                       property_name=>value pairs. If $params is set
 *                       as null, all projects will be returned
 *
 * @return array  Array of matching projects
 * {@getfields volunteer_project_get}
 * @access public
 */
function civicrm_api3_volunteer_project_get($params)
{
    //If we are in an editing context only show projects they can edit.
    $context = CRM_Utils_Array::value('context', $params);
    if ($context === 'edit' && !CRM_Volunteer_Permission::check('edit all volunteer projects')) {
        if (!isset($params['project_contacts'])) {
            $params['project_contacts'] = array();
        }
        $params['project_contacts']['volunteer_owner'] = array(CRM_Core_Session::getLoggedInContactID());
        unset($params['context']);
    }
    $result = CRM_Volunteer_BAO_Project::retrieve($params);
    foreach ($result as $k => $bao) {
        $result[$k] = $bao->toArray();
        $result[$k]['entity_attributes'] = $bao->getEntityAttributes();
        $profiles = civicrm_api3("UFJoin", "get", array("entity_id" => $bao->id, "entity_table" => "civicrm_volunteer_project", "options" => array("limit" => 0), "sequential" => 1));
        $result[$k]['profiles'] = $profiles['values'];
    }
    return civicrm_api3_create_success($result, $params, 'VolunteerProject', 'get');
}
 /**
  * set variables up before form is built
  *
  * @access public
  */
 function preProcess()
 {
     // VOL-71: permissions check is moved from XML to preProcess function to support
     // permissions-challenged Joomla instances
     if (CRM_Core_Config::singleton()->userPermissionClass->isModulePermissionSupported() && !CRM_Volunteer_Permission::check('register to volunteer')) {
         CRM_Utils_System::permissionDenied();
     }
     $vid = CRM_Utils_Request::retrieve('vid', 'Positive', $this, TRUE);
     $this->_project = CRM_Volunteer_BAO_Project::retrieveByID($vid);
     $this->setDestination();
     $this->assign('vid', $this->_project->id);
     if (empty($this->_project->needs)) {
         CRM_Core_Error::fatal('Project has no public volunteer needs enabled');
     }
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
     // current mode
     $this->_mode = $this->_action == CRM_Core_Action::PREVIEW ? 'test' : 'live';
 }