| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | $Rev: 267 $ | $LastChangedBy: brieb $ |
|---|
| 4 | $LastChangedDate: 2007-08-14 13:08:27 -0600 (Tue, 14 Aug 2007) $ |
|---|
| 5 | +-------------------------------------------------------------------------+ |
|---|
| 6 | | Copyright (c) 2004 - 2010, Kreotek LLC | |
|---|
| 7 | | All rights reserved. | |
|---|
| 8 | +-------------------------------------------------------------------------+ |
|---|
| 9 | | | |
|---|
| 10 | | Redistribution and use in source and binary forms, with or without | |
|---|
| 11 | | modification, are permitted provided that the following conditions are | |
|---|
| 12 | | met: | |
|---|
| 13 | | | |
|---|
| 14 | | - Redistributions of source code must retain the above copyright | |
|---|
| 15 | | notice, this list of conditions and the following disclaimer. | |
|---|
| 16 | | | |
|---|
| 17 | | - Redistributions in binary form must reproduce the above copyright | |
|---|
| 18 | | notice, this list of conditions and the following disclaimer in the | |
|---|
| 19 | | documentation and/or other materials provided with the distribution. | |
|---|
| 20 | | | |
|---|
| 21 | | - Neither the name of Kreotek LLC nor the names of its contributore may | |
|---|
| 22 | | be used to endorse or promote products derived from this software | |
|---|
| 23 | | without specific prior written permission. | |
|---|
| 24 | | | |
|---|
| 25 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|---|
| 26 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|---|
| 27 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
|---|
| 28 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
|---|
| 29 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
|---|
| 30 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
|---|
| 31 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
|---|
| 32 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
|---|
| 33 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
|---|
| 34 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
|---|
| 35 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|---|
| 36 | | | |
|---|
| 37 | +-------------------------------------------------------------------------+ |
|---|
| 38 | */ |
|---|
| 39 | class mailchimpUpdate{ |
|---|
| 40 | |
|---|
| 41 | function updateSettings($variables){ |
|---|
| 42 | |
|---|
| 43 | if(!isset($variables["mailchimp_secure"])) |
|---|
| 44 | $variables["mailchimp_secure"] = 0; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Check for a valid api key. |
|---|
| 48 | */ |
|---|
| 49 | if($variables["apikey_changed"] == "1" && $variables["mailchimp_apikey"] != ""){ |
|---|
| 50 | include_once("include/MCAPI.class.php"); |
|---|
| 51 | |
|---|
| 52 | $api = new MCAPI($variables["mailchimp_apikey"]); |
|---|
| 53 | $api->ping(); |
|---|
| 54 | if ($api->errorCode){ |
|---|
| 55 | unset($variables["mailchimp_apikey"]); |
|---|
| 56 | $this->updateErrorMessage = "Unable to change the MailChimp apikey: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 57 | return $variables; |
|---|
| 58 | }//end if |
|---|
| 59 | |
|---|
| 60 | }//end if |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Check for valid list id |
|---|
| 65 | */ |
|---|
| 66 | if($variables["apilist_changed"] == "1" && $variables["mailchimp_list_id"] != ""){ |
|---|
| 67 | include_once("include/MCAPI.class.php"); |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Check to see if api is already defined (from a possible api key check) |
|---|
| 71 | * If not, define it and check the key/connection |
|---|
| 72 | */ |
|---|
| 73 | if(!isset($api)){ |
|---|
| 74 | $api = new MCAPI($variables["mailchimp_apikey"]); |
|---|
| 75 | $api->ping(); |
|---|
| 76 | if ($api->errorCode){ |
|---|
| 77 | unset($variables["mailchimp_list_id"]); |
|---|
| 78 | $this->updateErrorMessage = "Unable to change the MailChimp list id: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 79 | return $variables; |
|---|
| 80 | }//end if |
|---|
| 81 | }//end if |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Look up the lists |
|---|
| 85 | */ |
|---|
| 86 | $lists = $api->lists(); |
|---|
| 87 | if ($api->errorCode){ |
|---|
| 88 | unset($variables["mailchimp_list_id"]); |
|---|
| 89 | $this->updateErrorMessage = "Unable to change the MailChimp list id: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 90 | return $variables; |
|---|
| 91 | }else{ |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Check to see if list id is valid |
|---|
| 95 | */ |
|---|
| 96 | $validId = false; |
|---|
| 97 | foreach($lists as $list) |
|---|
| 98 | if($list["id"] == $variables["mailchimp_list_id"]){ |
|---|
| 99 | $validId = true; |
|---|
| 100 | break; |
|---|
| 101 | }//endif |
|---|
| 102 | |
|---|
| 103 | if(!$validId){ |
|---|
| 104 | unset($variables["mailchimp_list_id"]); |
|---|
| 105 | $this->updateErrorMessage = "Unable to change the MailChimp list id: the id does not match a valid id on the account."; |
|---|
| 106 | return $variables; |
|---|
| 107 | }else{ |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Check to see if the list has a uuid. |
|---|
| 111 | */ |
|---|
| 112 | $hasUuid = false; |
|---|
| 113 | $hasCompany = false; |
|---|
| 114 | $hasType = false; |
|---|
| 115 | $mergeVars = $api->listMergeVars($variables["mailchimp_list_id"]); |
|---|
| 116 | if($api->errorCode){ |
|---|
| 117 | unset($variables["mailchimp_list_id"]); |
|---|
| 118 | $this->updateErrorMessage = "Unable to change the MailChimp list id: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 119 | return $variables; |
|---|
| 120 | }//end if |
|---|
| 121 | |
|---|
| 122 | $req = array(); |
|---|
| 123 | foreach($mergeVars as $mergeVar){ |
|---|
| 124 | |
|---|
| 125 | switch($mergeVar["tag"]){ |
|---|
| 126 | |
|---|
| 127 | case "UUID": |
|---|
| 128 | $hasUuid = true; |
|---|
| 129 | break; |
|---|
| 130 | |
|---|
| 131 | case "COMPANY": |
|---|
| 132 | $hasCompany = true; |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | case "TYPE": |
|---|
| 136 | $hasType = true; |
|---|
| 137 | break; |
|---|
| 138 | |
|---|
| 139 | }//end switch |
|---|
| 140 | |
|---|
| 141 | }//end foreach |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * If it doesn't have a uuid field, create it. |
|---|
| 145 | */ |
|---|
| 146 | if(!$hasUuid){ |
|---|
| 147 | $req = array( |
|---|
| 148 | "req"=>true, |
|---|
| 149 | "public"=>false, |
|---|
| 150 | "field_type"=>"text" |
|---|
| 151 | ); |
|---|
| 152 | $api->listMergeVarAdd($variables["mailchimp_list_id"], "UUID", "phpBMS unique user id", $req); |
|---|
| 153 | if($api->errorCode){ |
|---|
| 154 | unset($variables["mailchimp_list_id"]); |
|---|
| 155 | $this->updateErrorMessage = "Unable to change the MailChimp list id: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 156 | return $variables; |
|---|
| 157 | }//end if |
|---|
| 158 | |
|---|
| 159 | }//end if |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * If it doesn't have a company field, create it. |
|---|
| 163 | */ |
|---|
| 164 | if(!$hasCompany){ |
|---|
| 165 | $req = array( |
|---|
| 166 | "req"=>false, |
|---|
| 167 | "public"=>true, |
|---|
| 168 | "field_type"=>"text" |
|---|
| 169 | ); |
|---|
| 170 | $api->listMergeVarAdd($variables["mailchimp_list_id"], "COMPANY", "Company", $req); |
|---|
| 171 | if($api->errorCode){ |
|---|
| 172 | unset($variables["mailchimp_list_id"]); |
|---|
| 173 | $this->updateErrorMessage = "Unable to change the MailChimp list id: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 174 | return $variables; |
|---|
| 175 | }//end if |
|---|
| 176 | |
|---|
| 177 | }//end if |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * If it doesn't have a type field, create it. |
|---|
| 181 | */ |
|---|
| 182 | if(!$hasType){ |
|---|
| 183 | $req = array( |
|---|
| 184 | "req"=>false, |
|---|
| 185 | "public"=>true, |
|---|
| 186 | "field_type"=>"text" |
|---|
| 187 | ); |
|---|
| 188 | $api->listMergeVarAdd($variables["mailchimp_list_id"], "TYPE", "Type", $req); |
|---|
| 189 | if($api->errorCode){ |
|---|
| 190 | unset($variables["mailchimp_list_id"]); |
|---|
| 191 | $this->updateErrorMessage = "Unable to change the MailChimp list id: ".$api->errorMessage." (".$api->errorCode.")"; |
|---|
| 192 | return $variables; |
|---|
| 193 | }//end if |
|---|
| 194 | |
|---|
| 195 | }//end if |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * If the date list id has changed, the last sync date must be reset: |
|---|
| 200 | */ |
|---|
| 201 | $variables["mailchimp_last_sync_date"] = ""; |
|---|
| 202 | |
|---|
| 203 | }//end if |
|---|
| 204 | |
|---|
| 205 | }//end if |
|---|
| 206 | |
|---|
| 207 | }//end if |
|---|
| 208 | |
|---|
| 209 | return $variables; |
|---|
| 210 | |
|---|
| 211 | }//end function |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | }//end class |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | class mailchimpDisplay{ |
|---|
| 218 | |
|---|
| 219 | function getFields($therecord){ |
|---|
| 220 | |
|---|
| 221 | global $db; |
|---|
| 222 | |
|---|
| 223 | $theinput = new inputField("mailchimp_apikey",$therecord["mailchimp_apikey"],"mailchimp apikey", false, NULL, 48); |
|---|
| 224 | $fields[] = $theinput; |
|---|
| 225 | |
|---|
| 226 | $theinput = new inputCheckbox("mailchimp_secure", $therecord["mailchimp_secure"], "use ssl connection"); |
|---|
| 227 | $fields[] = $theinput; |
|---|
| 228 | |
|---|
| 229 | $theinput = new inputField("mailchimp_list_id", $therecord["mailchimp_list_id"], "list id"); |
|---|
| 230 | $fields[] = $theinput; |
|---|
| 231 | |
|---|
| 232 | $theinput = new inputField("mailchimp_last_sync_date", $therecord["mailchimp_last_sync_date"], "last sync date"); |
|---|
| 233 | $theinput->setAttribute("class", "uneditable"); |
|---|
| 234 | $theinput->setAttribute("readonly", "readonly"); |
|---|
| 235 | $fields[] = $theinput; |
|---|
| 236 | |
|---|
| 237 | return $fields; |
|---|
| 238 | }//end method --getFields-- |
|---|
| 239 | |
|---|
| 240 | function display($theform,$therecord){ |
|---|
| 241 | ?> |
|---|
| 242 | <div class="moduleTab" title="MailChimp"> |
|---|
| 243 | <fieldset> |
|---|
| 244 | <legend>Main</legend> |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | <input type="hidden" id="apikey_changed" name="apikey_changed" value="0" /> |
|---|
| 248 | |
|---|
| 249 | <p> |
|---|
| 250 | <span class="notes"> |
|---|
| 251 | To use this module, you need to create an account with MailChimp |
|---|
| 252 | (<a href="http://mailchimp.com">http://mailchimp.com</a>). |
|---|
| 253 | </span> |
|---|
| 254 | </p> |
|---|
| 255 | |
|---|
| 256 | <p> |
|---|
| 257 | <?php echo $theform->showField("mailchimp_apikey");?> |
|---|
| 258 | <br/> |
|---|
| 259 | <span class="notes"> |
|---|
| 260 | Your MailChimp api key may found under the "API Keys & Info" section |
|---|
| 261 | in your "Account" page (<a href="http://admin.mailchimp.com/account/api" >http://admin.mailchimp.com/account/api</a>). |
|---|
| 262 | </span> |
|---|
| 263 | </p> |
|---|
| 264 | <input type="hidden" id="listid_changed" name="apilist_changed" value="0" /> |
|---|
| 265 | <p><?php echo $theform->showField("mailchimp_list_id");?> |
|---|
| 266 | <br/> |
|---|
| 267 | <span class="notes"> |
|---|
| 268 | The list id for the list can be found under the list's settings near |
|---|
| 269 | the bottom of the page. It should say "unique id for list |*list name*|" |
|---|
| 270 | where |*list name*| is the name of the list. |
|---|
| 271 | <br/> |
|---|
| 272 | When selecting a list to use, be aware that the sync process will |
|---|
| 273 | remove records (on the MailChimp side) that do not exist in the client |
|---|
| 274 | table. |
|---|
| 275 | </span> |
|---|
| 276 | </p> |
|---|
| 277 | <p><?php echo $theform->showField("mailchimp_secure");?></p> |
|---|
| 278 | <p><?php echo $theform->showField("mailchimp_last_sync_date");?></p> |
|---|
| 279 | |
|---|
| 280 | </fieldset> |
|---|
| 281 | <p class="updateButtonP"><button type="button" class="Buttons UpdateButtons">save</button></p> |
|---|
| 282 | </div> |
|---|
| 283 | |
|---|
| 284 | <?php |
|---|
| 285 | }//end method |
|---|
| 286 | }//end class |
|---|
| 287 | ?> |
|---|