First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,412 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| 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-2017
* $Id$
*
*/
class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant {
/**
* Static field for all the grant information that we can potentially export.
* @var array
*/
static $_exportableFields = NULL;
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Get events Summary.
*
*
* @param bool $admin
*
* @return array
* Array of event summary values
*/
public static function getGrantSummary($admin = FALSE) {
$query = "
SELECT status_id, count(id) as status_total
FROM civicrm_grant GROUP BY status_id";
$dao = CRM_Core_DAO::executeQuery($query);
$status = array();
$summary = array();
$summary['total_grants'] = NULL;
$status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id');
foreach ($status as $id => $name) {
$stats[$id] = array(
'label' => $name,
'total' => 0,
);
}
while ($dao->fetch()) {
$stats[$dao->status_id] = array(
'label' => $status[$dao->status_id],
'total' => $dao->status_total,
);
$summary['total_grants'] += $dao->status_total;
}
$summary['per_status'] = $stats;
return $summary;
}
/**
* Get events Summary.
*
*
* @return array
* Array of event summary values
*/
public static function getGrantStatusOptGroup() {
$params = array();
$params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
$defaults = array();
$og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults);
if (!$og) {
CRM_Core_Error::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
}
return $og;
}
/**
* Fetch object based on array of properties.
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
* @param array $defaults
* (reference ) an assoc array to hold the flattened values.
*
* @return CRM_Grant_BAO_ManageGrant
*/
public static function retrieve(&$params, &$defaults) {
$grant = new CRM_Grant_DAO_Grant();
$grant->copyValues($params);
if ($grant->find(TRUE)) {
CRM_Core_DAO::storeValues($grant, $defaults);
return $grant;
}
return NULL;
}
/**
* Add grant.
*
* @param array $params
* Reference array contains the values submitted by the form.
* @param array $ids
* Reference array contains the id.
*
*
* @return object
*/
public static function add(&$params, &$ids) {
if (!empty($ids['grant_id'])) {
CRM_Utils_Hook::pre('edit', 'Grant', $ids['grant_id'], $params);
}
else {
CRM_Utils_Hook::pre('create', 'Grant', NULL, $params);
}
// first clean up all the money fields
$moneyFields = array(
'amount_total',
'amount_granted',
'amount_requested',
);
foreach ($moneyFields as $field) {
if (isset($params[$field])) {
$params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
}
}
// convert dates to mysql format
$dates = array(
'application_received_date',
'decision_date',
'money_transfer_date',
'grant_due_date',
);
foreach ($dates as $d) {
if (isset($params[$d])) {
$params[$d] = CRM_Utils_Date::processDate($params[$d], NULL, TRUE);
}
}
$grant = new CRM_Grant_DAO_Grant();
$grant->id = CRM_Utils_Array::value('grant_id', $ids);
$grant->copyValues($params);
// set currency for CRM-1496
if (!isset($grant->currency)) {
$config = CRM_Core_Config::singleton();
$grant->currency = $config->defaultCurrency;
}
$result = $grant->save();
$url = CRM_Utils_System::url('civicrm/contact/view/grant',
"action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
);
$grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
if (empty($params['skipRecentView'])) {
if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
$grant->find(TRUE);
}
$title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];
$recentOther = array();
if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::UPDATE)) {
$recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
"action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
);
}
if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::DELETE)) {
$recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
"action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
);
}
// add the recently created Grant
CRM_Utils_Recent::add($title,
$url,
$grant->id,
'Grant',
$grant->contact_id,
NULL,
$recentOther
);
}
if (!empty($ids['grant'])) {
CRM_Utils_Hook::post('edit', 'Grant', $grant->id, $grant);
}
else {
CRM_Utils_Hook::post('create', 'Grant', $grant->id, $grant);
}
return $result;
}
/**
* Create the event.
*
* @param array $params
* Reference array contains the values submitted by the form.
* @param array $ids
* Reference array contains the id.
*
* @return object
*/
public static function create(&$params, &$ids) {
$transaction = new CRM_Core_Transaction();
$grant = self::add($params, $ids);
if (is_a($grant, 'CRM_Core_Error')) {
$transaction->rollback();
return $grant;
}
$session = CRM_Core_Session::singleton();
$id = $session->get('userID');
if (!$id) {
$id = CRM_Utils_Array::value('contact_id', $params);
}
if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
$noteParams = array(
'entity_table' => 'civicrm_grant',
'note' => $params['note'] = $params['note'] ? $params['note'] : "null",
'entity_id' => $grant->id,
'contact_id' => $id,
'modified_date' => date('Ymd'),
);
CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
}
// Log the information on successful add/edit of Grant
$logParams = array(
'entity_table' => 'civicrm_grant',
'entity_id' => $grant->id,
'modified_id' => $id,
'modified_date' => date('Ymd'),
);
CRM_Core_BAO_Log::add($logParams);
// add custom field values
if (!empty($params['custom']) && is_array($params['custom'])) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id);
}
// check and attach and files as needed
CRM_Core_BAO_File::processAttachment($params,
'civicrm_grant',
$grant->id
);
$transaction->commit();
return $grant;
}
/**
* Delete the Contact.
*
* @param int $id
* Contact id.
*
* @return bool
*
*/
public static function deleteContact($id) {
$grant = new CRM_Grant_DAO_Grant();
$grant->contact_id = $id;
$grant->delete();
return FALSE;
}
/**
* Delete the grant.
*
* @param int $id
* Grant id.
*
* @return bool|mixed
*/
public static function del($id) {
CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray);
$grant = new CRM_Grant_DAO_Grant();
$grant->id = $id;
$grant->find();
// delete the recently created Grant
$grantRecent = array(
'id' => $id,
'type' => 'Grant',
);
CRM_Utils_Recent::del($grantRecent);
if ($grant->fetch()) {
$results = $grant->delete();
CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant);
return $results;
}
return FALSE;
}
/**
* Combine all the exportable fields from the lower levels object.
*
* @return array
* array of exportable Fields
*/
public static function &exportableFields() {
if (!self::$_exportableFields) {
if (!self::$_exportableFields) {
self::$_exportableFields = array();
}
$grantFields = array(
'grant_status' => array(
'title' => ts('Grant Status'),
'name' => 'grant_status',
'data_type' => CRM_Utils_Type::T_STRING,
),
'grant_type' => array(
'title' => ts('Grant Type'),
'name' => 'grant_type',
'data_type' => CRM_Utils_Type::T_STRING,
),
'grant_money_transfer_date' => array(
'title' => ts('Grant Money Transfer Date'),
'name' => 'grant_money_transfer_date',
'data_type' => CRM_Utils_Type::T_DATE,
),
'grant_amount_requested' => array(
'title' => ts('Grant Amount Requested'),
'name' => 'grant_amount_requested',
'data_type' => CRM_Utils_Type::T_FLOAT,
),
'grant_application_received_date' => array(
'title' => ts('Grant Application Received Date'),
'name' => 'grant_application_received_date',
'data_type' => CRM_Utils_Type::T_DATE,
),
);
$fields = CRM_Grant_DAO_Grant::export();
$grantNote = array(
'grant_note' => array(
'title' => ts('Grant Note'),
'name' => 'grant_note',
'data_type' => CRM_Utils_Type::T_TEXT,
),
);
$fields = array_merge($fields, $grantFields, $grantNote,
CRM_Core_BAO_CustomField::getFieldsForImport('Grant')
);
self::$_exportableFields = $fields;
}
return self::$_exportableFields;
}
/**
* Get grant record count for a Contact.
*
* @param int $contactID
*
* @return int
* count of grant records
*/
public static function getContactGrantCount($contactID) {
$query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
return CRM_Core_DAO::singleValueQuery($query);
}
}

View file

@ -0,0 +1,353 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| 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-2017
* $Id$
*
*/
class CRM_Grant_BAO_Query extends CRM_Core_BAO_Query {
/**
* @return array
*/
public static function &getFields() {
$fields = array();
$fields = CRM_Grant_BAO_Grant::exportableFields();
return $fields;
}
/**
* Build select for CiviGrant.
*
* @param $query
*
* @return void
*/
public static function select(&$query) {
if (!empty($query->_returnProperties['grant_status_id'])) {
$query->_select['grant_status_id'] = 'grant_status.id as grant_status_id';
$query->_element['grant_status'] = 1;
$query->_tables['grant_status'] = $query->_whereTables['grant_status'] = 1;
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
}
if (!empty($query->_returnProperties['grant_status'])) {
$query->_select['grant_status'] = 'grant_status.label as grant_status';
$query->_element['grant_status'] = 1;
$query->_tables['grant_status'] = $query->_whereTables['grant_status'] = 1;
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
}
if (!empty($query->_returnProperties['grant_type_id'])) {
$query->_select['grant_type_id'] = 'grant_type.id as grant_type_id';
$query->_element['grant_type'] = 1;
$query->_tables['grant_type'] = $query->_whereTables['grant_type'] = 1;
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
}
if (!empty($query->_returnProperties['grant_type'])) {
$query->_select['grant_type'] = 'grant_type.label as grant_type';
$query->_element['grant_type'] = 1;
$query->_tables['grant_type'] = $query->_whereTables['grant_type'] = 1;
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
}
if (!empty($query->_returnProperties['grant_note'])) {
$query->_select['grant_note'] = "civicrm_note.note as grant_note";
$query->_element['grant_note'] = 1;
$query->_tables['grant_note'] = 1;
}
if ($query->_mode & CRM_Contact_BAO_Query::MODE_GRANT) {
$query->_select['grant_amount_requested'] = 'civicrm_grant.amount_requested as grant_amount_requested';
$query->_select['grant_amount_granted'] = 'civicrm_grant.amount_granted as grant_amount_granted';
$query->_select['grant_amount_total'] = 'civicrm_grant.amount_total as grant_amount_total';
$query->_select['grant_application_received_date'] = 'civicrm_grant.application_received_date as grant_application_received_date ';
$query->_select['grant_report_received'] = 'civicrm_grant.grant_report_received as grant_report_received';
$query->_select['grant_money_transfer_date'] = 'civicrm_grant.money_transfer_date as grant_money_transfer_date';
$query->_element['grant_type_id'] = 1;
$query->_element['grant_status_id'] = 1;
$query->_tables['civicrm_grant'] = 1;
$query->_whereTables['civicrm_grant'] = 1;
}
}
/**
* Given a list of conditions in params generate the required.
* where clause
*
* @param $query
*
* @return void
*/
public static function where(&$query) {
foreach ($query->_params as $id => $values) {
if (!is_array($values) || count($values) != 5) {
continue;
}
if (substr($values[0], 0, 6) == 'grant_') {
self::whereClauseSingle($values, $query);
}
}
}
/**
* @param $values
* @param $query
*/
public static function whereClauseSingle(&$values, &$query) {
$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
list($name, $op, $value, $grouping, $wildcard) = $values;
$val = $names = array();
switch ($name) {
case 'grant_money_transfer_date_low':
case 'grant_money_transfer_date_high':
$query->dateQueryBuilder($values, 'civicrm_grant',
'grant_money_transfer_date', 'money_transfer_date',
'Money Transfer Date'
);
return;
case 'grant_money_transfer_date_notset':
$query->_where[$grouping][] = "civicrm_grant.money_transfer_date IS NULL";
$query->_qill[$grouping][] = ts("Grant Money Transfer Date is NULL");
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
return;
case 'grant_application_received_date_low':
case 'grant_application_received_date_high':
$query->dateQueryBuilder($values, 'civicrm_grant',
'grant_application_received_date',
'application_received_date', 'Application Received Date'
);
return;
case 'grant_application_received_notset':
$query->_where[$grouping][] = "civicrm_grant.application_received_date IS NULL";
$query->_qill[$grouping][] = ts("Grant Application Received Date is NULL");
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
return;
case 'grant_due_date_low':
case 'grant_due_date_high':
$query->dateQueryBuilder($values, 'civicrm_grant',
'grant_due_date',
'grant_due_date', 'Grant Due Date'
);
return;
case 'grant_due_date_notset':
$query->_where[$grouping][] = "civicrm_grant.grant_due_date IS NULL";
$query->_qill[$grouping][] = ts("Grant Due Date is NULL");
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
return;
case 'grant_decision_date_low':
case 'grant_decision_date_high':
$query->dateQueryBuilder($values, 'civicrm_grant',
'grant_decision_date',
'decision_date', 'Grant Decision Date'
);
return;
case 'grant_decision_date_notset':
$query->_where[$grouping][] = "civicrm_grant.decision_date IS NULL";
$query->_qill[$grouping][] = ts("Grant Decision Date is NULL");
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
return;
case 'grant_type_id':
case 'grant_type':
case 'grant_status_id':
case 'grant_status':
if (strstr($name, 'type')) {
$name = 'grant_type_id';
$label = 'Grant Type(s)';
}
else {
$name = 'status_id';
$label = 'Grant Status(s)';
}
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_grant.$name", $op, $value, "Integer");
list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Grant_DAO_Grant', $name, $value, $op);
$query->_qill[$grouping][] = ts("%1 %2 %3", array(1 => $label, 2 => $qillop, 3 => $qillVal));
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
return;
case 'grant_report_received':
if ($value == 1) {
$yesNo = 'Yes';
$query->_where[$grouping][] = "civicrm_grant.grant_report_received $op $value";
}
elseif ($value == 0) {
$yesNo = 'No';
$query->_where[$grouping][] = "civicrm_grant.grant_report_received IS NULL";
}
$query->_qill[$grouping][] = "Grant Report Received = $yesNo ";
$query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
return;
case 'grant_amount':
case 'grant_amount_low':
case 'grant_amount_high':
$query->numberRangeBuilder($values,
'civicrm_grant', 'grant_amount', 'amount_total', 'Total Amount'
);
}
}
/**
* @param string $name
* @param $mode
* @param $side
*
* @return null|string
*/
public static function from($name, $mode, $side) {
$from = NULL;
switch ($name) {
case 'civicrm_grant':
$from = " $side JOIN civicrm_grant ON civicrm_grant.contact_id = contact_a.id ";
break;
case 'grant_status':
$from .= " $side JOIN civicrm_option_group option_group_grant_status ON (option_group_grant_status.name = 'grant_status')";
$from .= " $side JOIN civicrm_option_value grant_status ON (civicrm_grant.status_id = grant_status.value AND option_group_grant_status.id = grant_status.option_group_id ) ";
break;
case 'grant_type':
$from .= " $side JOIN civicrm_option_group option_group_grant_type ON (option_group_grant_type.name = 'grant_type')";
if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) {
$from .= " INNER JOIN civicrm_option_value grant_type ON (civicrm_grant.grant_type_id = grant_type.value AND option_group_grant_type.id = grant_type.option_group_id ) ";
}
else {
$from .= " $side JOIN civicrm_option_value grant_type ON (civicrm_grant.grant_type_id = grant_type.value AND option_group_grant_type.id = grant_type.option_group_id ) ";
}
break;
case 'grant_note':
$from .= " $side JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_grant' AND
civicrm_grant.id = civicrm_note.entity_id )";
break;
}
return $from;
}
/**
* @param $mode
* @param bool $includeCustomFields
*
* @return array|null
*/
public static function defaultReturnProperties(
$mode,
$includeCustomFields = TRUE
) {
$properties = NULL;
if ($mode & CRM_Contact_BAO_Query::MODE_GRANT) {
$properties = array(
'contact_type' => 1,
'contact_sub_type' => 1,
'sort_name' => 1,
'grant_id' => 1,
'grant_type' => 1,
'grant_status' => 1,
'grant_amount_requested' => 1,
'grant_application_received_date' => 1,
'grant_report_received' => 1,
'grant_money_transfer_date' => 1,
'grant_note' => 1,
);
}
return $properties;
}
/**
* Add all the elements shared between grant search and advanaced search.
*
*
* @param CRM_Core_Form $form
*
* @return void
*/
public static function buildSearchForm(&$form) {
$grantType = CRM_Core_OptionGroup::values('grant_type');
$form->add('select', 'grant_type_id', ts('Grant Type'), $grantType, FALSE,
array('id' => 'grant_type_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
$grantStatus = CRM_Core_OptionGroup::values('grant_status');
$form->add('select', 'grant_status_id', ts('Grant Status'), $grantStatus, FALSE,
array('id' => 'grant_status_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
$form->addDate('grant_application_received_date_low', ts('App. Received Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_application_received_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
$form->addElement('checkbox', 'grant_application_received_notset', '', NULL);
$form->addDate('grant_money_transfer_date_low', ts('Money Sent Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_money_transfer_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
$form->addElement('checkbox', 'grant_money_transfer_date_notset', '', NULL);
$form->addDate('grant_due_date_low', ts('Report Due Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_due_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
$form->addElement('checkbox', 'grant_due_date_notset', '', NULL);
$form->addDate('grant_decision_date_low', ts('Grant Decision Date - From'), FALSE, array('formatType' => 'searchDate'));
$form->addDate('grant_decision_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
$form->addElement('checkbox', 'grant_decision_date_notset', '', NULL);
$form->addYesNo('grant_report_received', ts('Grant report received?'), TRUE);
$form->add('text', 'grant_amount_low', ts('Minimum Amount'), array('size' => 8, 'maxlength' => 8));
$form->addRule('grant_amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
$form->add('text', 'grant_amount_high', ts('Maximum Amount'), array('size' => 8, 'maxlength' => 8));
$form->addRule('grant_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
self::addCustomFormFields($form, array('Grant'));
$form->assign('validGrant', TRUE);
}
}