@@ -9,12 +9,13 @@ function __construct()
99
1010 function index ()
1111 {
12+ $ data ['mailchimp ' ]=($ this ->config ->item ('mc_api_key ' ) != null );
1213 $ config ['base_url ' ] = site_url ('?c=customers&m=index ' );
1314 $ config ['total_rows ' ] = $ this ->Customer ->count_all ();
1415 $ config ['per_page ' ] = '20 ' ;
1516 $ this ->pagination ->initialize ($ config );
16-
1717 $ data ['controller_name ' ]=strtolower (get_class ());
18+
1819 $ data ['form_width ' ]=$ this ->get_form_width ();
1920 $ data ['manage_table ' ]=get_people_manage_table ($ this ->Customer ->get_all ($ config ['per_page ' ], $ this ->input ->get ('per_page ' )),$ this );
2021 $ this ->load ->view ('people/manage ' ,$ data );
@@ -29,7 +30,7 @@ function search()
2930 $ data_rows =get_people_manage_table_data_rows ($ this ->Customer ->search ($ search ),$ this );
3031 echo $ data_rows ;
3132 }
32-
33+
3334 /*
3435 Gives search suggestions based on what is being searched for
3536 */
@@ -44,7 +45,19 @@ function suggest()
4445 */
4546 function view ($ customer_id =-1 )
4647 {
47- $ data ['person_info ' ]=$ this ->Customer ->get_info ($ customer_id );
48+ $ email =preg_replace ('/.*email:([^\/]*)\/.*/ ' , '$1 ' , uri_string ());
49+ if ($ email != uri_string ()) {
50+ $ data ['person_info ' ]=$ customer_id == -1 ? $ this ->Customer ->get_by_email ($ email ) : $ this ->Customer ->get_info ($ customer_id );
51+ if (!$ data ['person_info ' ] && $ email ) {
52+ if ($ key = $ this ->config ->item ('mc_api_key ' )) {
53+ $ this ->load ->library ('MailChimp ' , array ($ key ) , 'MailChimp ' );
54+ $ data ['person_info ' ] = $ this ->MailChimp ->getPersonDataByEmail ($ email );
55+ }
56+ }
57+ } else {
58+ $ data ['person_info ' ] = $ this ->Customer ->get_info ($ customer_id );
59+ }
60+
4861 $ this ->load ->view ("customers/form " ,$ data );
4962 }
5063
@@ -75,13 +88,40 @@ function save($customer_id=-1)
7588 //New customer
7689 if ($ customer_id ==-1 )
7790 {
78- echo json_encode (array ('success ' =>true ,'message ' =>$ this ->lang ->line ('customers_successful_adding ' ).' ' .
79- $ person_data ['first_name ' ].' ' .$ person_data ['last_name ' ],'person_id ' =>$ customer_data ['person_id ' ]));
91+ $ subscriptionInfo = '' ;
92+ if ($ key = $ this ->config ->item ('mc_api_key ' )) {
93+ $ this ->load ->library ('MailChimp ' , array ($ key ) , 'MailChimp ' );
94+
95+ if ($ this ->MailChimp ->handleSubscriptionForPerson ($ customer_data ['person_id ' ])) {
96+ $ subscriptionInfo = $ this ->lang ->line ('common_successful_subscription ' );
97+ } else {
98+ $ subscriptionInfo = $ this ->lang ->line ('common_unsuccessful_subscription ' ).': ' .$ this ->MailChimp ->errorMessage ;
99+ }
100+ }
101+
102+ echo json_encode (array ('success ' =>true ,
103+ 'message ' =>$ this ->lang ->line ('customers_successful_adding ' ).' ' .
104+ $ person_data ['first_name ' ].' ' .
105+ $ person_data ['last_name ' ].' ' .
106+ $ subscriptionInfo ,
107+ 'person_id ' =>$ customer_data ['person_id ' ]));
80108 }
81109 else //previous customer
82110 {
111+ $ subscriptionInfo = '' ;
112+ if ($ key = $ this ->config ->item ('mc_api_key ' )) {
113+ $ this ->load ->library ('MailChimp ' , array ($ key ) , 'MailChimp ' );
114+
115+ if ($ this ->MailChimp ->handleSubscriptionForPerson ($ customer_id , true )) {
116+ $ subscriptionInfo = $ this ->lang ->line ('common_successful_subscription ' );
117+ } else {
118+ $ subscriptionInfo = $ this ->lang ->line ('common_unsuccessful_subscription ' ).': ' .$ this ->MailChimp ->errorMessage ;
119+ }
120+ }
121+
83122 echo json_encode (array ('success ' =>true ,'message ' =>$ this ->lang ->line ('customers_successful_updating ' ).' ' .
84- $ person_data ['first_name ' ].' ' .$ person_data ['last_name ' ],'person_id ' =>$ customer_id ));
123+ $ person_data ['first_name ' ].' ' .$ person_data ['last_name ' ].'. ' .
124+ $ subscriptionInfo .' ' ,'person_id ' =>$ customer_id ));
85125 }
86126 }
87127 else //failure
0 commit comments