Advertisement
FeRR4L

[PHP] WHMCS Module - Send To MailChimp

Sep 26th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.45 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Addon Module Sample File
  4.  *
  5.  * This example addon module demonstrates all the functions an addon module can contain.
  6.  * Please refer to the full documentation @ http://docs.whmcs.com/Addon_Modules for more details.
  7.  *
  8.  * @package    WHMCS
  9.  * @author     WHMCS Limited <development@whmcs.com>
  10.  * @copyright  Copyright (c) WHMCS Limited 2005-2013
  11.  * @license    http://www.whmcs.com/license/ WHMCS Eula
  12.  * @version    $Id$
  13.  * @link       http://www.whmcs.com/
  14.  */
  15.  
  16. if (!defined("WHMCS"))
  17.     die("This file cannot be accessed directly");
  18.  
  19. function sendtomailchimp_config() {
  20.     $configarray = array(
  21.     "name" => "Send to MailChimp",
  22.     "description" => "This is an open source addon module to send registred customers to a MailChimp List",
  23.     "version" => "1.1",
  24.     "author" => "Elson Sartori",
  25.     "language" => "english",
  26.     "fields" => array(
  27.         // "option1" => array ("FriendlyName" => "Option1", "Type" => "text", "Size" => "25", "Description" => "Textbox", "Default" => "Example", ),
  28.         // "option2" => array ("FriendlyName" => "Option2", "Type" => "password", "Size" => "25", "Description" => "Password", ),
  29.         // "option3" => array ("FriendlyName" => "Option3", "Type" => "yesno", "Size" => "25", "Description" => "Sample Check Box", ),
  30.         // "option4" => array ("FriendlyName" => "Option4", "Type" => "dropdown", "Options" => "1,2,3,4,5", "Description" => "Sample Dropdown", "Default" => "3", ),
  31.         // "option5" => array ("FriendlyName" => "Option5", "Type" => "radio", "Options" => "Demo1,Demo2,Demo3", "Description" => "Radio Options Demo", ),
  32.         // "option6" => array ("FriendlyName" => "Option6", "Type" => "textarea", "Rows" => "3", "Cols" => "50", "Description" => "Description goes here", "Default" => "Test", ),
  33.     "apikey1" => array ("FriendlyName" => "Api Key", "Type" => "text", "Size" => "50", "Description" => "Your API key retrieved from https://admin.mailchimp.com/account/api/", "Default" => "", ),
  34.     "listid1" => array ("FriendlyName" => "List ID", "Type" => "text", "Size" => "10", "Description" => "Your list Unique ID obtained by going to http://admin.mailchimp.com/lists/ and clicking on the Settings link for the list. Unique ID found a the bottom of the page.", "Default" => "", ),
  35.     "dc1" => array ("FriendlyName" => "DataCenter", "Type" => "text", "Size" => "4", "Description" => "Your datacenter. Retrieved from dashboard URL eg. - https://us1.admin.mailchimp.com/", "Default" => "us1", ),
  36.     "groupname1" => array("FriendlyName" => "Group Name", "Type" => "text", "Size" => "20", "Description" => "Name of Group field on MailChimp. You need to create this text field by going to http://admin.mailchimp.com/lists/ and clicking on the Settings link for the list.", "Default" => "group", ),
  37.     // "dbloptin1" => array ("FriendlyName" => "Double OptIn", "Type" => "yesno", "Size" => "5", "Description" => "Set this if you would like the user to confirm their subscription before being added to your list", "Default" => "", ),
  38.     "groupid1" => array ("FriendlyName" => "Group ID", "Type" => "text", "Size" => "10", "Description" => "Clients Group ID to send to MailChimp. 0 = Users without group", "Default" => "0",),
  39.     ));
  40.     return $configarray;
  41. }
  42.  
  43. function sendtomailchimp_output($vars) {
  44.  
  45.     $modulelink = $vars['modulelink'];
  46.     $version = $vars['version'];
  47.     $api_key = $vars['apikey1'];
  48.     $list_id = $vars['listid1'];
  49.     $datacenter = $vars['dc1'];
  50.     // $doubleoptin1 = $vars['dbloptin1'];
  51.     $group1 = $vars['groupid1'];
  52.     //$groupid = strval($vars['groupid1']);
  53.     $groupid = $_POST['groupid1'];
  54.     $emailtype = "html";
  55.     $doubleoptin = "false";
  56.     $output = "php";
  57.     $groupname = $vars['groupname1'];
  58.     $LANG = $vars['_lang'];
  59.    
  60.     $table0 = "tblclientgroups";
  61.     $fields0 = "id,groupname";
  62.     $where0 = array('id' => $groupid);
  63.     $table1 = "tblclients";
  64.     $fields1 = "id,firstname,lastname,email,emailoptout,groupid,status";
  65.     // $where = array('email' => $vars['email']);
  66.     //$where = array('email' => $vars['email']);
  67.     //$where1 = array('id' => $vars['id']);
  68.     $where1 = array('groupid' => $groupid);
  69.  
  70.     echo '<p>'.$LANG['intro'].'</p>
  71.    <p>'.$LANG['description'].'</p>
  72.    <p>'.$LANG['documentation'].'</p>';
  73.     echo '<p></p>';
  74.    
  75.     // GET List
  76.     if(empty($_GET['action'])) {
  77.     //Bloco 1 sem função ainda
  78.     if ($_GET['outracoisa'] == 'successo1') {
  79.         echo '<div class="infobox">'.$LANG['sucesso1'].'</div>';
  80.     } elseif ($_GET['outracoisa'] == 'successo2') {
  81.         echo '<div class="infobox">'.$LANG['successo2'].'</div>';
  82.     } elseif ($_GET['outracoisa'] == 'falha1') {
  83.         echo '<div class="errorbox">'.$LANG['falha1'].'</div>';
  84.     } //Fim Bloco 1
  85.  
  86.     echo "<h2>";
  87.     echo $LANG['title1'];
  88.     echo "</h2>\r\n<h3>";
  89.     echo $LANG['description1'];
  90.     echo "</h3>\r\n";
  91.     echo "<form action=\"".$modulelink."&action=sendnow\" method=\"post\" name=\"send_now_form\" enctype=\"multipart/form-data\" style=\"margin-bottom:60px;\">\r\n";
  92.     echo "<p><b>";
  93. /// echo $LANG['groupid']."</b>";
  94. /// echo "<span style=\"color:red\">".$LANG['groupid']."</span>";
  95.     echo "<span style=\"color:red\">Group ID</span></b></p>";
  96.     echo "</br>\r\n<input type=\"text\" name=\"groupid1\" style=\"width:20px\" /></p>\r\n";
  97.     echo "<p><input type=\"submit\" value=\"";
  98. /// echo $LANG['sendbutton'];
  99.     echo "Enviar";
  100.     echo "\" /></p>\r\n";
  101. //  echo "<p><input type=\"button\" value=\"";
  102. //  echo $LANG['button01'];
  103. //  echo "\" /></p>\r\n";
  104.     echo "</form>\r\n";
  105. //        if (!$varteste) {
  106. //      include(ROOTDIR . "/modules/addons/sendtomailchimp/other/anotherfile.php");
  107. //        }
  108.     }
  109.    
  110.     // SEND
  111.     if($_GET['action'] == 'sendnow') {
  112.     echo '<p>*** Processing ***</p>';
  113.     echo '<p><b>Groupname: </b>';
  114.     var_dump($groupname);
  115.     echo '</p>';
  116.     //$groupid = $_POST['groupid1'];
  117.     echo '<p><b>groupid: </b>';
  118.     var_dump('$groupid');
  119.     echo '</p>';
  120.     //echo '<p><b>Where: </b>'.$where1[id].'</p>';
  121.     echo '<p><b>Where0: </b>';
  122.     var_dump($where0);
  123.     echo '</p>';
  124.     echo '<p><b>Where1: </b>';
  125.     var_dump($where1);
  126.     echo '</p>';
  127.  
  128.         $groups = select_query($table0,$fields0,$where0);
  129.     echo '<p><b>Groups: </b>';
  130.     var_dump($groups);
  131.     echo '</p>';
  132.     if ($groups){
  133.         $row0 = mysql_fetch_assoc($groups);
  134.         echo '<p><b>Row0(dump): </b>';
  135.         var_dump($row0);
  136.         echo '</p>';
  137.         //$usergroup = $row0[groupname];
  138.         $usergroup = str_replace(" ", "%20", $row0['groupname']);
  139.         echo '<p><b>usergroup(dump): </b>';
  140.         var_dump($usergroup);
  141.         echo '</p>';
  142.     }
  143.    
  144.     $clients = select_query($table1,$fields1,$where1);
  145.     //$group1 = $clients
  146.    
  147.     if($clients){
  148.    
  149.         $groupname = strtoupper($groupname);
  150.         echo '<p><b>groupname: </b>';
  151.         var_dump($groupname);
  152.         echo '</p>';
  153.         echo '<p>.</p>';
  154.        
  155.         while($row = mysql_fetch_assoc($clients)){
  156.         ///echo '<p><b>Row: </b>'.$row[id].' | '.$row[firstname].' | '.$row[lastname].' | '.$row[email].' | '.$row[groupid].' | '.$row[emailoptout].' -</p>';
  157.         echo '<p><b>Row: </b>';
  158.         var_dump($row);
  159.         echo '</p>';
  160.         // update_query('tbltickets', array('userid' => $vars['userid'], 'email' => '', 'name' => ''), array('id' => $ticket['id']));
  161.         //$id1 = $row[id];
  162.         $first_name = str_replace(" ", "%20", $row['firstname']);
  163.         $last_name = str_replace(" ", "%20", $row['lastname']);
  164.         $email = $row['email'];
  165.         $doubleoptin = $row['emailoptout'];
  166.         //echo '<p><b>group1 (before): </b>';
  167.         //var_dump($group1);
  168.         //echo '</p>';
  169.         $group1 = $row[groupid];
  170.         //echo '<p><b>group1 (after row): </b>';
  171.         //var_dump($group1);
  172.         //echo '</p>';
  173.         ////$group1 = strval($group1);
  174.         //echo '<p><b>group1 (after str): </b>';
  175.         //var_dump($group1);
  176.         //echo '</p>';
  177.         //$url = "http://$datacenter.api.mailchimp.com/1.3/?output=$output&method=listSubscribe&apikey=$api_key&id=$list_id&email_address=$email&merge_vars[FNAME]=$first_name&merge_vars[LNAME]=$last_name&email_type=$emailtype&double_optin=$doubleoptin&merge_vars[$groupname]=$group1";
  178.         $url = "http://$datacenter.api.mailchimp.com/1.3/?output=$output&method=listSubscribe&apikey=$api_key&id=$list_id&email_address=$email&merge_vars[FNAME]=$first_name&merge_vars[LNAME]=$last_name&email_type=$emailtype&double_optin=$doubleoptin&merge_vars[$groupname]=$usergroup";
  179.         $ch = curl_init();
  180.         curl_setopt($ch, CURLOPT_URL, $url);
  181.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  182.         $retval = curl_exec($ch);
  183.         curl_close($ch);
  184.         echo '<p>..</p>';
  185.        
  186.         logModuleCall('SendToMailChimp','send',$url,$retval,'','');
  187.         //logModuleCall($module,$action,$requeststring,$responsedata,$processeddata,$replacevars);
  188.         //logActivity("Debug: SendToMailChimp - URL: $url - Response: ".htmlentities($retval));
  189.        
  190.         }//while
  191.     }//if
  192.    
  193.     echo '<p>'.$clients[id].'</p>';
  194.     echo '<p>...</p>';
  195.     if(!$clients){
  196.         echo '<p>...</p>
  197.         <p>.:|Attention|:.</p>
  198.         <p>*** Module Error! ***</p>
  199.         <p>...</p>';
  200.     }
  201.    
  202.     echo '<p>***</p>
  203.     <p>Execution Ended!!!</p>
  204.     <p>***</p>';
  205.     }
  206.  
  207.     if ($successful) {
  208.         $result = "Success...";
  209.     } else {
  210.         $result = "Error...";
  211.     }
  212.     return $result;
  213.    
  214. }
  215.  
  216. function sendtomailchimp_sidebar($vars) {
  217.  
  218.     $modulelink = $vars['modulelink'];
  219.     $version = $vars['version'];
  220.     $LANG = $vars['_lang'];
  221.  
  222.     $sidebar = '<span class="header"><img src="images/icons/addonmodules.png" class="absmiddle" width="16" height="16" />'.$LANG['addonname'].'</span>
  223.    <ul class="menu">
  224.        <li><a href="'.$modulelink.'">'.$LANG['sidebarmain'].'</a></li>
  225.        <li><a>'.$LANG['sidebarversion'].' '.$version.'</a></li>
  226.    </ul>';
  227.     return $sidebar;
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement