| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class PCPBlock |
| 5 | */ |
| 6 | class PCPBlock extends PHPUnit_Framework_Testcase { |
| 7 | /** |
| 8 | * Helper function to create a PCP Block for Contribution Page |
| 9 | * |
| 10 | * @param int $contributionPageId |
| 11 | * Id of the Contribution Page. |
| 12 | * to be deleted |
| 13 | * @return array |
| 14 | * of created pcp block |
| 15 | */ |
| 16 | public function create($contributionPageId) { |
| 17 | $profileParams = array( |
| 18 | 'group_type' => 'Individual,Contact', |
| 19 | 'title' => 'Test Supprorter Profile', |
| 20 | 'help_pre' => 'Profle to PCP Contribution', |
| 21 | 'is_active' => 1, |
| 22 | 'is_cms_user' => 2, |
| 23 | ); |
| 24 | |
| 25 | $ufGroup = civicrm_api('uf_group', 'create', $profileParams); |
| 26 | $profileId = $ufGroup['id']; |
| 27 | |
| 28 | $fieldsParams = array( |
| 29 | array( |
| 30 | 'field_name' => 'first_name', |
| 31 | 'field_type' => 'Individual', |
| 32 | 'visibility' => 'Public Pages and Listings', |
| 33 | 'weight' => 1, |
| 34 | 'label' => 'First Name', |
| 35 | 'is_required' => 1, |
| 36 | 'is_active' => 1, |
| 37 | ), |
| 38 | array( |
| 39 | 'field_name' => 'last_name', |
| 40 | 'field_type' => 'Individual', |
| 41 | 'visibility' => 'Public Pages and Listings', |
| 42 | 'weight' => 2, |
| 43 | 'label' => 'Last Name', |
| 44 | 'is_required' => 1, |
| 45 | 'is_active' => 1, |
| 46 | ), |
| 47 | array( |
| 48 | 'field_name' => 'email', |
| 49 | 'field_type' => 'Contact', |
| 50 | 'visibility' => 'Public Pages and Listings', |
| 51 | 'weight' => 3, |
| 52 | 'label' => 'Email', |
| 53 | 'is_required' => 1, |
| 54 | 'is_active' => 1, |
| 55 | ), |
| 56 | ); |
| 57 | |
| 58 | foreach ($fieldsParams as $value) { |
| 59 | // we assume api v3. |
| 60 | $value['version'] = 3; |
| 61 | $value['uf_group_id'] = $profileId; |
| 62 | $ufField = civicrm_api('uf_field', 'create', $value); |
| 63 | } |
| 64 | |
| 65 | $joinParams = array( |
| 66 | 'module' => 'Profile', |
| 67 | 'entity_table' => 'civicrm_contribution_page', |
| 68 | 'entity_id' => 1, |
| 69 | 'weight' => 1, |
| 70 | 'uf_group_id' => $profileId, |
| 71 | 'is_active' => 1, |
| 72 | ); |
| 73 | $ufJoin = civicrm_api('uf_join', 'create', $joinParams); |
| 74 | |
| 75 | $params = array( |
| 76 | 'entity_table' => 'civicrm_contribution_page', |
| 77 | 'entity_id' => $contributionPageId, |
| 78 | 'supporter_profile_id' => $profileId, |
| 79 | 'is_approval_needed' => 0, |
| 80 | 'is_tellfriend_enabled' => 0, |
| 81 | 'tellfriend_limit' => 0, |
| 82 | 'link_text' => 'Create your own Personal Campaign Page!', |
| 83 | 'is_active' => 1, |
| 84 | 'notify_email' => 'info@civicrm.org', |
| 85 | ); |
| 86 | require_once 'CRM/Contribute/BAO/PCP.php'; |
| 87 | $blockPCP = CRM_Contribute_BAO_PCP::add($params); |
| 88 | return array('blockId' => $blockPCP->id, 'profileId' => $profileId); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Helper function to delete a PCP related stuff viz. Profile, PCP Block Entry |
| 93 | * |
| 94 | * @param array key value pair |
| 95 | * pcpBlockId - id of the PCP Block Id, profileID - id of Supporter Profile |
| 96 | * to be deleted |
| 97 | * @return boolean |
| 98 | * true if success, false otherwise |
| 99 | */ |
| 100 | public function delete($params) { |
| 101 | |
| 102 | $delete_params = array('id' => $params['profileId']); |
| 103 | $resulProfile = civicrm_api('uf_group', 'delete', $delete_params); |
| 104 | |
| 105 | require_once 'CRM/Contribute/DAO/PCPBlock.php'; |
| 106 | $dao = new CRM_Contribute_DAO_PCPBlock(); |
| 107 | $dao->id = $params['blockId']; |
| 108 | if ($dao->find(TRUE)) { |
| 109 | $resultBlock = $dao->delete(); |
| 110 | } |
| 111 | if ($id = CRM_Utils_Array::value('pcpId', $params)) { |
| 112 | require_once 'CRM/Contribute/BAO/PCP.php'; |
| 113 | CRM_Contribute_BAO_PCP::delete($id); |
| 114 | } |
| 115 | if ($resulProfile && $resultBlock) { |
| 116 | return TRUE; |
| 117 | } |
| 118 | return FALSE; |
| 119 | } |
| 120 | } |