<?php

/**
* @package Reports
*/
class Customer_Domains extends Report
{
    protected $featureSet = 'accounts';

    var $m_hasgraph = false;

    function process()
    {
        $numDomainsToShow = 20; // number of domains to show per page

        $numMonthsToShow = 7;   // number of months to show in the graph
                                // if there are no domains in a certain month, that month will not be shown

        $graphWidth = 320;      // width of the graph in pixels
        $graphHeight = 150;     // height of the graph in pixels


        // if graph doesn't show, set this to a directory that PHP has read & write access to
        $cache_dir = "c:\\temp\\";    // example for Windows
        // $cache_dir = "/tmp/";      // example for Linux


        // -------------------------------------------------------------

        // Check if filter is on
        if((isset($_GET['filter'])) && ($_GET['filter'] != "")) {
          $filter = $this->db->escape_string($_GET['filter']);
        }
          
        //ini_set('include_path', dirname(__FILE__) . '/../..');

        $firstDomain="";

        if (!get_magic_quotes_gpc()) {
          if(isset($_GET['firstDomain'])) $firstDomain = addslashes($_GET['firstDomain']); // first domain to show
          if(isset($_GET['go'])) $go = addslashes($_GET['go']); // direction to move through domains
        } else {
          if(isset($_GET['firstDomain'])) $firstDomain = $_GET['firstDomain'];
          if(isset($_GET['go'])) $go = $_GET['go'];
        }
          
          if ($firstDomain == "") {
            $firstDomain = 0;
          } else {
            if ($go == "back") {
              $firstDomain -= $numDomainsToShow;
            } else {
              $firstDomain += $numDomainsToShow;
            }
          }

          // get the total number of domains in the database
          $whereString = '';
          
          if(isset($filter)){
              $whereString = "WHERE Plan = '".$filter."' ";
          }
          
          $sql = "SELECT COUNT(*) "
                ."FROM domains "
                .$whereString;
          
          $result = $this->db->query($sql);
          list($totalDomains) = $result->fetch();
          
          // grab domain, customer id, plan id from database
          $whereString = '';
          
          if(@$_GET["filter"] != ''){
              $filter = $this->db->escape_string($_GET['filter']);
              
              $whereString = " AND Plan = '".$filter."' ";
          }
          
          $sql = "SELECT d.id, d.CustomerID, d.Plan "
                ."FROM domains d, package p, promotion pr WHERE d.Plan = p.id AND p.planid=pr.id AND pr.type=3"
                .$whereString
                ." ORDER BY id ASC "
                ."LIMIT ".$firstDomain.", ".$numDomainsToShow." ";
          
          $result = $this->db->query($sql);
          
          while (list($id, $CustomerID, $Plan) = $result->fetch()) {
              $userPackage = new UserPackage($id);
              $DomainName = $userPackage->getCustomFIeld('Domain Name');
              // we've got what we need so prepare the array for the report
              $domainsGroup[] = "<a href='http://$DomainName/' target='_blank'>$DomainName</a>";
              // these will require more manipulation
              $customerIDGroup[] = array($CustomerID);
              $planIDGroup[] = array($Plan);
          }

          if ($totalDomains > 0) { // make sure there were domains returned from the database

             // determine customer names from customer ids and prepare the array for the report
              if ( isset($customerIDGroup) && count($customerIDGroup) > 0 ) {
                foreach ($customerIDGroup as $customerID) {
                $userid = $customerID[0];
                $tUser = new User($userid);
                 if ($tUser->isOrganization() == true) {
                    $username = $tUser->getOrganization();
                    if (strlen($username) > 20) {
                        $title = "title=\"$username\"";
                        $dusername = mb_substr($username, 0,20) . "...";
                        $username =  "<a href=\"index.php?fuse=clients&view=Admin_Profile_Products&frmClientID=". $userid ."\" $title>" .$dusername."</a>";
                    } else {
                        $title = "";
                        $username =  "<a href=\"index.php?fuse=clients&view=Admin_Profile_Products&frmClientID=". $userid ."\" $title>" .$username."</a>";
                    }
                } else {
                    $username = $tUser->getFullName();
                    $title = "";
                    $username =  "<a href=\"index.php?fuse=clients&view=Admin_Profile_Products&frmClientID=". $userid ."\" $title>" .$username."</a>";
                }   
                $customerGroup[] = $username;
                unset($username);
            }
          }
            if ( isset($planIDGroup) && count($planIDGroup) > 0 ) {
            // determine domain plans from plan ids and prepare the array for the report
            foreach ($planIDGroup as $planID) {
              $sql = "SELECT planname, description, id FROM package WHERE id=?";
              $planResult = $this->db->query($sql, $planID[0]);
              while (list($planname, $description, $id) = $planResult->fetch()) {
                $description = preg_replace("/<.+?>/","",$description);
                $planGroup[] = "<a title='$description' href='" . $_SERVER['PHP_SELF'] . "?fuse=reports&amp;report=Customer+Domains&amp;type=Accounts&amp;view=ViewReport&amp;filter=$id'>$planname</a>";
                //$planGroup[] = "<a href='javascript:void(0);' title='$description'>$planname</a>";
              }
            }
            }

          } else { // there were no domains returned from the database
            $domainsGroup[] = "no domains to show";
            $customerGroup[] = " ";
            $planGroup[] = " ";
          }
          
          // display the graph image
          //echo "<br><br><img src='index.php?fuse=reports&amp;report=".urlencode('Customer Domains')."&amp;type=Accounts&amp;viewimage=1&filter=".@$filter."'><br><br>";

          
          // initialize the report
          $this->SetDescription("");
          
          // prepare a master array with items for the report
          $total = 0;
          if ( isset($domainsGroup) ) {
            $total = count($domainsGroup);
          
              for ($i=0; $i<$total; $i++) {
                $masterGroup[] = array($domainsGroup[$i], $customerGroup[$i], $planGroup[$i]);
              }
              // add all items to the report and set up column names
              $reportLastDomain = $firstDomain + $numDomainsToShow;
              $reportFirstDomain = $firstDomain + 1;
              $this->Add($masterGroup, $this->user->lang('Customer Domains'), array($this->user->lang('Domains').' '.$reportFirstDomain.' '.$this->user->lang('through').' '.$reportLastDomain, $this->user->lang('Customer'), $this->user->lang('Package')));

          }
          // send the report to the browser
          $this->Show();
          // display the "next" and "back" links to move through the list of domains
          // ...and make sure we don't try to look at database rows that don't exist
          if (!isset($filter)) {
              $nextBackLinks  = "";
              if ($firstDomain > 0) {
                $nextBackLinks .= "<a href='" . $_SERVER['PHP_SELF'] . "?fuse=reports&amp;report=Customer+Domains&amp;type=Accounts&amp;view=ViewReport&amp;firstDomain=" . $firstDomain . "&amp;go=back'>".$this->user->lang('Back')."</a>";
              }
              if (($firstDomain > 0) && (($firstDomain + $numDomainsToShow) < $totalDomains)) {
                  $nextBackLinks .= " | ";
              }
          
              if (($firstDomain + $numDomainsToShow) < $totalDomains) {
                $nextBackLinks .= "<a href='" . $_SERVER['PHP_SELF'] . "?fuse=reports&amp;report=Customer+Domains&amp;type=Accounts&amp;view=ViewReport&amp;firstDomain=" . $firstDomain . "&amp;go=next'>".$this->user->lang('Next')."</a>";
              }
          }
          else {
            $nextBackLinks  = "";
          if ($firstDomain > 0) {
            $nextBackLinks .= "<a href='" . $_SERVER['PHP_SELF'] . "?fuse=reports&amp;report=Customer+Domains&amp;type=Accounts&amp;view=ViewReport&amp;firstDomain=" . $firstDomain . "&amp;filter=$filter&go=back'>".$this->user->lang('Back')."</a> | ";
          }
          if (($firstDomain + $numDomainsToShow) < $totalDomains) {
            $nextBackLinks .= "<a href='" . $_SERVER['PHP_SELF'] . "?fuse=reports&amp;report=Customer+Domains&amp;type=Accounts&amp;view=ViewReport&amp;firstDomain=" . $firstDomain . "&amp;filter=$filter&go=next'>".$this->user->lang('Next')."</a> | ";
          }
          $nextBackLinks .= "[ <a title='$description' href='" . $_SERVER['PHP_SELF'] . "?fuse=reports&amp;report=Customer+Domains&amp;type=Accounts&amp;view=ViewReport'>".$this->user->lang('Remove filter')."</a> ]";
          }
          echo $nextBackLinks;

    }
}

?>
