GrantProgram.php 10.4 KB
Newer Older
1 2 3 4
<?php

/*
 +--------------------------------------------------------------------+
5
 | CiviCRM version 4.3                                                |
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2011                                |
 +--------------------------------------------------------------------+
 | 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-2011
 * $Id$
 *
 */


/**
 * This class contains  grant program related functions.
 */
41 42 43 44 45 46 47
class CRM_Grant_BAO_GrantProgram extends CRM_Grant_DAO_GrantProgram {
  /**
   * class constructor
   */
  function __construct() {
    parent::__construct();
  }
48

Pradeep Nayak's avatar
Pradeep Nayak committed
49 50 51 52 53 54 55 56 57
  /**
   * grant program status
   *
   * @var array
   * @static
   */
  private static $grantProgramStatus; 


58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  /**
   * Takes a bunch of params that are needed to match certain criteria and
   * retrieves the relevant objects. It also stores all the retrieved
   * values in the default array
   *
   * @param array $params   (reference ) an assoc array of name/value pairs
   * @param array $defaults (reference ) an assoc array to hold the flattened values
   *
   * @return object CRM_Contribute_DAO_GrantProgram object on success, null otherwise
   * @access public
   * @static
   */
  static function retrieve(&$params, &$defaults) {
    $program = new CRM_Grant_DAO_GrantProgram();
    $program->copyValues($params);
    if ($program->find(TRUE)) {
      CRM_Core_DAO::storeValues($program, $defaults);
      return $program;
76
    }
77 78
    return NULL;
  }
79

80 81 82 83 84 85 86 87 88 89 90
  /**
   * Function  to delete Grant Program
   * 
   * @param  int  $grantProgramID     ID of the par service fee to be deleted.
   * 
   * @access public
   * @static
   */
  static function del($grantProgramID) {
    if (!$grantProgramID) {
      CRM_Core_Error::fatal(ts('Invalid value passed to delete function'));
91 92
    }
    
93 94 95 96
    $dao = new CRM_Grant_DAO_GrantProgram();
    $dao->id = $grantProgramID;
    if (!$dao->find(TRUE)) {
      return NULL;
97
    }
98 99
    $dao->delete();
  }
100

101 102 103 104 105 106 107 108 109
  static function getOptionValueID($optioGroupID, $value) {
    $query = "SELECT id FROM civicrm_option_value WHERE  option_group_id = {$optioGroupID} AND value = {$value} ";
    return CRM_Core_DAO::singleValueQuery($query);
  }
    
  static function getOptionValue($id) {
    $query = "SELECT value FROM civicrm_option_value WHERE id = {$id} ";
    return CRM_Core_DAO::singleValueQuery($query);
  }
110

111 112 113 114
  static function getOptionName ($id) {
    $query = "SELECT label FROM civicrm_option_value WHERE id = {$id} ";
    return CRM_Core_DAO::singleValueQuery($query);
  }
115

116 117 118 119
  static function grantPrograms($id = NULL) {
    $where = '';
    if (!empty($id)) {
      $where = "WHERE id = {$id}";
120
    }
121 122 123 124 125 126 127 128
    $query = "SELECT id, label FROM civicrm_grant_program {$where}";
    $dao = CRM_Core_DAO::executeQuery($query);
    while ($dao->fetch()) {
      if (!empty($id)) {
        $grantPrograms = $dao->label;
      } 
      else {
        $grantPrograms[$dao->id] = $dao->label;
129
      }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    }
    return $grantPrograms;
  }  

  static function contributionTypes() {
    $typeDao = new CRM_Financial_DAO_FinancialType();
    $typeDao->find();
    while ($typeDao->fetch()) {
      $contributionTypes[$typeDao->id] = $typeDao->name;
    }
    return $contributionTypes;
  }
    
  static function create(&$params, &$ids) {
    if (empty($params)) {
      return;
    }
    $moneyFields = array( 
      'total_amount',
      'remainder_amount' 
    );
    foreach ($moneyFields as $field) {
      if (isset($params[$field])) {
        $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
154
      }
155 156 157
    }
    // convert dates to mysql format
    $dates = array('allocation_date');
158
        
159 160 161
    foreach ($dates as $date) {
      if (isset( $params[$date])) {
        $params[$date] = CRM_Utils_Date::processDate($params[$date], NULL, TRUE);
162
      }
163 164 165
    }
    $grantProgram = new CRM_Grant_DAO_GrantProgram();
    $grantProgram->id = CRM_Utils_Array::value('grant_program', $ids);
166
        
167
    $grantProgram->copyValues($params);
168
        
169 170
    return $result = $grantProgram->save();
  }
171

172 173 174 175
  public function getDisplayName($id) {
    $sql = "SELECT display_name FROM civicrm_contact WHERE civicrm_contact.id = $id ";
    return CRM_Core_DAO::singleValueQuery($sql);
  }
176 177
     
     
178 179
  public function getAddress($id, $locationTypeID = NULL) {
    $sql = "
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
   SELECT civicrm_contact.id as contact_id,
          civicrm_address.street_address as street_address,
          civicrm_address.supplemental_address_1 as supplemental_address_1,
          civicrm_address.supplemental_address_2 as supplemental_address_2,
          civicrm_address.city as city,
          civicrm_address.postal_code as postal_code,
          civicrm_address.postal_code_suffix as postal_code_suffix,
          civicrm_state_province.abbreviation as state,
          civicrm_country.name as country,
          civicrm_location_type.name as location_type
     FROM civicrm_contact
LEFT JOIN civicrm_address ON civicrm_address.contact_id = civicrm_contact.id
LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id
LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
LEFT JOIN civicrm_location_type ON civicrm_location_type.id = civicrm_address.location_type_id
WHERE civicrm_contact.id = $id ";

197 198 199 200 201 202 203
    $params = array();
    if (!$locationTypeID) {
      $sql .= " AND civicrm_address.is_primary = 1";
    } 
    else {
      $sql .= " AND civicrm_address.location_type_id = %1";
      $params[1] = array($locationTypeID, 'Integer');
204
    }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    $dao = CRM_Core_DAO::executeQuery($sql, $params);
    $location = array();
    $config = CRM_Core_Config::singleton();
    while ($dao->fetch()) {
      $address = '';
      CRM_Utils_String::append( 
        $address, ', ',
        array( 
          $dao->street_address,
          $dao->supplemental_address_1,
          $dao->supplemental_address_2,
          $dao->city,
          $dao->state,
          $dao->postal_code,
          $dao->country
        ) 
      );
      $location['address'] = addslashes($address);
    } 
    return $location;
Pradeep Nayak's avatar
Pradeep Nayak committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  }  
  /**
   * Get all the n grant program statuses
   *
   * @access public
   * @return array - array reference of all grant program statuses if any
   * @static
   */
  public static function &grantProgramStatus($id = NULL) {
    if (!self::$grantProgramStatus) {
      self::$grantProgramStatus = array();
      self::$grantProgramStatus = CRM_Core_OptionGroup::values('grant_program_status');
    }
    if($id) {
      return self::$grantProgramStatus[$id];
    }    
    return self::$grantProgramStatus;
242
  }
Pradeep Nayak's avatar
Pradeep Nayak committed
243 244 245 246 247 248 249 250
  
  
  static function getGrantPrograms($id = NULL) {
    $grantPrograms = array();
    $where = ' WHERE is_active = 1';
    if (!empty($id)) {
      $where .= " AND id = {$id}";
    }
251
    $query = "SELECT id, name FROM civicrm_grant_program " . $where;
Pradeep Nayak's avatar
Pradeep Nayak committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    $dao = CRM_Core_DAO::executeQuery($query);
    while ($dao->fetch()) {
      $grantPrograms[$dao->id] = $dao->name;
    }
    return $grantPrograms;
  }
    
  static function getGrants($params) {
    if (!empty($params)) {
      $where = "WHERE "; 
      foreach ($params as $key => $value) {
        if ($key == 'status_id') {
          $where .= "{$key} IN ( {$value} ) AND ";
        } 
        else {
          if (strstr($value, 'NULL')) {
            $where .= "{$key} IS {$value} AND ";
          } 
          else {
            $where .= "{$key} = '{$value}' AND ";
          }
        }
      }
      $where = rtrim($where ," AND ");
      $query = "SELECT * FROM civicrm_grant {$where} ORDER BY application_received_date ASC";
      $dao = CRM_Core_DAO::executeQuery($query);
      while ($dao->fetch()) {
        $grants[$dao->id]['assessment'] = $dao->assessment;
        $grants[$dao->id]['amount_total'] = $dao->amount_total;
        $grants[$dao->id]['amount_requested'] = $dao->amount_requested;
        $grants[$dao->id]['amount_granted'] = $dao->amount_granted;
        $grants[$dao->id]['status_id'] = $dao->status_id;
        $grants[$dao->id]['contact_id'] = $dao->contact_id;
        $grants[$dao->id]['grant_id'] = $dao->id;     
      }
    }
    return $grants;
  }
    
  static function sendMail($contactID, &$values, $grantStatus) {
292
    $value = array();
Pradeep Nayak's avatar
Pradeep Nayak committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    if (CRM_Utils_Array::value('is_auto_email', $values)) {
      list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
      if (isset($email)) {
        $valueName = strtolower($grantStatus);
        if ($grantStatus == 'Awaiting Information') {
          $explode = explode(' ', $grantStatus);
          $valueName = strtolower($explode[0]) . '_info';
        }
        $sendTemplateParams = array(
          'groupName' => 'msg_tpl_workflow_grant',
          'valueName' => 'grant_'.$valueName,
          'contactId' => $contactID,
          'tplParams' => array(
            'email' => $email,
           ),
          'PDFFilename' => '',
        );
        $sendTemplateParams['from'] = $email;
        $sendTemplateParams['toName'] = $displayName;
        $sendTemplateParams['toEmail'] = $email;
        $sendTemplateParams['autoSubmitted'] = TRUE;
        CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams);
      }
    }
  }    
318
}