File indexing completed on 2024-05-12 05:58:47

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  *
0022  * Created: 13.09.2017
0023  */
0024 
0025 class Default_Model_Section
0026 {
0027 
0028     public function fetchSponsorHierarchy()
0029     {
0030         $sql = "
0031             SELECT `section`.`name` AS `section_name`, `sponsor`.`sponsor_id`,`sponsor`.`name` AS `sponsor_name`
0032             FROM `section_sponsor`
0033             JOIN `sponsor` ON `sponsor`.`sponsor_id` = `section_sponsor`.`sponsor_id`
0034             JOIN `section` ON `section`.`section_id` = `section_sponsor`.`section_id`
0035 
0036         ";
0037         $resultSet = $this->getAdapter()->fetchAll($sql);
0038         $optgroup = array();
0039         foreach ($resultSet as $item) {
0040             $optgroup[$item['section_name']][$item['sponsor_id']] = $item['sponsor_name'];
0041         }
0042 
0043         return $optgroup;
0044     }
0045 
0046     /**
0047      * @return Zend_Db_Adapter_Abstract
0048      */
0049     private function getAdapter()
0050     {
0051         return Zend_Db_Table::getDefaultAdapter();
0052     }
0053 
0054     public function fetchAllSections()
0055     {
0056         $sql = "
0057             SELECT `section_id`,`name`,`description`
0058             FROM `section`
0059             WHERE `is_active` = 1
0060             AND `hide` = 0
0061             ORDER BY `section`.`order`
0062         ";
0063         $resultSet = $this->getAdapter()->fetchAll($sql);
0064 
0065         return $resultSet;
0066     }
0067 
0068     public function fetchAllSectionsAndCategories()
0069     {
0070         $sql = "
0071             SELECT 
0072             `s`.`section_id`
0073             ,`s`.`name`
0074             ,`s`.`description`
0075             ,`c`.`project_category_id`
0076             ,`pc`.`title`
0077             FROM `section` `s`
0078             JOIN `section_category` `c` ON `s`.`section_id` = `c`.`section_id`
0079             JOIN `project_category` `pc` ON `c`.`project_category_id` = `pc`.`project_category_id` AND `pc`.`is_deleted` = 0 AND `pc`.`is_active` = 1 AND `pc`.`rgt`=`pc`.`lft`+1
0080             WHERE `s`.`is_active` = 1
0081             ORDER BY `s`.`name` , `pc`.`title`
0082         ";
0083         $resultSet = $this->getAdapter()->fetchAll($sql);
0084 
0085         return $resultSet;
0086     }
0087 
0088     public function fetchCategoriesWithPayout()
0089     {
0090         $sql = "SELECT `m`.`section_id`,`m`.`project_category_id`,SUM(`m`.`credits_section`)/100 `amount`, `pc`.`title`
0091                 FROM `micro_payout` `m`
0092                 JOIN `project_category` `pc` ON `m`.`project_category_id` = `pc`.`project_category_id` 
0093                 WHERE `m`.`paypal_mail` IS NOT NULL AND `m`.`paypal_mail` <> '' AND (`m`.`paypal_mail` REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$')            
0094                 AND `m`.`yearmonth` = DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m')  AND `m`.`is_license_missing` = 0 AND `m`.`is_source_missing`=0 AND `m`.`is_pling_excluded` = 0 
0095                 AND `m`.`is_member_pling_excluded`=0
0096                 GROUP BY `project_category_id`
0097                 ";
0098         $resultSet = $this->getAdapter()->fetchAll($sql);
0099 
0100         return $resultSet;
0101 
0102     }
0103 
0104     public function fetchCategoriesWithPlinged()
0105     {
0106         $sql = "SELECT
0107                 `p`.`project_category_id`, `m`.`section_id`,`pc`.`title`
0108                 FROM `project_plings` `pl`
0109                 INNER JOIN `stat_projects` `p` ON `pl`.`project_id` = `p`.`project_id` AND `p`.`status` = 100
0110                 INNER JOIN `section_category` `m` ON `p`.`project_category_id` = `m`.`project_category_id`
0111                 INNER JOIN `project_category` `pc` ON `m`.`project_category_id` = `pc`.`project_category_id`                
0112                 WHERE `pl`.`is_deleted` = 0 AND `pl`.`is_active` = 1
0113                 GROUP BY `p`.`project_category_id`
0114                 ORDER BY `pc`.`title` ASC
0115                 ";
0116         $resultSet = $this->getAdapter()->fetchAll($sql);
0117 
0118         return $resultSet;
0119     }
0120 
0121     /** removed inner join supporters
0122      * public function fetchCategoriesWithPlinged()
0123      * {
0124      * $sql = " select
0125      * p.project_category_id, m.section_id,pc.title
0126      * from project_plings pl
0127      * inner join stat_projects p on pl.project_id = p.project_id and p.status = 100
0128      * inner join section_category m on p.project_category_id = m.project_category_id
0129      * inner join project_category pc on m.project_category_id = pc.project_category_id
0130      * inner join (
0131      * select distinct su2.member_id
0132      * from section_support_paypements ss
0133      * JOIN support su2 ON su2.id = ss.support_id
0134      * where yearmonth = DATE_FORMAT(NOW()- INTERVAL 1 MONTH,'%Y%m')
0135      * ) ss on pl.member_id = ss.member_id
0136      * where pl.is_deleted = 0 and pl.is_active = 1
0137      * group by p.project_category_id
0138      * ";
0139      * $resultSet = $this->getAdapter()->fetchAll($sql);
0140      * return $resultSet;
0141      * }
0142      *
0143      * @param int $section_id
0144      *
0145      * @return array
0146      */
0147     public function getNewActivePlingProduct($section_id = null)
0148     {
0149         $sqlSection = "";
0150 
0151         if (!empty($section_id)) {
0152             $sqlSection = " and m.section_id = " . $section_id;
0153         }
0154 
0155         $sql = "
0156                 select 
0157                 pl.member_id as pling_member_id
0158                 ,pl.project_id                        
0159                 ,p.title
0160                 ,p.image_small
0161                 ,p.laplace_score
0162                 ,p.count_likes
0163                 ,p.count_dislikes   
0164                 ,p.member_id 
0165                 ,p.profile_image_url
0166                 ,p.username
0167                 ,p.cat_title as catTitle
0168                 ,(
0169                 select max(created_at) from project_plings pt where pt.member_id = pl.member_id and pt.project_id=pl.project_id
0170                 ) as created_at
0171                 ,(select count(1) from project_plings pl2 where pl2.project_id = p.project_id and pl2.is_active = 1 and pl2.is_deleted = 0  ) as sum_plings
0172                 from project_plings pl
0173                 inner join stat_projects p on pl.project_id = p.project_id and p.status=100  
0174                 inner join section_category m on p.project_category_id = m.project_category_id                 
0175                 where pl.is_deleted = 0 and pl.is_active = 1 " .$sqlSection."
0176                 order by created_at desc   
0177                 limit 20
0178         ";
0179         $resultSet = $this->getAdapter()->fetchAll($sql);
0180 
0181         return $resultSet;
0182     }
0183 
0184     public function fetchTopProductsPerSection($section_id = null)
0185     {
0186         $sqlSection = "";
0187 
0188         if (!empty($section_id)) {
0189             $sqlSection = " and m.section_id = " . $section_id;
0190         }
0191 
0192         $sql = "
0193             select 
0194             p.project_id,
0195             p.member_id,
0196             p.project_category_id,
0197             p.title,
0198             p.description,
0199             p.created_at,
0200             p.changed_at,
0201             p.image_small,
0202             p.username,
0203             p.profile_image_url,
0204             p.cat_title,
0205             p.laplace_score,
0206             sum(m.credits_plings)/100 AS probably_payout_amount
0207             from stat_projects p,micro_payout m
0208             where p.project_id = m.project_id
0209             and m.paypal_mail is not null and m.paypal_mail <> '' and (m.paypal_mail regexp '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$') 
0210             " . $sqlSection . "
0211             and m.yearmonth = DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m')  and m.is_license_missing = 0 and m.is_source_missing=0 and m.is_pling_excluded = 0 
0212             and m.is_member_pling_excluded=0
0213             GROUP BY m.project_id
0214             order by sum(m.credits_plings) desc
0215             limit 20
0216         ";
0217 
0218         $resultSet = $this->getAdapter()->fetchAll($sql);
0219 
0220         return $resultSet;
0221     }
0222 
0223     public function fetchTopPlingedProductsPerSection($section_id = null)
0224     {
0225         $sqlSection = "";
0226 
0227         if (!empty($section_id)) {
0228             $sqlSection = " and m.section_id = " . $section_id;
0229         }
0230 
0231         // ignore if supporter still active
0232         // inner join (
0233         //     select distinct su2.member_id
0234         //     from section_support_paypements ss
0235         //     JOIN support su2 ON su2.id = ss.support_id
0236         //     where yearmonth = DATE_FORMAT(NOW()- INTERVAL 1 MONTH,'%Y%m')
0237         // ) ss on pl.member_id = ss.member_id
0238 
0239         $sql = "
0240                 select pl.project_id
0241                 ,count(1) as sum_plings 
0242                 ,(select count(1) from project_plings pls where pls.project_id=pl.project_id and pls.is_deleted=0) as sum_plings_all
0243                 ,p.title
0244                 ,p.image_small
0245                 ,p.laplace_score
0246                 ,p.count_likes
0247                 ,p.count_dislikes   
0248                 ,p.member_id 
0249                 ,p.profile_image_url
0250                 ,p.username
0251                 ,p.cat_title as catTitle
0252                 ,p.project_changed_at
0253                 ,p.version
0254                 ,p.description
0255                 ,p.package_names
0256                 ,p.count_comments
0257                 ,p.changed_at
0258                 ,p.created_at
0259                 from project_plings pl
0260                 inner join stat_projects p on pl.project_id = p.project_id and p.status = 100
0261                 inner join section_category m on p.project_category_id = m.project_category_id
0262                 where pl.is_deleted = 0 and pl.is_active = 1 " . $sqlSection . "
0263                 group by pl.project_id
0264                 order by sum_plings desc ,sum_plings_all desc
0265                 limit 20        
0266         ";
0267 
0268         $resultSet = $this->getAdapter()->fetchAll($sql);
0269 
0270         return $resultSet;
0271     }
0272 
0273     /**
0274      *  ignore if supporter still active
0275      * inner join (
0276      * select distinct su2.member_id
0277      * from section_support_paypements ss
0278      * JOIN support su2 ON su2.id = ss.support_id
0279      * where yearmonth = DATE_FORMAT(NOW()- INTERVAL 1 MONTH,'%Y%m')
0280      * ) ss on pl.member_id = ss.member_id
0281      */
0282     public function fetchTopPlingedProductsPerCategory($cat_id)
0283     {
0284         $sql = "SELECT `pl`.`project_id`
0285                 ,count(1) AS `sum_plings` 
0286                 ,(SELECT count(1) FROM `project_plings` `pls` WHERE `pls`.`project_id`=`pl`.`project_id` AND `pls`.`is_deleted`=0) AS `sum_plings_all`
0287                 ,`p`.`title`
0288                 ,`p`.`image_small`
0289                 ,`p`.`laplace_score`
0290                 ,`p`.`count_likes`
0291                 ,`p`.`count_dislikes`   
0292                 ,`p`.`member_id` 
0293                 ,`p`.`profile_image_url`
0294                 ,`p`.`username`
0295                 ,`p`.`cat_title` AS `catTitle`
0296                 ,`p`.`project_changed_at`
0297                 ,`p`.`version`
0298                 ,`p`.`description`
0299                 ,`p`.`package_names`
0300                 ,`p`.`count_comments`
0301                 ,`p`.`changed_at`
0302                 ,`p`.`created_at`
0303                 FROM `project_plings` `pl`
0304                 INNER JOIN `stat_projects` `p` ON `pl`.`project_id` = `p`.`project_id` AND `p`.`status` = 100                                
0305                 WHERE `pl`.`is_deleted` = 0 AND `pl`.`is_active` = 1 AND `p`.`project_category_id`=:cat_id
0306                 GROUP BY `pl`.`project_id`
0307                 ORDER BY `sum_plings` DESC 
0308                 LIMIT 20     ";
0309         $resultSet = $this->getAdapter()->fetchAll($sql, array("cat_id" => $cat_id));
0310 
0311         return $resultSet;
0312     }
0313 
0314     public function fetchTopProductsPerCategory($cat_id)
0315     {
0316         $sql = "SELECT 
0317                 `p`.`project_id`,
0318                 `p`.`member_id`,
0319                 `p`.`project_category_id`,
0320                 `p`.`title`,
0321                 `p`.`description`,
0322                 `p`.`created_at`,
0323                 `p`.`changed_at`,
0324                 `p`.`image_small`,
0325                 `p`.`username`,
0326                 `p`.`profile_image_url`,
0327                 `p`.`cat_title`,
0328                 `p`.`laplace_score`,
0329                 sum(`m`.`credits_plings`)/100 AS `probably_payout_amount`
0330                 FROM `stat_projects` `p`,`micro_payout` `m`
0331                 WHERE  `p`.`project_id` = `m`.`project_id`
0332                      AND `m`.`paypal_mail` IS NOT NULL AND `m`.`paypal_mail` <> ''
0333                      AND (`m`.`paypal_mail` REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$') 
0334                       AND `m`.`yearmonth` = DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m')  AND `m`.`is_license_missing` = 0 AND `m`.`is_source_missing`=0 AND `m`.`is_pling_excluded` = 0 
0335                         AND `m`.`is_member_pling_excluded`=0           
0336                             AND `p`.`project_category_id` = :cat_id 
0337            GROUP BY `m`.`project_id`   
0338                 ORDER BY sum(`m`.`credits_plings`) DESC 
0339                 LIMIT 20";
0340         $resultSet = $this->getAdapter()->fetchAll($sql, array("cat_id" => $cat_id));
0341 
0342         return $resultSet;
0343     }
0344 
0345     public function fetchProbablyPayoutLastMonth($section_id)
0346     {
0347         $sqlSection = "";
0348 
0349         if (!empty($section_id)) {
0350             $sqlSection = " and s.section_id = " . $section_id;
0351         }
0352 
0353         /*
0354         $sql = "select  sum(probably_payout_amount) probably_payout_amount
0355             from member_dl_plings m, section s, section_category c
0356             where  s.section_id = c.section_id and c.project_category_id = m.project_category_id
0357            ".$sqlSection."
0358             and m.paypal_mail is not null and m.paypal_mail <> ''
0359             and (m.paypal_mail regexp '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$')
0360             and m.yearmonth = DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m')  and m.is_license_missing = 0 and m.is_source_missing=0 and m.is_pling_excluded = 0
0361             and m.is_member_pling_excluded=0
0362             ";
0363         */
0364         $sql = "SELECT s.sum_amount_payout AS probably_payout_amount FROM section_funding_stats s
0365                 WHERE s.yearmonth = DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m')
0366                " . $sqlSection . "
0367                 ";
0368         $resultSet = $this->getAdapter()->fetchRow($sql);
0369 
0370         return $resultSet['probably_payout_amount'];
0371 
0372     }
0373 
0374     public function fetchTopPlingedCreatorPerSection($section_id = null)
0375     {
0376         $sqlSection = "";
0377 
0378         if (!empty($section_id)) {
0379             $sqlSection = " and mm.section_id = " . $section_id;
0380         }
0381 
0382         /**
0383          * inner join (
0384          * select distinct su2.member_id
0385          * from section_support_paypements ss
0386          * JOIN support su2 ON su2.id = ss.support_id
0387          * where yearmonth = DATE_FORMAT(NOW() - INTERVAL 1 MONTH,'%Y%m')
0388          * ) ss on pl.member_id = ss.member_id
0389          */
0390         $sql = "select p.member_id,
0391         count(1) as cnt,
0392         (select count(1) from project_plings pls , stat_projects ppp where pls.project_id=ppp.project_id and pls.is_deleted=0 and ppp.member_id=p.member_id) as sum_plings_all,
0393         m.username,
0394         m.profile_image_url,
0395         m.created_at
0396         from stat_projects p
0397         join project_plings pl on p.project_id = pl.project_id
0398         join member m on p.member_id = m.member_id
0399         inner join section_category mm on p.project_category_id = mm.project_category_id
0400         
0401         where p.status = 100
0402         and pl.is_deleted = 0 and pl.is_active = 1 " . $sqlSection . "  
0403         group by p.member_id
0404         order by cnt desc 
0405         limit 20
0406         ";
0407         $resultSet = $this->getAdapter()->fetchAll($sql);
0408 
0409         return $resultSet;
0410     }
0411 
0412     public function fetchTopCreatorPerSection($section_id = null)
0413     {
0414         $sqlSection = "";
0415 
0416         if (!empty($section_id)) {
0417             $sqlSection = " and s.section_id = " . $section_id;
0418         }
0419 
0420         $sql = "
0421             select                 
0422                 me.username,
0423                 me.profile_image_url,
0424                 m.member_id,
0425                 sum(m.credits_plings)/100 probably_payout_amount
0426                 from micro_payout m, section s, section_category c, member me
0427                 where s.section_id = c.section_id and c.project_category_id = m.project_category_id AND me.member_id = m.member_id
0428                 and m.paypal_mail is not null and m.paypal_mail <> ''
0429                 and (m.paypal_mail regexp '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$') 
0430                 " . $sqlSection . "  
0431                 and m.yearmonth =  DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m') 
0432            and m.is_license_missing = 0 and m.is_source_missing=0 and m.is_pling_excluded = 0 
0433                 and m.is_member_pling_excluded=0
0434                 group by me.username,me.profile_image_url,m.member_id
0435                 order by sum(m.credits_plings) desc
0436                 limit 20
0437         ";
0438 
0439         $resultSet = $this->getAdapter()->fetchAll($sql);
0440 
0441         return $resultSet;
0442     }
0443 
0444     /** removed inner join suppoerts
0445      * public function fetchTopPlingedCreatorPerCategory($cat_id)
0446      * {
0447      *
0448      * $sql = "select p.member_id,
0449      * count(1) as cnt,
0450      * (select count(1) from project_plings pls , stat_projects ppp where pls.project_id=ppp.project_id and
0451      * pls.is_deleted=0 and ppp.member_id=p.member_id) as sum_plings_all, m.username, m.profile_image_url, m.created_at
0452      * from stat_projects p join project_plings pl on p.project_id = pl.project_id join member m on p.member_id =
0453      * m.member_id inner join ( select distinct su2.member_id from section_support_paypements ss JOIN support su2 ON
0454      * su2.id = ss.support_id where yearmonth = DATE_FORMAT(NOW() - INTERVAL 1 MONTH,'%Y%m')
0455      * ) ss on pl.member_id = ss.member_id
0456      * where p.status = 100 and p.project_category_id=:cat_id
0457      * and pl.is_deleted = 0 and pl.is_active = 1
0458      * group by p.member_id
0459      * order by cnt desc
0460      * limit 20";
0461      * $resultSet = $this->getAdapter()->fetchAll($sql,array("cat_id"=>$cat_id));
0462      * return $resultSet;
0463      * }
0464      */
0465     public function fetchTopPlingedCreatorPerCategory($cat_id)
0466     {
0467 
0468         $sql = "SELECT `p`.`member_id`,
0469                     count(1) AS `cnt`,
0470                     (SELECT count(1) FROM `project_plings` `pls` , `stat_projects` `ppp` WHERE `pls`.`project_id`=`ppp`.`project_id` AND `pls`.`is_deleted`=0 AND `ppp`.`member_id`=`p`.`member_id`) AS `sum_plings_all`,
0471                     `m`.`username`,
0472                     `m`.`profile_image_url`,
0473                     `m`.`created_at`
0474                     FROM `stat_projects` `p`
0475                     JOIN `project_plings` `pl` ON `p`.`project_id` = `pl`.`project_id`
0476                     JOIN `member` `m` ON `p`.`member_id` = `m`.`member_id`                                       
0477                     WHERE `p`.`status` = 100 AND `p`.`project_category_id`=:cat_id
0478                     AND `pl`.`is_deleted` = 0 AND `pl`.`is_active` = 1  
0479                     GROUP BY `p`.`member_id`
0480                     ORDER BY `cnt` DESC 
0481                     LIMIT 20";
0482         $resultSet = $this->getAdapter()->fetchAll($sql, array("cat_id" => $cat_id));
0483 
0484         return $resultSet;
0485     }
0486 
0487     public function fetchTopCreatorPerCategory($cat_id)
0488     {
0489 
0490         $sql = "SELECT              
0491                 `p`.`username`,
0492                 `p`.`profile_image_url`,
0493                 `p`.`member_id`,
0494                 SUM(`m`.`credits_plings`)/100 `probably_payout_amount`
0495                 FROM `stat_projects` `p`, `micro_payout` `m`
0496                 WHERE `p`.`member_id` = `m`.`member_id` AND `p`.`project_id` = `m`.`project_id`
0497                 AND `m`.`paypal_mail` IS NOT NULL AND `m`.`paypal_mail` <> ''
0498                 AND (`m`.`paypal_mail` REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$') 
0499                  AND `m`.`yearmonth` =  DATE_FORMAT(CURRENT_DATE() - INTERVAL 1 MONTH, '%Y%m') AND `m`.`is_license_missing` = 0 AND `m`.`is_source_missing`=0 AND `m`.`is_pling_excluded` = 0 
0500                 AND `m`.`is_member_pling_excluded`=0
0501                 AND `p`.`project_category_id` = :cat_id
0502                 GROUP BY `p`.`username`,`p`.`profile_image_url`,`p`.`member_id`
0503                 ORDER BY sum(`m`.`credits_plings`) DESC 
0504                 LIMIT 20";
0505         $resultSet = $this->getAdapter()->fetchAll($sql, array("cat_id" => $cat_id));
0506 
0507         return $resultSet;
0508     }
0509 
0510     public function fetchFirstSectionForStoreCategories($category_array)
0511     {
0512         $sql = "
0513             SELECT *
0514             FROM `section`
0515             JOIN `section_category` ON `section_category`.`section_id` = `section`.`section_id`
0516             WHERE `is_active` = 1
0517             AND `section_category`.`project_category_id` IN (:category_id)
0518             LIMIT 1
0519         ";
0520         $resultSet = $this->getAdapter()->fetchRow($sql, array('category_id' => $category_array));
0521 
0522         return $resultSet;
0523     }
0524 
0525     public function fetchSectionForCategory($category_id)
0526     {
0527         $sql = "
0528             SELECT *
0529             FROM `section`
0530             JOIN `section_category` ON `section_category`.`section_id` = `section`.`section_id`
0531             WHERE `is_active` = 1
0532             AND `section_category`.`project_category_id` = :category_id
0533             LIMIT 1
0534         ";
0535         $resultSet = $this->getAdapter()->fetchRow($sql, array('category_id' => $category_id));
0536 
0537         return $resultSet;
0538     }
0539 
0540     public function isMemberSectionSupporter($section_id, $member_id)
0541     {
0542         $sql = "
0543             SELECT *
0544             FROM `section_support` 
0545             JOIN `section` ON `section`.`section_id` = `section_support`.`section_id`
0546             JOIN `support` ON `support`.`id` = `section_support`.`support_id` AND `support`.`status_id` = 2
0547             WHERE `section_support`.`is_active` = 1
0548             AND `section`.`section_id` = :section_id
0549             AND `support`.`member_id` = :member_id
0550             LIMIT 1
0551         ";
0552         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_id' => $section_id, 'member_id' => $member_id));
0553 
0554         if ($resultSet) {
0555             return true;
0556         }
0557 
0558         return false;
0559     }
0560 
0561     public function wasMemberSectionSupporter($section_id, $member_id)
0562     {
0563         $sql = "
0564             SELECT *
0565             FROM `section_support` 
0566             JOIN `section` ON `section`.`section_id` = `section_support`.`section_id`
0567             JOIN `support` ON `support`.`id` = `section_support`.`support_id` AND `support`.`status_id` >= 2
0568             WHERE `section_support`.`is_active` = 1
0569             AND `section`.`section_id` = :section_id
0570             AND `support`.`member_id` = :member_id
0571             LIMIT 1
0572         ";
0573         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_id' => $section_id, 'member_id' => $member_id));
0574 
0575         if ($resultSet) {
0576             return true;
0577         }
0578 
0579         return false;
0580     }
0581 
0582     /**
0583      * @param int $yearmonth
0584      *
0585      * @return array
0586      */
0587     public function fetchAllSectionStats($yearmonth = null, $isForAdmin = false)
0588     {
0589         $sql = "SELECT * FROM `section_funding_stats` `p`
0590                 WHERE `p`.`yearmonth` = :yearmonth";
0591 
0592         if (!$isForAdmin) {
0593             $sql .= " AND p.yearmonth >= DATE_FORMAT((NOW() - INTERVAL 1 MONTH),'%Y%m')";
0594         }
0595 
0596         if (empty($yearmonth)) {
0597             $yearmonth = "DATE_FORMAT(NOW(),'%Y%m')";
0598         }
0599         $resultSet = $this->getAdapter()->fetchAll($sql, array('yearmonth' => $yearmonth));
0600 
0601         return $resultSet;
0602     }
0603 
0604     /**
0605      * @param int $yearmonth
0606      *
0607      * @return array
0608      */
0609     public function fetchSectionStats($yearmonth = null, $section_id, $isForAdmin = false)
0610     {
0611         $sql = "SELECT * FROM `section_funding_stats` `p`
0612                 WHERE `p`.`yearmonth` = :yearmonth
0613                 AND `p`.`section_id` = :section_id";
0614 
0615         if (!$isForAdmin) {
0616             $sql .= " AND p.yearmonth >= DATE_FORMAT((NOW()),'%Y%m')";
0617         }
0618 
0619         if (empty($yearmonth)) {
0620             $yearmonth = "DATE_FORMAT(NOW(),'%Y%m')";
0621         }
0622         $resultSet = $this->getAdapter()->fetchRow($sql, array('yearmonth' => $yearmonth, 'section_id' => $section_id));
0623 
0624         return $resultSet;
0625     }
0626 
0627     /**
0628      * @param int $section_id
0629      *
0630      * @return array
0631      */
0632     public function fetchSectionStatsLastMonth($section_id)
0633     {
0634         $sql = "SELECT * FROM `section_funding_stats` `p`
0635                 WHERE `p`.`yearmonth` = DATE_FORMAT(NOW() - INTERVAL 1 MONTH,'%Y%m')
0636                 AND `p`.`section_id` = :section_id";
0637 
0638         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_id' => $section_id));
0639 
0640         return $resultSet;
0641     }
0642 
0643     /**
0644      * @param int $yearmonth
0645      *
0646      * @return array
0647      */
0648     public function fetchSectionSupportStats($yearmonth = null, $section_id, $isForAdmin = false)
0649     {
0650         $sql = "SELECT `p`.`yearmonth`, `p`.`section_id`, SUM(`p`.`tier`) AS `sum_support`, NULL AS `sum_sponsor`, NULL AS `sum_dls`, NULL AS `sum_dls_payout`, NULL AS `sum_amount_payout`, NULL AS `sum_amount`
0651           ,(SELECT COUNT(1) AS `num_supporter` FROM (
0652               SELECT COUNT(1) AS `num_supporter`,`ss`.`section_id`, `su2`.`member_id` FROM `section_support_paypements` `ss`
0653                       JOIN `support` `su2` ON `su2`.`id` = `ss`.`support_id`
0654                       WHERE `ss`.`yearmonth` = :yearmonth
0655                       GROUP BY `ss`.`section_id`, `su2`.`member_id`
0656                     ) `A`
0657                     WHERE `A`.`section_id` = `p`.`section_id`
0658                 ) AS `num_supporter` FROM `section_support_paypements` `p`
0659           WHERE `p`.`yearmonth` = :yearmonth
0660           AND `p`.`section_id` = :section_id ";
0661 
0662         if (!$isForAdmin) {
0663             $sql .= " AND p.yearmonth >= DATE_FORMAT((NOW() - INTERVAL 1 MONTH),'%Y%m')";
0664         }
0665 
0666         $sql .= " GROUP BY p.yearmonth, p.section_id";
0667         if (empty($yearmonth)) {
0668             $yearmonth = "DATE_FORMAT(NOW(),'%Y%m')";
0669         }
0670         $resultSet = $this->getAdapter()->fetchRow($sql, array('yearmonth' => $yearmonth, 'section_id' => $section_id));
0671 
0672         return $resultSet;
0673     }
0674 
0675     public function fetchSection($section_id)
0676     {
0677         $sql = "
0678             SELECT *
0679             FROM `section`
0680             WHERE `is_active` = 1 AND `section_id` = :section_id
0681         ";
0682         $resultSet = $this->getAdapter()->fetchRow($sql, array('section_id' => $section_id));
0683 
0684         return $resultSet;
0685     }
0686 
0687     public function getAllDownloadYears($isForAdmin = false)
0688     {
0689         $sql = "
0690                 SELECT 
0691                     SUBSTR(`member_dl_plings`.`yearmonth`,1,4) AS `year`,
0692                     MAX(`member_dl_plings`.`yearmonth`) AS `max_yearmonth`
0693                 FROM
0694                     `member_dl_plings`";
0695         if (!$isForAdmin) {
0696             $sql .= " WHERE SUBSTR(`member_dl_plings`.`yearmonth`,1,4) = DATE_FORMAT(NOW(),'%Y')";
0697         }
0698 
0699         $sql .= " GROUP BY SUBSTR(`member_dl_plings`.`yearmonth`,1,4)
0700                 ORDER BY SUBSTR(`member_dl_plings`.`yearmonth`,1,4) DESC
0701             ";
0702         $result = Zend_Db_Table::getDefaultAdapter()->query($sql);
0703 
0704         if ($result->rowCount() > 0) {
0705             return $result->fetchAll();
0706         } else {
0707             return array();
0708 
0709         }
0710     }
0711     
0712     
0713     public function getAllDownloadMonths($year, $isForAdmin = false)
0714     {
0715         $sql = "
0716                 SELECT 
0717                     DISTINCT `member_dl_plings`.`yearmonth`
0718                 FROM
0719                     `member_dl_plings`
0720                 WHERE
0721                 SUBSTR(`member_dl_plings`.`yearmonth`,1,4) = :year ";
0722                 
0723         if(!$isForAdmin) {
0724             $sql .= " AND `member_dl_plings`.`yearmonth` >= DATE_FORMAT((NOW() - INTERVAL 1 MONTH),'%Y%m')";
0725         }
0726 
0727         $sql .= " ORDER BY `member_dl_plings`.`yearmonth` DESC";
0728         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('year' => $year));
0729 
0730         if ($result->rowCount() > 0) {
0731             return $result->fetchAll();
0732         } else {
0733             return array();
0734 
0735         }
0736     }
0737     
0738 }