Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-25-10-57-01
[civicrm-core.git] / bin / deprecated / UpdateMembershipReminderDate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28
29 /*
30 * This file updates the Reminder dates of all valid membership records.
31 *
32 */
33
34 /**
35 * Class CRM_UpdateMembershipReminderDate
36 */
37 class CRM_UpdateMembershipReminderDate {
38 /**
39 *
40 */
41 function __construct() {
42 // you can run this program either from an apache command, or from the cli
43 if (php_sapi_name() == "cli") {
44 require_once ("cli.php");
45 $cli = new civicrm_cli();
46 //if it doesn't die, it's authenticated
47 }
48 else {
49 //from the webserver
50 $this->initialize();
51
52 $config = CRM_Core_Config::singleton();
53
54 // this does not return on failure
55 CRM_Utils_System::authenticateScript(TRUE);
56
57 //log the execution time of script
58 CRM_Core_Error::debug_log_message('UpdateMembershipReminderDate.php');
59 }
60 }
61
62 function initialize() {
63 require_once '../civicrm.config.php';
64 require_once 'CRM/Core/Config.php';
65
66 $config = CRM_Core_Config::singleton();
67 }
68
69 public function updateMembershipReminderDate() {
70 require_once 'CRM/Member/PseudoConstant.php';
71
72 //get all active statuses of membership.
73 $allStatuses = CRM_Member_PseudoConstant::membershipStatus();
74
75 //set membership reminder date if membership
76 //record has one of the following status.
77 $validStatus = array('New', 'Current', 'Grace');
78
79 $statusIds = array();
80 foreach ($validStatus as $status) {
81 $statusId = array_search($status, $allStatuses);
82 if ($statusId) {
83 $statusIds[$statusId] = $statusId;
84 }
85 }
86
87 //we don't have valid status to check,
88 //therefore no need to proceed further.
89 if (empty($statusIds)) {
90 return;
91 }
92
93 //set reminder date for all memberships,
94 //in case reminder date is missing and
95 //membership type has reminder day set.
96
97 $query = '
98 UPDATE civicrm_membership membership
99 INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id )
100 INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_type_id )
101 SET membership.reminder_date = DATE_SUB( membership.end_date, INTERVAL type.renewal_reminder_day + 1 DAY )
102 WHERE membership.reminder_date IS NULL
103 AND contact.is_deleted = 0
104 AND ( contact.is_deceased IS NULL OR contact.is_deceased = 0 )
105 AND type.renewal_reminder_day IS NOT NULL
106 AND membership.status_id IN ( ' . implode(' , ', $statusIds) . ' )';
107
108 CRM_Core_DAO::executeQuery($query);
109 }
110 }
111
112 $reminderDate = new CRM_UpdateMembershipReminderDate();
113
114 echo "\n Updating... ";
115 $reminderDate->updateMembershipReminderDate();
116 echo "\n\n Membership(s) reminder date updated. (Done) \n";
117