When on a member profile page pull banners for members county
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 17 Jan 2014 17:47:31 +0000 (17:47 +0000)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 17 Jan 2014 17:47:31 +0000 (17:47 +0000)
If member_id is numeric then pick county banners to appear on profile
pages.

Toolkit/Banners/BannersIterator.php

index 9c511ff..cd136ed 100644 (file)
@@ -43,14 +43,14 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * @var    array
      * @access private
      */
-       private $_banners;
+    private $_banners;
 
     /**
      * type of banners in group
      * @var    array
      * @access private
      */
-       private $_type;
+    private $_type;
 
     //  }}}
     //  {{{ __construct()
@@ -60,18 +60,18 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * construct
      *
      * @param string $type banner type this iterator is dealing with
-        *
+     *
      * @return void
      * @access private
      */
-       private function __construct($type)
-       {
-               $this->_type = strtolower($type);
-               $this->_banners = array();
-       }
+    private function __construct($type)
+    {
+        $this->_type = strtolower($type);
+        $this->_banners = array();
+    }
 
     //  }}}
-    // {{{     getIterator()
+    //    {{{    getIterator()
 
 
     /**
@@ -80,12 +80,12 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * @return ArrayIterator array iterator
      * @access public
      */
-       public function getIterator()
-       {
-               return new ArrayIterator($this->_banners);
-       }
+    public function getIterator()
+    {
+        return new ArrayIterator($this->_banners);
+    }
 
-       //      }}}
+    //    }}}
 
     //  {{{ addBanner()
 
@@ -94,18 +94,18 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * Adds banner to the group
      *
      * @param Toolkit_Banners_Banner $banner banner to add
-        *
+     *
      * @return void
      * @access public
      * @throws InvalidArgumentException if $banner is not a Banner object
      */
-       public function addBanner($banner)
-       {
-               if (!($banner instanceof Toolkit_Banners_Banner)) {
-                       throw new InvalidArgumentException('Invalid Argument');
-               }
-               $this->_banners[] = $banner;
-       }
+    public function addBanner($banner)
+    {
+        if (!($banner instanceof Toolkit_Banners_Banner)) {
+            throw new InvalidArgumentException('Invalid Argument');
+        }
+        $this->_banners[] = $banner;
+    }
 
     //  }}}
 
@@ -116,15 +116,15 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * Create a new Iterator
      *
      * @param string $type types of banners we are going to be dealing with
-        *
+     *
      * @return Toolkit_Banners_BannersIterator iterator object
      * @access public
      * @static
      */
-       public static function create($type)
-       {
-               return new self($type);
-       }
+    public static function create($type)
+    {
+        return new self($type);
+    }
 
     //  }}}
 
@@ -135,30 +135,30 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * Sets all the banners that are active and of the proper type
      *
      * @param PDO $pdo Database handler
-        *
+     *
      * @return false on error
      * @access public
      */
-       public function fetchAll(PDO $pdo, Config_Container $config)
-       {
-               //      Reset collection of banners
-               $this->_banners = array();
-
-               try {
-                       $sql = "
-                               SELECT id
-                                 FROM banners
-                                WHERE position = '{$this->_type}'
-                                  AND active = CAST(1 AS BOOLEAN)";
-
-                       foreach ($pdo->query($sql) as $row) {
-                               $this->_banners[]
-                                       = Toolkit_Banners_Banner::fetch($pdo, $config, $row['id']);
-                       }
-               } catch (PDOException $e) {
-                       return Toolkit_Common::handleError($e);
-               }
-       }
+    public function fetchAll(PDO $pdo, Config_Container $config)
+    {
+        //    Reset collection of banners
+        $this->_banners = array();
+
+        try {
+            $sql = "
+                SELECT id
+                  FROM banners
+                 WHERE position = '{$this->_type}'
+                   AND active = CAST(1 AS BOOLEAN)";
+
+            foreach ($pdo->query($sql) as $row) {
+                $this->_banners[]
+                    = Toolkit_Banners_Banner::fetch($pdo, $config, $row['id']);
+            }
+        } catch (PDOException $e) {
+            return Toolkit_Common::handleError($e);
+        }
+    }
 
     //  }}}
     //  {{{ fetchAllAssignedToMemberCategory()
@@ -171,78 +171,78 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      *
      * @param PDO     $pdo   Database handler
      * @param integer $catid Member category
-        *
+     *
      * @return false on error
      * @access public
      * @throws InvalidArgumentException if $catid is not an integer
      */
