add warning on standalone add membership form
authorAndrew Hunt <andrew@aghstrategies.com>
Mon, 29 Apr 2013 06:25:33 +0000 (02:25 -0400)
committerAndrew Hunt <andrew@aghstrategies.com>
Mon, 29 Apr 2013 06:25:33 +0000 (02:25 -0400)
CRM/Member/Form/Membership.php
templates/CRM/Member/Form/MembershipStandalone.js [new file with mode: 0644]

index 7e3ba888f91e14364a9111ea955b176fcd4db6be..a66dcdfb85c5902d04979f0aa3f5defa595a1818 100644 (file)
@@ -235,6 +235,26 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
           $resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/Membership.js');
         }
       }
+      else { 
+        $resources = CRM_Core_Resources::singleton();
+        $resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/MembershipStandalone.js');
+        $statuses = array();
+        $membershipStatus = new CRM_Member_DAO_MembershipStatus();
+        $membershipStatus->is_current_member = 1;
+        $membershipStatus->find();
+        $membershipStatus->selectAdd();
+        $membershipStatus->selectAdd('id');
+        while ($membershipStatus->fetch()) {
+          $statuses[$membershipStatus->id] = $membershipStatus->label;
+        }
+        $membershipStatus->free();
+        $passthru = array(
+          'typeorgs' => CRM_Member_BAO_MembershipType::getMembershipTypeOrganization(),
+          'memtypes' => CRM_Member_BAO_MembershipType::getMembershipTypes(false),
+          'statuses' => $statuses,
+        );
+        $resources->addSetting(array('existingMems' => $passthru));
+      }
     }
 
     // when custom data is included in this page
diff --git a/templates/CRM/Member/Form/MembershipStandalone.js b/templates/CRM/Member/Form/MembershipStandalone.js
new file mode 100644 (file)
index 0000000..163ffb5
--- /dev/null
@@ -0,0 +1,47 @@
+cj(function($) {
+  memberResults = new Array;
+  $("input[name='contact[1]']").result( function() {
+    var contact_id = cj("input[name='contact_select_id[1]']").val();
+    CRM.api('Membership', 'get', {'sequential': 1, 'contact_id': contact_id},
+      {success: function(data) {
+        if (data['values']) {
+          memberResults = data['values'];
+          checkExistingMemOrg();
+        }
+      }});
+      });
+      
+  checkExistingMemOrg();
+
+  $("select[name='membership_type_id[0]']").change(function() { checkExistingMemOrg(); });
+
+  function checkExistingMemOrg () {
+    if (memberResults) {
+      var selectedorg = $("select[name='membership_type_id[0]']").val();
+      $.each(memberResults, function() {
+        if (this['membership_type_id'] in CRM.existingMems.typeorgs) {
+          if (CRM.existingMems.typeorgs[this['membership_type_id']] == selectedorg) {
+            if(this['status_id'] in CRM.existingMems.statuses) {
+              var membership_status = CRM.existingMems.statuses[this['status_id']];
+              var andEndDate = '';
+              var endDate = this.membership_end_date;
+              if (endDate) {
+                andEndDate = ts(" and end date of %1", {1:endDate});
+              }
+              
+              var renewUrl = CRM.url('civicrm/contact/view/membership',
+                "reset=1&action=renew&cid="+this.contact_id+"&id="+this['id']+"&context=membership&selectedChild=member"
+              );
+
+              var membershipTab = CRM.url('civicrm/contact/view',
+                "reset=1&force=1&cid="+this.contact_id+"&selectedChild=member"
+              );
+              
+              CRM.alert(ts('This contact has an existing %1 membership record with %2 status%3.<ul><li><a href="%4">Renew the existing membership instead</a></li><li><a href="%5">View all existing and / or expired memberships for this contact</a></li></ul>', {1:CRM.existingMems.memtypes[this.membership_type_id], 2:membership_status, 3:andEndDate, 4:renewUrl, 5:membershipTab}), ts('Duplicate Membership?'), 'alert');
+            }
+          }
+        }
+      });
+    }
+  }
+});