예제 #1
0
 /**
  * Create a Volunteer Project
  *
  * Takes an associative array and creates a Project object. This function is
  * invoked from within the web form layer and also from the API layer. Allows
  * the creation of project contacts, e.g.:
  *
  * $params['project_contacts'] = array(
  *   $relationship_type_name_or_id => $arr_contact_ids,
  * );
  *
  * @param array   $params      an assoc array of name/value pairs
  *
  * @return CRM_Volunteer_BAO_Project object
  * @access public
  * @static
  */
 static function create(array $params)
 {
     $projectId = CRM_Utils_Array::value('id', $params);
     $op = empty($projectId) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE;
     if (!empty($params['check_permissions']) && !CRM_Volunteer_Permission::checkProjectPerms($op, $projectId)) {
         CRM_Utils_System::permissionDenied();
         // FIXME: If we don't return here, the script keeps executing. This is not
         // what I expect from CRM_Utils_System::permissionDenied().
         return FALSE;
     }
     // check required params
     if (!self::dataExists($params)) {
         CRM_Core_Error::fatal('Not enough data to create volunteer project object.');
     }
     // default to active unless explicitly turned off
     $params['is_active'] = CRM_Utils_Array::value('is_active', $params, TRUE);
     $project = new CRM_Volunteer_BAO_Project();
     $project->copyValues($params);
     $project->save();
     $existingContacts = CRM_Volunteer_BAO_Project::getContactsNestedByRelationship($project->id);
     $projectContacts = CRM_Utils_Array::value('project_contacts', $params, array());
     foreach ($projectContacts as $relationshipType => &$contactIds) {
         $contactIds = CRM_Volunteer_BAO_Project::validateContactFormat($contactIds);
         foreach ($contactIds as $id) {
             if (!array_key_exists($relationshipType, $existingContacts) || !in_array($id, $existingContacts[$relationshipType])) {
                 civicrm_api3('VolunteerProjectContact', 'create', array('contact_id' => $id, 'project_id' => $project->id, 'relationship_type_id' => $relationshipType));
             }
         }
     }
     $project->contacts = $projectContacts;
     $profiles = CRM_Utils_Array::value('profiles', $params, array());
     foreach ($profiles as $profile) {
         $profile['is_active'] = 1;
         $profile['module'] = "CiviVolunteer";
         $profile['entity_table'] = "civicrm_volunteer_project";
         $profile['entity_id'] = $project->id;
         $result = civicrm_api3('UFJoin', 'create', $profile);
         if ($result['is_error'] == 0) {
             $project->profileIds[] = $result['values'][0]['id'];
         }
     }
     if ($op === CRM_Core_Action::UPDATE && array_key_exists('campaign_id', $params)) {
         $project->updateAssociatedActivities();
     }
     return $project;
 }