-       public function fetchAllAssignedToMemberCategory(PDO $pdo, $catid)
-       {
-               if (!ctype_digit((string)$catid)) {
-                       throw new InvalidArgumentException(
-                               '$catid must be an integer.'
-                       );
-               }
-
-               //      Reset collection of banners
-               $this->_banners = array();
-
-               $categories = Toolkit_Common::getHierarchicalTreeStructure(
-                       $pdo,
-                       'category',
-                       'category_id',
-                       'parent_id',
-                       'pos',
-                       $catid
-               );
-
-               $categories = implode(', ', array_keys($categories));
-
-               $bannersAssignedToMemberCategory = "
-                       SELECT bid
-                         FROM banners2membercategories
-                        WHERE catid IN ($categories)
-               ";
-               $bannersThatAreActive = "
-                       SELECT id
-                         FROM banners
-                        WHERE campaign IN(
-                               SELECT id
-                                 FROM banner_campaigns c
-                                WHERE sdate <= CURRENT_DATE
-                                  AND (edate >= CURRENT_DATE OR edate IS NULL)
-                                  AND ((paid_impressions = 0 OR paid_impressions IS NULL) OR
-                                           (paid_impressions < (
-                                                       SELECT COUNT(*)
-                                                         FROM banner_campaign_impressions
-                                                        WHERE campaign = c.id)
-                                               )
-                               )
-                       )
-               ";
-
-               try {
-                       $sql = "
-                               SELECT id
-                                 FROM banners
-                                WHERE position = '{$this->_type}'
-                                  AND active = CAST(1 AS BOOLEAN)
-                        INTERSECT
-                               $bannersAssignedToMemberCategory
-                        INTERSECT
-                               $bannersThatAreActive
-                               ";
-
-                       $stmt = $pdo->prepare($sql);
-                       $stmt->execute();
-                       while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
-                               $this->_banners[]
-                                       = Toolkit_Banners_Banner::fetch($pdo, $row['id']);
-                       }
-               } catch (PDOException $e) {
-                       return Toolkit_Common::handleError($e);
-               }
-       }
+    public function fetchAllAssignedToMemberCategory(PDO $pdo, $catid)
+    {
+        if (!ctype_digit((string)$catid)) {
+            throw new InvalidArgumentException(
+                '$catid must be an integer.'
+            );
+        }
+
+        //    Reset collection of banners
+        $this->_banners = array();
+
+        $categories = Toolkit_Common::getHierarchicalTreeStructure(
+            $pdo,
+            'category',
+            'category_id',
+            'parent_id',
+            'pos',
+            $catid
+        );
+
+        $categories = implode(', ', array_keys($categories));
+
+        $bannersAssignedToMemberCategory = "
+            SELECT bid
+              FROM banners2membercategories
+             WHERE catid IN ($categories)
+        ";
+        $bannersThatAreActive = "
+            SELECT id
+              FROM banners
+             WHERE campaign IN(
+                SELECT id
+                  FROM banner_campaigns c
+                 WHERE sdate <= CURRENT_DATE
+                   AND (edate >= CURRENT_DATE OR edate IS NULL)
+                   AND ((paid_impressions = 0 OR paid_impressions IS NULL) OR
+                        (paid_impressions < (
+                            SELECT COUNT(*)
+                              FROM banner_campaign_impressions
+                             WHERE campaign = c.id)
+                        )
+                )
+            )
+        ";
+
+        try {
+            $sql = "
+                SELECT id
+                  FROM banners
+                 WHERE position = '{$this->_type}'
+                   AND active = CAST(1 AS BOOLEAN)
+             INTERSECT
+                $bannersAssignedToMemberCategory
+             INTERSECT
+                $bannersThatAreActive
+                ";
+
+            $stmt = $pdo->prepare($sql);
+            $stmt->execute();
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $this->_banners[]
+                    = Toolkit_Banners_Banner::fetch($pdo, $row['id']);
+            }
+        } catch (PDOException $e) {
+            return Toolkit_Common::handleError($e);
+        }
+    }
 
     //  }}}
     //  {{{ fetchAllAssignedToPage()
@@ -253,47 +253,47 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      *
      * @param PDO     $pdo   Database handler
      * @param integer $catid Toolbox category id
-        *
+     *
      * @return false on error
      * @access public
      * @throws InvalidArgumentException if $catid is not an integer
      */
-       public function fetchAllAssignedToPage(
-               PDO $pdo,
-               Config_Container $config,
-               $catid
-       ) {
-               if (!ctype_digit((string)$catid)) {
-                       throw new InvalidArgumentException(
-                               '$catid must be an integer.'
-                       );
-               }
-
-               //      Reset collection of banners
-               $this->_banners = array();
-
-               try {
-                       $sql = "
-                               SELECT id
-                                 FROM banners
-                                WHERE position = '{$this->_type}'
-                                  AND active = CAST(1 AS BOOLEAN)
-                        INTERSECT
-                               SELECT bid
-                                 FROM banners2navpages
-                                WHERE catid = :catid";
-
-                       $stmt = $pdo->prepare($sql);
-                       $stmt->bindParam(':catid', $catid, PDO::PARAM_INT);
-                       $stmt->execute();
-                       while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
-                               $this->_banners[]
-                                       = Toolkit_Banners_Banner::fetch($pdo, $config, $row['id']);
-                       }
-               } catch (PDOException $e) {
-                       return Toolkit_Common::handleError($e);
-               }
-       }
+    public function fetchAllAssignedToPage(
+        PDO $pdo,
+        Config_Container $config,
+        $catid
+    ) {
+        if (!ctype_digit((string)$catid)) {
+            throw new InvalidArgumentException(
+                '$catid must be an integer.'
+            );
+        }
+
+        //    Reset collection of banners
+        $this->_banners = array();
+
+        try {
+            $sql = "
+                SELECT id
+                  FROM banners
+                 WHERE position = '{$this->_type}'
+                   AND active = CAST(1 AS BOOLEAN)
+             INTERSECT
+                 SELECT bid
+                   FROM banners2navpages
+                  WHERE catid = :catid";
+
+            $stmt = $pdo->prepare($sql);
+            $stmt->bindParam(':catid', $catid, PDO::PARAM_INT);
+            $stmt->execute();
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $this->_banners[]
+                    = Toolkit_Banners_Banner::fetch($pdo, $config, $row['id']);
+            }
+        } catch (PDOException $e) {
+            return Toolkit_Common::handleError($e);
+        }
+    }
 
     //  }}}
     //  {{{ fetchAllAvailable()
@@ -303,48 +303,64 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * Sets all banners available for a page
      *
      * Takes into account if the page has any member categories assigned to it.
-        * If it does, it will also fetch and set the banners that are assigned to
-        * those categories and sub categories even if those banners are not
-        * assigned directly to the toolbox page.
+     * If it does, it will also fetch and set the banners that are assigned to
+     * those categories and sub categories even if those banners are not
+     * assigned directly to the toolbox page.
      *
      * @param PDO                                $pdo                Database handler
-        * @param Config_Container                   $config             Application Configuration
+     * @param Config_Container                   $config             Application Configuration
      * @param Toolkit_Members_CategoriesIterator $categoriesIterator Member categories iterator
      * @param integer                            $pageCatid          Toolbox page category id
-        *
+     *
      * @return false on error
      * @access public
      * @throws InvalidArgumentException if $pageCatid is not an integer
      */
-       public function fetchAllAvailable(
-               PDO $pdo,
-               Config_Container $config,
-               Toolkit_Members_CategoriesIterator $categoriesIterator,
-               $pageCatid
-       ) {
-               if (!ctype_digit((string)$pageCatid)) {
-                       throw new InvalidArgumentException(
-                               '$pageCatid must be an integer.'
-                       );
-               }
-
-               //      Reset collection of banners
-               $this->_banners = array();
-
-               try {
+    public function fetchAllAvailable(
+        PDO $pdo,
+        Config_Container $config,
+        Toolkit_Members_CategoriesIterator $categoriesIterator,
+        $pageCatid
+    ) {
+        if (!ctype_digit((string)$pageCatid)) {
+            throw new InvalidArgumentException(
+                '$pageCatid must be an integer.'
+            );
+        }
+
+        //    Reset collection of banners
+        $this->_banners = array();
+
+        try {
             $selectBannersAssignedToPage = "
                 SELECT bid
                   FROM banners2navpages
                  WHERE catid = :pageCatid";
 
+            $selectBannersAssignedToCounties = '';
             if ($county = filter_var($_REQUEST['countyId'], FILTER_VALIDATE_INT)) {
                 $selectBannersAssignedToCounties = "
                  UNION
                 SELECT banner
                   FROM banners2counties
                  WHERE county = {$county}";
-            } else {
-                $selectBannersAssignedToCounties = '';
+            } else if ($memberId = filter_var($_REQUEST['member_id'], FILTER_VALIDATE_INT)) {
+                $memberSql = "
+                SELECT county
+                  FROM members.member
+                 WHERE member_id = :member_id";
+                $memberStmt = $pdo->prepare($memberSql);
+                $memberStmt->bindParam(':member_id', $memberId, PDO::PARAM_INT);
+                $memberStmt->execute();
+                $countyId = $memberStmt->fetchColumn();
+                if ($countyId) {
+                    $selectBannersAssignedToCounties = "
+                     UNION
+                    SELECT banner
+                      FROM banners2counties
+                     WHERE county = {$countyId}";
+                } else {
+                }
             }
 
             $memberCatsOnPage = $this->_getMemberCatsAvailableOnPage(
@@ -353,15 +369,15 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
                 $pageCatid
             );
 
-                       $bannersWithinRunDate = "
-                               SELECT id
-                                 FROM banners
-                                WHERE campaign IN
-                                 (SELECT id
-                                    FROM banner_campaigns
-                                   WHERE sdate <= CURRENT_DATE
-                                         AND (edate >= CURRENT_DATE
-                                          OR edate IS NULL))";
+            $bannersWithinRunDate = "
+                SELECT id
+                  FROM banners
+                 WHERE campaign IN
+                  (SELECT id
+                     FROM banner_campaigns
+                    WHERE sdate <= CURRENT_DATE
+                      AND (edate >= CURRENT_DATE
+                       OR edate IS NULL))";
             $memberCats = implode(', ', $memberCatsOnPage);
             if (empty($memberCats)) {
                 $memberCats = 9999999;
@@ -370,30 +386,30 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
                 SELECT bid
                   FROM banners2membercategories
                  WHERE catid IN ($memberCats)";
-                       $sql = "
-                               (SELECT id
-                                 FROM banners
-                                WHERE position = '{$this->_type}'
-                                  AND active = CAST(1 AS BOOLEAN))
-                        INTERSECT
-                                       ($selectBannersAssignedToPage
-                               UNION
-                                       $selectBannersAssignedToMemberCatsOnPage
+            $sql = "
+                (SELECT id
+                  FROM banners
+                 WHERE position = '{$this->_type}'
+                   AND active = CAST(1 AS BOOLEAN))
+             INTERSECT
+                     ($selectBannersAssignedToPage
+                 UNION
+                     $selectBannersAssignedToMemberCatsOnPage
                     $selectBannersAssignedToCounties)
-                        INTERSECT
-                               ($bannersWithinRunDate)";
-
-                       $stmt = $pdo->prepare($sql);
-                       $stmt->bindParam(':pageCatid', $pageCatid, PDO::PARAM_INT);
-                       $stmt->execute();
-                       while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
-                               $this->_banners[]
-                                       = Toolkit_Banners_Banner::fetch($pdo, $config, $row['id']);
-                       }
-               } catch (PDOException $e) {
-                       return Toolkit_Common::handleError($e);
-               }
-       }
+             INTERSECT
+                ($bannersWithinRunDate)";
+
+            $stmt = $pdo->prepare($sql);
+            $stmt->bindParam(':pageCatid', $pageCatid, PDO::PARAM_INT);
+            $stmt->execute();
+            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
+                $this->_banners[]
+                    = Toolkit_Banners_Banner::fetch($pdo, $config, $row['id']);
+            }
+        } catch (PDOException $e) {
+            return Toolkit_Common::handleError($e);
+        }
+    }
 
     //  }}}
 
@@ -404,28 +420,28 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * Gets a banner from the collection
      *
      * @param integer $key index position to fetch a banner from
-        *
+     *
      * @return mixed false if no banners, else Toolkit_Banners_Banner obj
      * @access public
      * @throws InvalidArgumentException if $key is not null and not an integer
      */
-       public function getBanner($key = null)
-       {
-               //      Have no impressions
-               if (!count($this->_banners)) {
-                       return false;
-               }
-               if (!is_null($key) && !ctype_digit((string)$key)) {
-                       throw new InvalidArgumentException(
-                               '$key must be an integer.'
-                       );
-               }
-
-               reset($this->_banners);
-               return is_null($key)
-                       ? current($this->_banners)
-                       : $this->_banners[$key];
-       }
+    public function getBanner($key = null)
+    {
+        //    Have no impressions
+        if (!count($this->_banners)) {
+            return false;
+        }
+        if (!is_null($key) && !ctype_digit((string)$key)) {
+            throw new InvalidArgumentException(
+                '$key must be an integer.'
+            );
+        }
+
+        reset($this->_banners);
+        return is_null($key)
+            ? current($this->_banners)
+            : $this->_banners[$key];
+    }
 
     //  }}}
     //  {{{ _getMemberCatsAssignedToPage()
@@ -436,13 +452,13 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      *
      * @param PDO     $pdo       Database handler
      * @param integer $pageCatid Toolbox page category id
-        *
+     *
      * @return array Member categories that are assigned to the page
      * @access private
      */
-       private function _getMemberCatsAssignedToPage(PDO $pdo, $pageCatid)
-       {
-               $memberCatsOnPage = array();
+    private function _getMemberCatsAssignedToPage(PDO $pdo, $pageCatid)
+    {
+        $memberCatsOnPage = array();
         $sql = "
             SELECT category
               FROM member_categories2toolbox_pages
@@ -456,7 +472,7 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
         }
 
         return $memberCatsOnPage;
-       }
+    }
 
     //  }}}
     //  {{{ _getMemberCatsAvailableOnPage()
@@ -470,23 +486,23 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * @param unknown $pdo       Parameter description (if any) ...
      * @param object  $category  Parameter description (if any) ...
      * @param unknown $pageCatid Parameter description (if any) ...
-        *
+     *
      * @return array   Return description (if any) ...
      * @access private
      */
-       private function _getMemberCatsAvailableOnPage(
-               PDO $pdo,
-               Toolkit_Members_CategoriesIterator $category,
-               $pageCatid
-       ) {
-               $categories = array();
-               $memberCats = $this->_getMemberCatsAssignedToPage($pdo, $pageCatid);
-               foreach ($memberCats as $i) {
-                       $categories = array_merge($categories, $category->getTree($pdo, $i));
-               }
-
-               return array_merge($categories, $memberCats);
-       }
+    private function _getMemberCatsAvailableOnPage(
+        PDO $pdo,
+        Toolkit_Members_CategoriesIterator $category,
+        $pageCatid
+    ) {
+        $categories = array();
+        $memberCats = $this->_getMemberCatsAssignedToPage($pdo, $pageCatid);
+        foreach ($memberCats as $i) {
+            $categories = array_merge($categories, $category->getTree($pdo, $i));
+        }
+
+        return array_merge($categories, $memberCats);
+    }
 
     //  }}}
     //  {{{ getTotal()
@@ -498,10 +514,10 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
      * @return integer count of banners in the array
      * @access public
      */
-       public function getTotal()
-       {
-               return count($this->_banners);
-       }
+    public function getTotal()
+    {
+        return count($this->_banners);
+    }
 
     //  }}}
 
@@ -511,9 +527,9 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
     /**
      * Gets all available banners that can be displayed
      *
-        * Finds available banners in the collection that are within their run
-        * date (sdate - edate), have not exceeded their impression count
-        * (if applicable)
+     * Finds available banners in the collection that are within their run
+     * date (sdate - edate), have not exceeded their impression count
+     * (if applicable)
      *
      * @return array available banners
      * @access public
@@ -522,7 +538,7 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
     {
         $available = array();
         $today = new Date();
-               $today->setDate(strtotime('midnight'), DATE_FORMAT_UNIXTIME);
+        $today->setDate(strtotime('midnight'), DATE_FORMAT_UNIXTIME);
         foreach ($this->_banners as $i) {
             $sdate = $i->getCampaign()->getSDate();
             $edate = $i->getCampaign()->getEDate();
@@ -530,34 +546,34 @@ class Toolkit_Banners_BannersIterator implements IteratorAggregate
             $edate->addDays(1);
             $paidImps = $i->getCampaign()->getPaidImpressions();
             $accruedImps = $i->getCampaign()
-                               ->getImpressions();
+                ->getImpressions();
 
             $countingPaidImps = $paidImps;
             $hasAvailPaidImps = $accruedImps < $paidImps;
-                       $startBeforeOrOn = Date::compare($sdate, $today);
-                       if ($startBeforeOrOn < 1) {
+            $startBeforeOrOn = Date::compare($sdate, $today);
+            if ($startBeforeOrOn < 1) {
                 //  has end date
                 if (!is_null($edate->getDate())) {
                     //  end date has not passed
                     if ($edate->after($today)) {
                         if ($countingPaidImps && $hasAvailPaidImps) {
-                                                       //  going by impressions and have avail
+                            //  going by impressions and have avail
                             $available[] = $i;
                         } elseif (!$countingPaidImps) {
-                                                       //  end date hasn't passed and not counting imps
+                            //  end date hasn't passed and not counting imps
                             $available[] = $i;
                         }
                     }
                 } else {
                     if ($countingPaidImps && $hasAvailPaidImps) {
-                                               //  no end date, but going by impressions
+                        //  no end date, but going by impressions
                         $available[] = $i;
                     } elseif (!$countingPaidImps) {
-                                               //  no end date, not counting impressions
+                        //  no end date, not counting impressions
                         $available[] = $i;
                     }
                 }
-                       }
+            }
         }
 
         return $available;