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

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Default_Model_Info
0024 {
0025 
0026     const WALLPAPERCATEGORYID = '295';
0027 
0028     const TAG_ISORIGINAL = 'original-product';
0029 
0030     public function getLast200ImgsProductsForAllStores($limit = 200)
0031     {
0032 
0033         /** @var Zend_Cache_Core $cache */
0034         $cache = Zend_Registry::get('cache');
0035         $cacheName = __FUNCTION__ . md5('getLast200ImgsProductsForAllStores' . $limit);
0036         if ($resultSet = $cache->load($cacheName)) {
0037             return $resultSet;
0038         } else {
0039 
0040             $activeCategories = $this->getActiveCategoriesForAllStores();
0041 
0042             $sql = '
0043                 SELECT 
0044                     `image_small`
0045                     ,`project_id`
0046                     ,`title`
0047                 FROM
0048                     `project`
0049                 WHERE
0050                     `project`.`image_small` IS NOT NULL 
0051                     AND `project`.`status` = 100
0052                         AND `project`.`project_category_id` IN (' . implode(',', $activeCategories) . ')
0053                 ORDER BY ifnull(`project`.`changed_at`, `project`.`created_at`) DESC
0054                 ';
0055 
0056             if (isset($limit)) {
0057                 $sql .= ' limit ' . (int)$limit;
0058             }
0059 
0060             $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0061 
0062             if (count($resultSet) > 0) {
0063 
0064                 $cache->save($resultSet, $cacheName, array(), 14400);
0065 
0066                 return $resultSet;
0067             } else {
0068                 return array();
0069             }
0070         }
0071     }
0072 
0073     public function getActiveCategoriesForAllStores($limit = null)
0074     {
0075         $sql = '
0076         SELECT DISTINCT
0077             `config_store_category`.`project_category_id`
0078         FROM
0079             `config_store`
0080         JOIN
0081             `config_store_category` ON `config_store`.`store_id` = `config_store_category`.`store_id`
0082         JOIN
0083             `project_category` ON `config_store_category`.`project_category_id` = `project_category`.`project_category_id`
0084         WHERE `project_category`.`is_active` = 1
0085         ORDER BY `config_store_category`.`order`;
0086         ';
0087 
0088         if (isset($limit)) {
0089             $sql .= ' limit ' . (int)$limit;
0090         }
0091 
0092         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0093 
0094         if (count($resultSet) > 0) {
0095             $values = array_map(function ($row) {
0096                 return $row['project_category_id'];
0097             }, $resultSet);
0098 
0099             return $values;
0100         } else {
0101             return array();
0102         }
0103     }
0104 
0105     public function getActiveStoresForCrossDomainLogin($limit = null)
0106     {
0107         $sql = '
0108         SELECT DISTINCT
0109             `config_store`.`host`
0110         FROM
0111             `config_store`
0112         WHERE `config_store`.`cross_domain_login` = 1
0113         ORDER BY `config_store`.`order`;
0114         ';
0115 
0116         if (isset($limit)) {
0117             $sql .= ' limit ' . (int)$limit;
0118         }
0119 
0120         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0121 
0122         if (count($resultSet) > 0) {
0123             $values = array_map(function ($row) {
0124                 return $row['host'];
0125             }, $resultSet);
0126 
0127             return $values;
0128         } else {
0129             return array();
0130         }
0131     }
0132 
0133     public function countTotalActiveMembers()
0134     {
0135 
0136         $cacheName = __FUNCTION__ . md5('countTotalActiveMembers');
0137         $cache = Zend_Registry::get('cache');
0138 
0139         $result = $cache->load($cacheName);
0140 
0141         if ($result) {
0142             return (int)$result['count_active_members'];
0143         }
0144 
0145         $sql = "SELECT count(1) AS `count_active_members` FROM (                    
0146                     SELECT count(1) AS `count_active_projects` FROM `project` `p`
0147                     WHERE `p`.`status` = 100
0148                     AND `p`.`type_id` = 1
0149                     GROUP BY `p`.`member_id`
0150                 ) AS `A`;";
0151 
0152         $result = $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql);
0153         $cache->save($result, $cacheName);
0154 
0155         return (int)$result['count_active_members'];
0156     }
0157 
0158     /**
0159      * if category id not set the latest comments for all categories on the current host wil be returned.
0160      *
0161      * @param int      $limit
0162      * @param int|null $project_category_id
0163      * @param array    $tags
0164      *
0165      * @return array
0166      * @throws Zend_Cache_Exception
0167      * @throws Zend_Exception
0168      */
0169     public function getLatestComments($limit = 5, $project_category_id = null, $tags = null)
0170     {
0171         /** @var Zend_Cache_Core $cache */
0172         $cache = Zend_Registry::get('cache');
0173         $cacheNameTags = is_array($tags) ? implode('_', $tags) : '';
0174         $cacheName =
0175             __FUNCTION__ . '_new_' . md5(Zend_Registry::get('store_host') . (int)$limit . (int)$project_category_id . $cacheNameTags);
0176 
0177         if (($latestComments = $cache->load($cacheName))) {
0178             return $latestComments;
0179         }
0180 
0181         if (empty($project_category_id)) {
0182             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0183         } else {
0184             $activeCategories = $this->getActiveCategoriesForCatId($project_category_id);
0185         }
0186 
0187         if (count($activeCategories) == 0) {
0188             return array();
0189         }
0190 
0191         $sql = '                
0192                    SELECT
0193                        `comment_id`
0194                        ,`comment_text`
0195                        ,`member`.`member_id`
0196                        ,`member`.`profile_image_url`
0197                        ,`comment_created_at`
0198                        ,`member`.`username`
0199                        ,`comment_target_id`
0200                        ,`title`
0201                        ,`stat_projects`.`project_id`  
0202                        ,`cat_title` AS `catTitle`                                  
0203                    FROM `comments`
0204                    STRAIGHT_JOIN `member` ON `comments`.`comment_member_id` = `member`.`member_id`
0205                    INNER JOIN `stat_projects` ON `comments`.`comment_target_id` = `stat_projects`.`project_id` ';
0206 
0207         /*
0208         if (isset($tags)) {
0209             $sql .= ' JOIN (SELECT DISTINCT project_id FROM stat_project_tagids WHERE tag_id in (' . implode(',', $tags)
0210                 . ')) AS store_tags ON stat_projects.project_id = store_tags.project_id';
0211         }
0212         */
0213         
0214         $sql .= ' WHERE comments.comment_active = 1            
0215             AND stat_projects.status = 100
0216             AND stat_projects.type_id in (1,3)
0217             AND comments.comment_type = 0
0218             AND stat_projects.project_category_id IN (' . implode(',', $activeCategories) . ')                          
0219         ';
0220         
0221         //Store Tag Filter
0222         if (isset($tags)) {
0223             $tagList = $tags;
0224             //build where statement für projects
0225             $sql .= " AND (";
0226 
0227             if(!is_array($tagList)) {
0228                 $tagList = array($tagList);
0229             }
0230             
0231             foreach($tagList as $item) {
0232                 #and
0233                 $sql .= ' find_in_set('.$item.', tag_ids) AND ';
0234             }
0235             $sql .= ' 1=1)';;
0236         }
0237 
0238         $sql .= '  ORDER BY comments.comment_created_at DESC ';
0239 
0240         if (isset($limit)) {
0241             $sql .= ' limit ' . (int)$limit;
0242         }
0243 
0244         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0245 
0246         if (count($resultSet) > 0) {
0247             $cache->save($resultSet, $cacheName, array(), 300);
0248 
0249             return $resultSet;
0250         } else {
0251             $cache->save(array(), $cacheName, array(), 300);
0252 
0253             return array();
0254         }
0255     }
0256 
0257     /**
0258      * @param int $omitCategoryId
0259      *
0260      * @return array
0261      * @TODO: check all occurrences of this function
0262      */
0263     public function getActiveCategoriesForCurrentHost($omitCategoryId = null)
0264     {
0265         $currentHostMainCategories = Zend_Registry::get('store_category_list');
0266 
0267         $modelCategory = new Default_Model_DbTable_ProjectCategory();
0268         $activeChildren = $modelCategory->fetchChildIds($currentHostMainCategories);
0269         $activeCategories = array_unique(array_merge($currentHostMainCategories, $activeChildren));
0270 
0271         if (empty($omitCategoryId)) {
0272             return $activeCategories;
0273         }
0274 
0275         $omitChildren = $modelCategory->fetchChildIds($omitCategoryId);
0276 
0277         return array_diff($activeCategories, $omitChildren);
0278     }
0279 
0280     /**
0281      * @param int      $project_category_id
0282      * @param int|null $omitCategoryId
0283      *
0284      * @return array
0285      */
0286     public function getActiveCategoriesForCatId($project_category_id, $omitCategoryId = null)
0287     {
0288         $modelCategory = new Default_Model_DbTable_ProjectCategory();
0289         $activeChildren = $modelCategory->fetchChildIds($project_category_id);
0290         $activeCategories = array_unique(array_merge(array($project_category_id), $activeChildren));
0291         if (empty($omitCategoryId)) {
0292             return $activeCategories;
0293         }
0294         $omitChildren = $modelCategory->fetchChildIds($omitCategoryId);
0295 
0296         return array_diff($activeCategories, $omitChildren);
0297     }
0298 
0299     /**
0300      * if category id not set the most downloaded products for all categories on the current host wil be returned.
0301      *
0302      * @param int   $limit
0303      * @param null  $project_category_id
0304      * @param array $tags
0305      *
0306      * @return array|false|mixed
0307      * @throws Zend_Cache_Exception
0308      * @throws Zend_Exception
0309      */
0310     public function getMostDownloaded($limit = 100, $project_category_id = null, $tags = null)
0311     {
0312         /** @var Zend_Cache_Core $cache */
0313         $cache = Zend_Registry::get('cache');
0314         $cacheNameTags = is_array($tags) ? implode('_', $tags) : '';
0315         $cacheName =
0316             __FUNCTION__ . '_new_' . md5(Zend_Registry::get('store_host') . (int)$limit . (int)$project_category_id . $cacheNameTags);
0317 
0318         if (($mostDownloaded = $cache->load($cacheName))) {
0319             return $mostDownloaded;
0320         }
0321 
0322         if (empty($project_category_id)) {
0323             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0324         } else {
0325             $activeCategories = $this->getActiveCategoriesForCatId($project_category_id);
0326         }
0327 
0328         if (count($activeCategories) == 0) {
0329             return array();
0330         }
0331 
0332         $sql = '
0333             SELECT 
0334                  `p`.`project_id`
0335                 ,`p`.`title`
0336                 ,`p`.`image_small`       
0337                 ,`s`.`amount` 
0338                 ,`s`.`category_title` 
0339                 ,`p`.`package_types`
0340                 ,`p`.`tag_ids`
0341                 FROM `stat_downloads_quarter_year` `s`
0342                 INNER JOIN `stat_projects` `p` ON `s`.`project_id` = `p`.`project_id`';
0343 
0344         /*
0345         if (isset($tags)) {
0346             $sql .= ' JOIN (SELECT DISTINCT project_id FROM stat_project_tagids WHERE tag_id in (' . implode(',', $tags)
0347                 . ')) AS store_tags ON p.project_id = store_tags.project_id';
0348         }
0349          * 
0350          */
0351 
0352         $sql .= ' WHERE
0353                     p.status=100
0354                     and 
0355                     p.project_category_id IN (' . implode(',', $activeCategories) . ')          
0356             ';
0357         
0358         //Store Tag Filter
0359         if (isset($tags)) {
0360             $tagList = $tags;
0361             //build where statement für projects
0362             $sql .= " AND (";
0363 
0364             if(!is_array($tagList)) {
0365                 $tagList = array($tagList);
0366             }
0367             
0368             foreach($tagList as $item) {
0369                 #and
0370                 $sql .= ' find_in_set('.$item.', tag_ids) AND ';
0371             }
0372             $sql .= ' 1=1)';;
0373         }
0374 
0375         $sql .= '  ORDER BY s.amount DESC ';
0376 
0377         if (isset($limit)) {
0378             $sql .= ' limit ' . (int)$limit;
0379         }
0380 
0381         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0382 
0383         if (count($resultSet) > 0) {
0384             $cache->save($resultSet, $cacheName, array(), 300);
0385 
0386             return $resultSet;
0387         } else {
0388             $cache->save($resultSet, $cacheName, array(), 300);
0389 
0390             return array();
0391         }
0392     }
0393 
0394     /**
0395      * @param int         $limit
0396      * @param int|null    $project_category_id
0397      * @param array|null  $tags
0398      * @param string|null $tag_isoriginal
0399      *
0400      * @return array|false
0401      * @throws Zend_Cache_Exception
0402      * @throws Zend_Exception
0403      */
0404     public function getLastProductsForHostStores($limit = 10, $project_category_id = null, $tags = null, $tag_isoriginal = null)
0405     {
0406         $catids = "";
0407         if ($project_category_id) {
0408             $catids = str_replace(',', '', (string)$project_category_id);
0409         }
0410 
0411         /** @var Zend_Cache_Core $cache */
0412         $cache = Zend_Registry::get('cache');
0413         $cacheNameTags = is_array($tags) ? implode('_', $tags) : '';
0414         $cacheName =
0415             __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$limit . $catids . $cacheNameTags . $tag_isoriginal);
0416 
0417         if (($resultSet = $cache->load($cacheName))) {
0418             return $resultSet;
0419         }
0420 
0421         $activeCategories = array();
0422         if (empty($project_category_id)) {
0423             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0424         } else {
0425             $cats = explode(",", $project_category_id);
0426             if (count($cats) == 1) {
0427                 $activeCategories = $this->getActiveCategoriesForCatId($project_category_id);
0428             } else {
0429                 foreach ($cats as $cat) {
0430                     $tmp = $this->getActiveCategoriesForCatId($cat);
0431                     $activeCategories = array_merge($tmp, $activeCategories);
0432                 }
0433             }
0434         }
0435 
0436         if (count($activeCategories) == 0) {
0437             return array();
0438         }
0439 
0440         $sql = '
0441             SELECT 
0442                 `p`.*              
0443             FROM
0444                 `stat_projects`  AS `p`
0445                 ';
0446 
0447         /*
0448         if (isset($tags)) {
0449             $sql .= ' JOIN (SELECT DISTINCT project_id FROM stat_project_tagids WHERE tag_id in (' . implode(',', $tags)
0450                 . ')) AS store_tags ON p.project_id = store_tags.project_id ';
0451         }
0452          * 
0453          */
0454 
0455         $sql .= '
0456             WHERE
0457                 `p`.`status` = 100                
0458                 AND `p`.`project_category_id` IN (' . implode(',', $activeCategories) . ')
0459                 AND `p`.`amount_reports` IS NULL';
0460         
0461         //Store Tag Filter
0462         if (isset($tags)) {
0463             $tagList = $tags;
0464             //build where statement für projects
0465             $sql .= " AND (";
0466 
0467             if(!is_array($tagList)) {
0468                 $tagList = array($tagList);
0469             }
0470             
0471             foreach($tagList as $item) {
0472                 #and
0473                 $sql .= ' find_in_set('.$item.', tag_ids) AND ';
0474             }
0475             $sql .= ' 1=1)';;
0476         }
0477 
0478         if (isset($tag_isoriginal)) {
0479             if ($tag_isoriginal) {
0480                 $sql .= ' AND find_in_set("' . self::TAG_ISORIGINAL . '", tags)';
0481             } else {
0482                 $sql .= ' AND NOT find_in_set("' . self::TAG_ISORIGINAL . '", tags)';
0483             }
0484         }
0485 
0486         //$sql .= ' ORDER BY IFNULL(p.changed_at,p.created_at)  DESC';
0487         $sql .= ' ORDER BY p.major_updated_at  DESC';
0488 
0489         if (isset($limit)) {
0490             $sql .= ' limit ' . (int)$limit;
0491         }
0492 
0493         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0494         $cache->save($resultSet, $cacheName, array(), 300);
0495 
0496         if (count($resultSet) > 0) {
0497 
0498             return $resultSet;
0499         }
0500 
0501         return array();
0502     }
0503 
0504 
0505     /**
0506      * @param int         $limit
0507      * @param string|null $project_category_id
0508      * @param array|null  $tags
0509      * @param string|null $tag_isoriginal
0510      * @param int         $offset
0511      *
0512      * @return string
0513      * @throws Zend_Cache_Exception
0514      * @throws Zend_Exception
0515      */
0516     public function getJsonLastProductsForHostStores(
0517         $limit = 10,
0518         $project_category_id = null,
0519         $tags = null,
0520         $tag_isoriginal = null,
0521         $offset = 0
0522     ) {
0523         
0524         $store_id = null;
0525         
0526         if (empty($store_id)) {
0527             $store_config = Zend_Registry::get('store_config');
0528             $store_id = $store_config->store_id;
0529             $store_tags = Zend_Registry::isRegistered('config_store_tags') ? Zend_Registry::get('config_store_tags') : array();
0530             
0531             if(empty($tags)) {
0532                $tags = $store_tags; 
0533             } else {
0534                 $tags = array_merge($tags, $store_tags);
0535             }
0536         }
0537         
0538         
0539         
0540         $cat_ids = "";
0541         if ($project_category_id) {
0542             $cat_ids = str_replace(',', '_', (string)$project_category_id);
0543         }
0544 
0545         /** @var Zend_Cache_Core $cache */
0546         $cache = Zend_Registry::get('cache');
0547         $cacheNameTags = is_array($tags) ? implode('_', $tags) : '';
0548         $cacheName =
0549             __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$limit . $cat_ids . $cacheNameTags . $tag_isoriginal
0550                 . $offset);
0551 
0552         if (($resultSet = $cache->load($cacheName))) {
0553             return $resultSet;
0554         }
0555 
0556         $activeCategories = array();
0557         if (empty($project_category_id)) {
0558             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0559         } else {
0560             $cats = explode(",", $project_category_id);
0561             if (count($cats) == 1) {
0562                 $activeCategories = $this->getActiveCategoriesForCatId($project_category_id);
0563             } else {
0564                 foreach ($cats as $cat) {
0565                     $tmp = $this->getActiveCategoriesForCatId($cat);
0566                     $activeCategories = array_merge($tmp, $activeCategories);
0567                 }
0568             }
0569         }
0570 
0571         if (count($activeCategories) == 0) {
0572             return array();
0573         }
0574 
0575         $sql = '
0576             SELECT 
0577                 `project_id`,
0578                 `member_id`,
0579                 `image_small`,
0580                 `title`,
0581                 `version`,
0582                 `cat_title`,
0583                 `count_comments`,
0584                 `package_names`,
0585                 `tag_ids`,
0586                 `laplace_score`,
0587                 `count_likes`,
0588                 `count_dislikes`,
0589                 `changed_at`,
0590                 `created_at`            
0591             FROM
0592                 `stat_projects`  AS `p`';
0593 
0594         /*
0595         if (isset($tags)) {
0596             $sql .= ' JOIN (SELECT DISTINCT project_id FROM stat_project_tagids WHERE tag_id in (' . implode(',', $tags)
0597                 . ')) AS store_tags ON p.project_id = store_tags.project_id';
0598         }
0599          * 
0600          */
0601 
0602 
0603         $sql .= '
0604             WHERE
0605                 `p`.`status` = 100                
0606                 AND `p`.`project_category_id` IN (' . implode(',', $activeCategories) . ')
0607                 AND `p`.`amount_reports` IS NULL';
0608         
0609         //Store Tag Filter
0610         if (isset($tags)) {
0611             $tagList = $tags;
0612             //build where statement für projects
0613             $sql .= " AND (";
0614 
0615             if(!is_array($tagList)) {
0616                 $tagList = array($tagList);
0617             }
0618             
0619             foreach($tagList as $item) {
0620                 #and
0621                 $sql .= ' find_in_set('.$item.', tag_ids) AND ';
0622             }
0623             $sql .= ' 1=1)';;
0624         }
0625 
0626         if (isset($tag_isoriginal)) {
0627             if ($tag_isoriginal) {
0628                 $sql .= ' AND find_in_set("' . self::TAG_ISORIGINAL . '", tags)';
0629             } else {
0630                 $sql .= ' AND NOT find_in_set("' . self::TAG_ISORIGINAL . '", tags)';
0631             }
0632         }
0633 
0634         $sql .= ' ORDER BY IFNULL(p.changed_at,p.created_at)  DESC';
0635 
0636         if (isset($limit)) {
0637             $sql .= ' limit ' . (int)$limit;
0638         }
0639         if (isset($offset)) {
0640             $sql .= ' offset ' . (int)$offset;
0641         }
0642         
0643         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0644         $imagehelper = new Default_View_Helper_Image();
0645         foreach ($resultSet as &$value) {
0646             $value['image_small'] = $imagehelper->Image($value['image_small'], array('width' => 200, 'height' => 200));
0647         }
0648         if (count($resultSet) > 0) {
0649             $result = Zend_Json::encode($resultSet);
0650             $cache->save($result, $cacheName, array(), 300);
0651 
0652             return $result;
0653         }
0654 
0655         return Zend_Json::encode('');
0656     }
0657 
0658     /**
0659      * @param int         $limit
0660      * @param string|null $project_category_id
0661      * @param array|null  $tags
0662      *
0663      * @return array|false
0664      * @throws Zend_Cache_Exception
0665      * @throws Zend_Exception
0666      */
0667     public function getTopProductsForHostStores($limit = 10, $project_category_id = null, $tags = null)
0668     {
0669         $catids = "";
0670         if ($project_category_id) {
0671             $catids = str_replace(',', '', (string)$project_category_id);
0672         }
0673 
0674         /** @var Zend_Cache_Core $cache */
0675         $cache = Zend_Registry::get('cache');
0676         $cacheNameTags = is_array($tags) ? implode('_', $tags) : '';
0677         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$limit . $catids . $cacheNameTags);
0678 
0679         if (($resultSet = $cache->load($cacheName))) {
0680             return $resultSet;
0681         }
0682 
0683         $activeCategories = array();
0684         if (empty($project_category_id)) {
0685             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0686         } else {
0687             $cats = explode(",", $project_category_id);
0688             if (count($cats) == 1) {
0689                 $activeCategories = $this->getActiveCategoriesForCatId($project_category_id);
0690             } else {
0691                 foreach ($cats as $cat) {
0692                     $tmp = $this->getActiveCategoriesForCatId($cat);
0693                     $activeCategories = array_merge($tmp, $activeCategories);
0694                 }
0695             }
0696         }
0697 
0698         if (count($activeCategories) == 0) {
0699             return array();
0700         }
0701 
0702         $sql = '
0703             SELECT 
0704                 `p`.*              
0705             FROM
0706                 `stat_projects`  AS `p`';
0707 
0708         /*
0709         if (isset($tags)) {
0710             $sql .= ' JOIN (SELECT DISTINCT project_id FROM stat_project_tagids WHERE tag_id in (' . implode(',', $tags)
0711                 . ')) AS store_tags ON p.project_id = store_tags.project_id';
0712         }
0713          * 
0714          */
0715 
0716         $sql .= '
0717             WHERE
0718                 `p`.`status` = 100                
0719                 AND `p`.`project_category_id` IN (' . implode(',', $activeCategories) . ')
0720                 AND `p`.`amount_reports` IS NULL';
0721 
0722         //Store Tag Filter
0723         if (isset($tags)) {
0724             $tagList = $tags;
0725             //build where statement für projects
0726             $sql .= " AND (";
0727 
0728             if(!is_array($tagList)) {
0729                 $tagList = array($tagList);
0730             }
0731             
0732             foreach($tagList as $item) {
0733                 #and
0734                 $sql .= ' find_in_set('.$item.', tag_ids) AND ';
0735             }
0736             $sql .= ' 1=1)';;
0737         }
0738         
0739         /*$sql .= ' ORDER BY (round(((count_likes + 6) / ((count_likes + count_dislikes) + 12)),2) * 100) DESC, created_at DESC
0740             ';*/ 
0741         $sql .= ' ORDER BY laplace_score DESC, created_at DESC
0742             ';
0743         if (isset($limit)) {
0744             $sql .= ' limit ' . (int)$limit;
0745         }
0746 
0747         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0748         $cache->save($resultSet, $cacheName, array(), 300);
0749 
0750         if (count($resultSet) > 0) {
0751             return $resultSet;
0752         }
0753 
0754         return array();
0755     }
0756 
0757     public function getRandProduct()
0758     {
0759         $pid = $this->getRandomStoreProjectIds();
0760         $project_id = $pid['project_id'];
0761 
0762         $sql="SELECT 
0763                 p.project_id
0764                 ,p.title
0765                 ,p.description  
0766                 ,p.image_small
0767                 ,p.count_comments   
0768                 ,p.changed_at
0769                 ,pr.likes as count_likes
0770                 ,pr.dislikes as count_dislikes
0771                 ,IFNULL(pr.score_with_pling, 500) AS laplace_score
0772                 ,m.profile_image_url
0773                 ,m.username                               
0774                 FROM
0775                 project as p            
0776                 JOIN member AS m ON m.member_id = p.member_id
0777                 LEFT join  stat_rating_project AS pr  ON p.project_id = pr.project_id
0778                 WHERE
0779                 p.project_id = :project_id";
0780         /*$sql = '
0781             SELECT 
0782                 `p`.*
0783                 ,laplace_score(`p`.`count_likes`, `p`.`count_dislikes`) AS `laplace_score`
0784                 ,`m`.`profile_image_url`
0785                 ,`m`.`username`
0786             FROM
0787                 `project` AS `p`
0788             JOIN 
0789                 `member` AS `m` ON `m`.`member_id` = `p`.`member_id`
0790             WHERE
0791                `p`.`project_id` = :project_id
0792             ';*/
0793         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql, array('project_id' => $project_id));
0794         return $resultSet;
0795       
0796     }
0797 
0798     public function getRandPlingedProduct()
0799     {
0800         $pid = $this->getRandomPlingedProjectIds();
0801         $project_id = $pid['project_id'];
0802 
0803         $sql = "SELECT 
0804                 p.project_id
0805                 ,p.title
0806                 ,p.description  
0807                 ,p.image_small
0808                 ,p.count_comments   
0809                 ,p.changed_at
0810                 ,pr.likes as count_likes
0811                 ,pr.dislikes as count_dislikes
0812                 ,IFNULL(pr.score_with_pling, 500) AS laplace_score
0813                 ,m.profile_image_url
0814                 ,m.username                               
0815                 ,(select count(1) from project_plings pp where pp.project_id = p.project_id and pp.is_deleted = 0) as sum_plings
0816                 FROM
0817                 project as p            
0818                 JOIN member AS m ON m.member_id = p.member_id
0819                 LEFT join  stat_rating_project AS pr  ON p.project_id = pr.project_id
0820                 WHERE
0821                 p.project_id = :project_id";
0822         /*$sql = '
0823             SELECT 
0824                 `p`.*
0825                 ,laplace_score(`p`.`count_likes`, `p`.`count_dislikes`) AS `laplace_score`
0826                 ,`m`.`profile_image_url`
0827                 ,`m`.`username`
0828                 ,(select count(1) from project_plings pp where pp.project_id = p.project_id and pp.is_deleted = 0) as sum_plings
0829             FROM
0830                 `project` AS `p`
0831             JOIN 
0832                 `member` AS `m` ON `m`.`member_id` = `p`.`member_id`
0833             WHERE
0834                `p`.`project_id` = :project_id
0835             ';*/
0836         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql, array('project_id' => $project_id));
0837         return $resultSet;
0838         /*$resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql, array('project_id' => $project_id));
0839         if (count($resultSet) > 0) {
0840             return new Zend_Paginator(new Zend_Paginator_Adapter_Array($resultSet));
0841         }
0842 
0843         return new Zend_Paginator(new Zend_Paginator_Adapter_Array(array()));*/
0844     }
0845 
0846     public function getRandFeaturedProduct()
0847     {
0848         $pid = $this->getRandomFeaturedProjectIds();
0849         $project_id = $pid['project_id'];
0850 
0851         $sql="SELECT 
0852                 p.project_id
0853                 ,p.title
0854                 ,p.description  
0855                 ,p.image_small
0856                 ,p.count_comments   
0857                 ,p.changed_at
0858                 ,pr.likes as count_likes
0859                 ,pr.dislikes as count_dislikes
0860                 ,IFNULL(pr.score_with_pling, 500) AS laplace_score
0861                 ,m.profile_image_url
0862                 ,m.username                               
0863                 FROM
0864                 project as p            
0865                 JOIN member AS m ON m.member_id = p.member_id
0866                 LEFT join  stat_rating_project AS pr  ON p.project_id = pr.project_id
0867                 WHERE
0868                 p.project_id = :project_id";
0869         /*$sql = '
0870             SELECT 
0871                 `p`.project_id                
0872                 ,IFNULL(`pr`.`score_with_pling`, 500) AS `laplace_score`
0873                 ,`m`.`profile_image_url`
0874                 ,`m`.`username`               
0875             FROM
0876                 `project` AS `p`            
0877             JOIN 
0878                 `member` AS `m` ON `m`.`member_id` = `p`.`member_id`
0879             LEFT join  `stat_rating_project` AS `pr`  ON `p`.`project_id` = `pr`.`project_id`     
0880             WHERE
0881                `p`.`project_id` = :project_id
0882             ';*/
0883         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql, array('project_id' => $project_id));
0884         return $resultSet;
0885     }
0886 
0887     public function getRandomStoreProjectIds()
0888     {
0889         /** @var Zend_Cache_Core $cache */
0890         $cache = Zend_Registry::get('cache');
0891         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host'));
0892 
0893         $resultSet = $cache->load($cacheName);
0894 
0895         if (false == $resultSet) {
0896             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0897             if (count($activeCategories) == 0) {
0898                 return array();
0899             }
0900             $sql = '
0901                     SELECT 
0902                         `p`.`project_id`                   
0903                     FROM
0904                         `project` AS `p`                
0905                     WHERE
0906                         `p`.`status` = 100
0907                         AND `p`.`type_id` = 1                    
0908                         AND `p`.`project_category_id` IN (' . implode(',', $activeCategories) . ')                    
0909                     ';
0910             $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0911             $cache->save($resultSet, $cacheName, array(), 3600 * 24);
0912         }
0913 
0914         $irandom = rand(0, sizeof($resultSet)-1);
0915 
0916         return $resultSet[$irandom];
0917     }
0918 
0919     public function getRandomPlingedProjectIds()
0920     {
0921         /** @var Zend_Cache_Core $cache */
0922         $cache = Zend_Registry::get('cache');
0923         $cacheName = __FUNCTION__ ;
0924 
0925         $resultSet = $cache->load($cacheName);
0926 
0927         if (false == $resultSet) {
0928             $sql="      select    
0929                             p.project_id
0930                         from project_plings pl
0931                         inner join stat_projects p on pl.project_id = p.project_id            
0932                         where pl.is_deleted = 0 and pl.is_active = 1 ";
0933             $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0934             $cache->save($resultSet, $cacheName, array(), 3600 * 24); //cache is cleaned once a day
0935         }
0936 
0937         $irandom = rand(0, sizeof($resultSet)-1);
0938 
0939         return $resultSet[$irandom];
0940     }
0941 
0942 
0943     public function getRandomFeaturedProjectIds()
0944     {
0945         /** @var Zend_Cache_Core $cache */
0946         $cache = Zend_Registry::get('cache');
0947         $cacheName = __FUNCTION__ ;
0948 
0949         $resultSet = $cache->load($cacheName);
0950 
0951         if (false == $resultSet) {
0952             $sql="select project_id from  project p where p.status = 100 and p.featured = 1 ";
0953             $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
0954             $cache->save($resultSet, $cacheName, array(), 3600 * 24); //cache is cleaned once a day
0955         }
0956 
0957         $irandom = rand(0, sizeof($resultSet)-1);
0958 
0959         return $resultSet[$irandom];
0960     }
0961 
0962  
0963     /**
0964      * @param int  $limit
0965      * @param null $project_category_id
0966      *
0967      * @return array|Zend_Paginator
0968      */
0969     public function getFeaturedProductsForHostStores($limit = 10, $project_category_id = null)
0970     {
0971         /** @var Zend_Cache_Core $cache */
0972         $cache = Zend_Registry::get('cache');
0973         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$limit . (int)$project_category_id);
0974 
0975         if (false !== ($resultSet = $cache->load($cacheName))) {
0976 
0977             return new Zend_Paginator(new Zend_Paginator_Adapter_Array($resultSet));
0978         }
0979 
0980         if (empty($project_category_id)) {
0981             $activeCategories = $this->getActiveCategoriesForCurrentHost();
0982         } else {
0983             $activeCategories = $this->getActiveCategoriesForCatId($project_category_id);
0984         }
0985 
0986         if (count($activeCategories) == 0) {
0987             return array();
0988         }
0989 
0990         $sql = '
0991             SELECT 
0992                 `p`.*              
0993                 ,`m`.`profile_image_url`
0994                 ,`m`.`username`
0995             FROM
0996                 `stat_projects` AS `p`
0997             JOIN 
0998                 `member` AS `m` ON `m`.`member_id` = `p`.`member_id`
0999             WHERE
1000                 `p`.`status` = 100
1001                 AND `p`.`type_id` = 1
1002                 AND `p`.`featured` = 1
1003                 AND `p`.`project_category_id` IN (' . implode(',', $activeCategories) . ')                
1004             ';
1005         if (isset($limit)) {
1006             $sql .= ' limit ' . (int)$limit;
1007         }
1008 
1009         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql);
1010         $cache->save($resultSet, $cacheName, array(), 60);
1011 
1012         if (count($resultSet) > 0) {
1013             return new Zend_Paginator(new Zend_Paginator_Adapter_Array($resultSet));
1014         } else {
1015             return new Zend_Paginator(new Zend_Paginator_Adapter_Array(array()));
1016         }
1017     }
1018 
1019     public function getLastCommentsForUsersProjects($member_id, $limit = 10, $comment_type=0)
1020     {
1021         /** @var Zend_Cache_Core $cache */
1022         $cache = Zend_Registry::get('cache');
1023         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$member_id . (int)$limit).$comment_type;
1024 
1025         if (false !== ($resultSet = $cache->load($cacheName))) {
1026             return $resultSet;
1027         }
1028        
1029 
1030         $sql = '
1031                 SELECT 
1032                 comment_id
1033                 ,comment_text
1034                 , member.member_id
1035                 ,member.profile_image_url
1036                 ,comment_created_at
1037                 ,username            
1038                 ,title
1039                 ,project_id
1040                 ,comments.comment_target_id
1041 
1042                 FROM comments           
1043                 JOIN project ON comments.comment_target_id = project.project_id 
1044                 STRAIGHT_JOIN member ON comments.comment_member_id = member.member_id
1045                 WHERE comments.comment_active = 1
1046                 AND project.status = 100
1047                 and comments.comment_type=:comment_type
1048                 AND project.member_id =:member_id
1049                 and comments.comment_member_id <>:member_id
1050                 ORDER BY comments.comment_created_at DESC               
1051         ';
1052 
1053         if (isset($limit)) {
1054             $sql .= ' limit ' . (int)$limit;
1055         }
1056 
1057         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql, array('member_id' => $member_id, 'comment_type'=>$comment_type));
1058 
1059         if (count($resultSet) > 0) {
1060             $cache->save($resultSet, $cacheName, array(), 300);
1061 
1062             return $resultSet;
1063         } else {
1064             $cache->save(array(), $cacheName, array(), 300);
1065 
1066             return array();
1067         }
1068     }
1069     public function getFeaturedProductsForUser($member_id,$limit = 10)
1070     {
1071         /** @var Zend_Cache_Core $cache */
1072         $cache = Zend_Registry::get('cache');
1073         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$member_id . (int)$limit);
1074 
1075         if (false !== ($resultSet = $cache->load($cacheName))) {
1076             return $resultSet;
1077         }
1078         
1079         $sql="SELECT 
1080                 p.project_id
1081                 ,p.title
1082                 ,p.description  
1083                 ,p.image_small
1084                 ,p.count_comments   
1085                 ,p.changed_at                
1086                 ,p.laplace_score
1087                 ,p.profile_image_url
1088                 ,p.username                               
1089                 FROM
1090                 stat_projects as p                                           
1091                 WHERE
1092                 p.status = 100 and p.featured = 1  and p.member_id = 24 
1093         ";
1094 
1095         if (isset($limit)) {
1096             $sql .= ' limit ' . (int)$limit;
1097         }
1098 
1099         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('threshold' => Default_Model_Spam::SPAM_THRESHOLD, 'member_id' => $member_id));
1100 
1101         if ($result->rowCount() > 0) {            
1102             $resultSet = $result->fetchAll();        
1103         } else {
1104             $resultSet = array();
1105         }
1106         $cache->save($resultSet, $cacheName, array(), 300);
1107         return $resultSet;
1108     }
1109 
1110     public function getLastVotesForUsersProjects($member_id, $limit = 10)
1111     {
1112         /** @var Zend_Cache_Core $cache */
1113         $cache = Zend_Registry::get('cache');
1114         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$member_id . (int)$limit);
1115 
1116         if (false !== ($resultSet = $cache->load($cacheName))) {
1117             return $resultSet;
1118         }
1119 
1120         $sql = '
1121                 SELECT 
1122                 `rating_id`                
1123                 ,`member`.`member_id`   
1124                 ,`member`.`profile_image_url`                           
1125                 ,`username`            
1126                 ,`user_like`
1127                 ,`user_dislike`
1128                 ,`score`
1129                 ,`project_rating`.`project_id`
1130                 ,`project_rating`.`created_at`
1131                 ,`project`.`title`
1132                 ,`comments`.`comment_text`
1133                 ,`comments`.`comment_id`
1134                 FROM `project_rating`           
1135                 JOIN `project` ON `project_rating`.`project_id` = `project`.`project_id` 
1136                 join `comments` on `project_rating`.`comment_id` = `comments`.`comment_id`   
1137                 STRAIGHT_JOIN `member` ON `project_rating`.`member_id` = `member`.`member_id`
1138                 WHERE `project`.`status` = 100 and `project_rating`.`rating_active`=1
1139                 AND `project`.`member_id` = :member_id
1140                 ORDER BY `rating_id` DESC               
1141         ';
1142 
1143         if (isset($limit)) {
1144             $sql .= ' limit ' . (int)$limit;
1145         }
1146 
1147         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql, array('member_id' => $member_id));
1148 
1149         if (count($resultSet) > 0) {
1150             $cache->save($resultSet, $cacheName, array(), 300);
1151 
1152             return $resultSet;
1153         } else {
1154             $cache->save(array(), $cacheName, array(), 300);
1155 
1156             return array();
1157         }
1158     }
1159 
1160      public function getLastSpamProjects($member_id, $limit = 10)
1161     {
1162         /** @var Zend_Cache_Core $cache */
1163         $cache = Zend_Registry::get('cache');
1164         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$member_id . (int)$limit);
1165 
1166         if (false !== ($resultSet = $cache->load($cacheName))) {
1167             return $resultSet;
1168         }
1169 
1170         $sql = "
1171             SELECT *
1172             FROM `stat_projects`
1173             WHERE `stat_projects`.`amount_reports` >= :threshold AND `stat_projects`.`status` = 100
1174             AND  `stat_projects`.`member_id` = :member_id
1175             ORDER BY `stat_projects`.`changed_at` DESC, `stat_projects`.`created_at` DESC, `stat_projects`.`amount_reports` DESC
1176         ";
1177 
1178         if (isset($limit)) {
1179             $sql .= ' limit ' . (int)$limit;
1180         }
1181 
1182         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('threshold' => Default_Model_Spam::SPAM_THRESHOLD, 'member_id' => $member_id));
1183 
1184         if ($result->rowCount() > 0) {            
1185             $resultSet = $result->fetchAll();        
1186         } else {
1187             $resultSet = array();
1188         }
1189         $cache->save($resultSet, $cacheName, array(), 300);
1190         return $resultSet;
1191         
1192     }
1193 
1194     public function getLastDonationsForUsersProjects($member_id, $limit = 10)
1195     {
1196         /** @var Zend_Cache_Core $cache */
1197         $cache = Zend_Registry::get('cache');
1198         $cacheName = __FUNCTION__ . '_' . md5(Zend_Registry::get('store_host') . (int)$member_id . (int)$limit);
1199 
1200         if (false !== ($resultSet = $cache->load($cacheName))) {
1201             return $resultSet;
1202         }
1203 
1204         $sql = '
1205          SELECT 
1206                 plings.project_id,
1207                 plings.id 
1208                 ,member.member_id
1209                 ,profile_image_url
1210                 ,plings.create_time
1211                 ,username
1212                 ,plings.amount
1213                 ,comment
1214                 ,project.title
1215                 FROM plings
1216                 JOIN project ON project.project_id = plings.project_id   
1217                 STRAIGHT_JOIN member ON plings.member_id = member.member_id     
1218                 WHERE 
1219                 plings.status_id = 2
1220                 AND project.status=100
1221                 AND project.member_id = :member_id
1222                 ORDER BY create_time DESC
1223         ';
1224 
1225         if (isset($limit)) {
1226             $sql .= ' limit ' . (int)$limit;
1227         }
1228 
1229         $resultSet = Zend_Db_Table::getDefaultAdapter()->fetchAll($sql, array('member_id' => $member_id));
1230 
1231         if (count($resultSet) > 0) {
1232             $cache->save($resultSet, $cacheName, array(), 300);
1233 
1234             return $resultSet;
1235         } else {
1236             $cache->save(array(), $cacheName, array(), 300);
1237 
1238             return array();
1239         }
1240     }
1241 
1242     /**
1243      * @param int $limit
1244      *
1245      * @return array|false|mixed
1246      */
1247     public function getNewActiveMembers($limit = 20)
1248     {
1249         /** @var Zend_Cache_Core $cache */
1250         $cache = Zend_Registry::get('cache');
1251         $cacheName = __FUNCTION__ . '_' . md5((int)$limit);
1252 
1253         if (false !== ($newMembers = $cache->load($cacheName))) {
1254             return $newMembers;
1255         }
1256 
1257         $sql = '
1258                 SELECT 
1259                 member_id,
1260                 profile_image_url,
1261                 username,
1262                 created_at
1263                 FROM member
1264                 WHERE `is_active` = :activeVal
1265                 AND `type` = :typeVal     
1266                 ORDER BY created_at DESC             
1267             ';
1268 
1269         if (isset($limit)) {
1270             $sql .= ' limit ' . (int)$limit;
1271         }
1272 
1273         $resultMembers = Zend_Db_Table::getDefaultAdapter()->query($sql, array(
1274             'activeVal' => Default_Model_Member::MEMBER_ACTIVE,
1275             'typeVal'   => Default_Model_Member::MEMBER_TYPE_PERSON
1276         ))->fetchAll()
1277         ;
1278 
1279         $cache->save($resultMembers, $cacheName, array(), 300);
1280 
1281         return $resultMembers;
1282     }
1283 
1284 
1285    
1286     public function getSupporters($limit = 20)
1287     {
1288         /** @var Zend_Cache_Core $cache */
1289         $cache = Zend_Registry::get('cache');
1290         $cacheName = __FUNCTION__ . '_' . md5((int)$limit);
1291 
1292         if (false !== ($newSupporters = $cache->load($cacheName))) {
1293             return $newSupporters;
1294         }
1295         $sql = '
1296                 select  
1297                 s.member_id as supporter_id
1298                 ,s.member_id
1299                 ,(select username from member m where m.member_id = s.member_id) as username
1300                 ,(select profile_image_url from member m where m.member_id = s.member_id) as profile_image_url
1301                 ,max(s.active_time_max) as created_at
1302                 from v_support s
1303                 group by member_id
1304                 order by active_time_max desc                                       
1305         ';
1306         if (isset($limit)) {
1307             $sql .= ' limit ' . (int)$limit;
1308         }
1309         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1310         $cache->save($result, $cacheName, array(), 300);
1311 
1312         return $result;
1313     }
1314 
1315     public function getNewActiveSupporters($limit = 20)
1316     {
1317         /** @var Zend_Cache_Core $cache */
1318         $cache = Zend_Registry::get('cache');
1319         $cacheName = __FUNCTION__ . '_' . md5((int)$limit);
1320 
1321         if (false !== ($newSupporters = $cache->load($cacheName))) {
1322             return $newSupporters;
1323         }
1324         /*$sql = '
1325                         SELECT 
1326                         s.member_id as supporter_id
1327                         ,s.member_id
1328                         ,(select username from member m where m.member_id = s.member_id) as username
1329                         ,(select profile_image_url from member m where m.member_id = s.member_id) as profile_image_url
1330                         ,min(s.active_time) as created_at
1331                         from support s 
1332                         where s.status_id = 2  
1333                         and (DATE_ADD((s.active_time), INTERVAL 1 YEAR) > now())
1334                         group by member_id
1335                         order by s.active_time desc                                       
1336         ';*/
1337         $sql = '
1338                 select  
1339                 s.member_id as supporter_id
1340                 ,s.member_id
1341                 ,(select username from member m where m.member_id = s.member_id) as username
1342                 ,(select profile_image_url from member m where m.member_id = s.member_id) as profile_image_url
1343                 ,max(s.active_time_max) as created_at
1344                 from v_support s
1345                 group by member_id
1346                 order by active_time_max desc                                       
1347         ';
1348         if (isset($limit)) {
1349             $sql .= ' limit ' . (int)$limit;
1350         }
1351         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1352         $cache->save($result, $cacheName, array(), 300);
1353 
1354         return $result;
1355     }
1356     
1357 
1358     public function getNewActiveSupportersForSectionAll($limit = 20)
1359     {
1360         /** @var Zend_Cache_Core $cache */
1361         $cache = Zend_Registry::get('cache');
1362         $cacheName = __FUNCTION__ . '_' . md5((int)$limit);
1363 
1364         if (false !== ($newSupporters = $cache->load($cacheName))) {
1365             return $newSupporters;
1366         }
1367         
1368         $sql = '
1369                 SELECT  s.*,
1370                 s.member_id as supporter_id
1371                 ,s.member_id
1372                 ,(select username from member m where m.member_id = s.member_id) as username
1373                 ,(select profile_image_url from member m where m.member_id = s.member_id) as profile_image_url
1374                 ,MAX(s.active_time) AS active_time_max
1375                 ,ss.tier AS section_support_tier
1376                 from section_support_paypements ss
1377                 JOIN support s ON s.id = ss.support_id
1378                 WHERE ss.yearmonth = DATE_FORMAT(NOW(), "%Y%m")
1379                 GROUP BY s.member_id,ss.tier
1380                 order BY active_time_max desc                                       
1381         ';
1382         if (isset($limit)) {
1383             $sql .= ' limit ' . (int)$limit;
1384         }
1385         $result = Zend_Db_Table::getDefaultAdapter()->query($sql)->fetchAll();
1386         $cache->save($result, $cacheName, array(), 300);
1387 
1388         return $result;
1389     }
1390 
1391     public function getSupporterActiveMonths($member_id)
1392     {
1393         $cache = Zend_Registry::get('cache');
1394         $cacheName = __FUNCTION__ . '_' . $member_id ;
1395         if (false !== ($newSupporters = $cache->load($cacheName))) {
1396             return $newSupporters;
1397         }
1398         $sql = "select 
1399                 count(distinct yearmonth) active_months
1400                 from section_support_paypements ss
1401                 JOIN support s ON s.id = ss.support_id
1402                 where s.member_id = :member_id              
1403                 ";
1404         $result = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql,array('member_id' => $member_id));        
1405         $cache->save($result, $cacheName, array(), 300);
1406         return $result;
1407     }
1408 
1409     public function getSectionSupportersActiveMonths($section_id)
1410     {
1411         /** @var Zend_Cache_Core $cache */
1412         $cache = Zend_Registry::get('cache');
1413         $cacheName = __FUNCTION__ . '_' . $section_id ;
1414         if (false !== ($newSupporters = $cache->load($cacheName))) {
1415             return $newSupporters;
1416         }
1417         $sql = "SELECT COUNT(1) AS active_months, member_id,sum(tier) sum_support FROM
1418                 (
1419                 SELECT s.member_id, p.yearmonth , sum(p.tier) tier FROM section_support_paypements p
1420                 JOIN support s ON s.id = p.support_id
1421                 WHERE p.section_id = :section_id
1422                 GROUP BY s.member_id, p.yearmonth
1423                 ) A
1424                 GROUP BY member_id
1425                 ";
1426 
1427         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' => $section_id))->fetchAll();
1428         $cache->save($result, $cacheName, array(), 300);
1429         return $result;
1430     }
1431 
1432     public function getNewActiveSupportersForSectionUnique($section_id, $limit = 1000)
1433     {
1434         /** @var Zend_Cache_Core $cache */
1435         $cache = Zend_Registry::get('cache');
1436         $cacheName = __FUNCTION__ . '_' . $section_id . '_' . md5((int)$limit);
1437 
1438         if (false !== ($newSupporters = $cache->load($cacheName))) {
1439             return $newSupporters;
1440         }
1441         
1442         $sql = '
1443                 SELECT  
1444                 s.member_id
1445                 ,m.username
1446                 ,m.profile_image_url
1447                 ,sum(ss.tier) AS sum_support
1448                 from section_support_paypements ss
1449                 JOIN support s ON s.id = ss.support_id
1450                 join member m on m.member_id = s.member_id
1451                 WHERE ss.section_id = :section_id
1452                 AND ss.yearmonth = DATE_FORMAT(NOW(), "%Y%m")
1453                 GROUP BY s.member_id
1454                 order BY ss.tier DESC                                  
1455         ';
1456         if (isset($limit)) {
1457             $sql .= ' limit ' . (int)$limit;
1458         }
1459         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' => $section_id))->fetchAll();
1460         $cache->save($result, $cacheName, array(), 300);
1461 
1462         return $result;
1463     }
1464 
1465     public function getRandomSupporterForSection($section_id)
1466     {
1467         /** @var Zend_Cache_Core $cache */
1468         $cache = Zend_Registry::get('cache');
1469         $cacheName = __FUNCTION__ . '_' . $section_id;
1470 
1471         $supporters = $cache->load($cacheName);
1472         if (!$supporters) {
1473             $sql = '
1474                     select section_id, member_id, weight 
1475                     from v_supporter_view_queue 
1476                     where section_id = :section_id
1477                     order by weight desc                        
1478             ';        
1479             $supporters = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' => $section_id))->fetchAll();      
1480             
1481             //If there is no real supporter, show pling user
1482             if(!$supporters || count($supporters) == 0) {
1483                 $sql = '
1484                     select section_id, member_id, weight 
1485                     from v_supporter_view_queue_all 
1486                     where section_id = :section_id
1487                     order by weight desc                        
1488                 ';        
1489                 $supporters = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' => $section_id))->fetchAll(); 
1490             }
1491             
1492             $cache->save($supporters, $cacheName, array(), 300);
1493         }
1494             
1495         $sumWeight =0;
1496         foreach ($supporters as $s) {
1497             $sumWeight=$sumWeight+$s['weight'];
1498         }
1499         // select Random [1.. sumWeight];
1500         $randomWeight = rand(1,$sumWeight);
1501         $sumWeight =0;
1502         $member_id=null;
1503         foreach ($supporters as $s) {
1504            $sumWeight=$sumWeight+$s['weight'];
1505            if($sumWeight >= $randomWeight)
1506            {
1507                 $member_id = $s['member_id'];
1508                 break;
1509            }
1510         }
1511         if($member_id)
1512         {
1513             $sql = "select member_id,username,profile_image_url from member where member_id=:member_id";
1514             
1515             $result = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql,array('member_id' => $member_id));
1516             return $result;
1517         }
1518 
1519 
1520         return null;
1521     }
1522    
1523 
1524     public function getNewActiveSupportersForSection($section_id, $limit = 20)
1525     {
1526         /** @var Zend_Cache_Core $cache */
1527         $cache = Zend_Registry::get('cache');
1528         $cacheName = __FUNCTION__ . '_' . $section_id . '_' . md5((int)$limit);
1529 
1530         if (false !== ($newSupporters = $cache->load($cacheName))) {
1531             return $newSupporters;
1532         }
1533         
1534         $sql = '
1535                 SELECT  s.*,
1536                 s.member_id as supporter_id
1537                 ,s.member_id
1538                 ,(select username from member m where m.member_id = s.member_id) as username
1539                 ,(select profile_image_url from member m where m.member_id = s.member_id) as profile_image_url
1540                 ,MAX(s.active_time) AS active_time_max
1541                 ,ss.tier AS section_support_tier
1542                 from section_support_paypements ss
1543                 JOIN support s ON s.id = ss.support_id
1544                 WHERE ss.section_id = :section_id
1545                 AND ss.yearmonth = DATE_FORMAT(NOW(), "%Y%m")
1546                 GROUP BY s.member_id,ss.tier
1547                 order BY  active_time_max desc                                       
1548         ';
1549         if (isset($limit)) {
1550             $sql .= ' limit ' . (int)$limit;
1551         }
1552         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' => $section_id))->fetchAll();
1553         $cache->save($result, $cacheName, array(), 300);
1554 
1555         return $result;
1556     }
1557     
1558     
1559     public function getNewActiveSupportersForSectionAndMonth($section_id, $yearmonth, $limit = 100)
1560     {
1561         /** @var Zend_Cache_Core $cache */
1562         $cache = Zend_Registry::get('cache');
1563         $cacheName = __FUNCTION__ . '_' . $section_id . '_' . $yearmonth . '_' . md5((int)$limit);
1564 
1565         if (false !== ($newSupporters = $cache->load($cacheName))) {
1566             return $newSupporters;
1567         }
1568         
1569         $sql = '
1570                 SELECT  s.*,
1571                 s.member_id as supporter_id
1572                 ,s.member_id
1573                 ,(select username from member m where m.member_id = s.member_id) as username
1574                 ,(select profile_image_url from member m where m.member_id = s.member_id) as profile_image_url
1575                 ,MAX(s.active_time) AS active_time_max
1576                 ,SUM(ss.tier) AS sum_tier
1577                 from section_support_paypements ss
1578                 JOIN support s ON s.id = ss.support_id
1579                 WHERE ss.section_id = :section_id
1580                 AND ss.yearmonth = :yearmonth
1581                 GROUP BY s.member_id
1582                 order BY SUM(ss.tier) DESC, active_time_max desc                                       
1583         ';
1584         if (isset($limit)) {
1585             $sql .= ' limit ' . (int)$limit;
1586         }
1587         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' => $section_id, 'yearmonth' => $yearmonth))->fetchAll();
1588         $cache->save($result, $cacheName, array(), 300);
1589 
1590         return $result;
1591     }
1592 
1593     public function getNewActivePlingProduct($limit = 20)
1594     {
1595         /** @var Zend_Cache_Core $cache */
1596         $cache = Zend_Registry::get('cache');
1597         $cacheName = __FUNCTION__ . '_' . md5((int)$limit);
1598 
1599         if (false !== ($newSupporters = $cache->load($cacheName))) {
1600             return $newSupporters;
1601         }
1602 
1603         $sql = '  
1604                         select 
1605                         pl.member_id as pling_member_id
1606                         ,pl.project_id                        
1607                         ,p.title
1608                         ,p.image_small
1609                         ,p.laplace_score
1610                         ,p.count_likes
1611                         ,p.count_dislikes   
1612                         ,p.member_id 
1613                         ,p.profile_image_url
1614                         ,p.username
1615                         ,p.cat_title as catTitle
1616                         ,(
1617                             select max(created_at) from project_plings pt where pt.member_id = pl.member_id and pt.project_id=pl.project_id
1618                         ) as created_at
1619                         ,(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
1620                         from project_plings pl
1621                         inner join stat_projects p on pl.project_id = p.project_id and p.status=100                   
1622                         where pl.is_deleted = 0 and pl.is_active = 1 
1623                         order by created_at desc                                                  
1624         ';
1625         if (isset($limit)) {
1626             $sql .= ' limit ' . (int)$limit;
1627         }
1628         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1629 
1630         $cache->save($result, $cacheName, array(), 300);
1631 
1632         return $result;
1633     }
1634 
1635 
1636     public function getJsonNewActivePlingProduct($limit = 20,$offset=null)
1637     {
1638         /** @var Zend_Cache_Core $cache */
1639         $cache = Zend_Registry::get('cache');
1640         $cacheName = __FUNCTION__ . '_' . md5((int)$limit).md5((int)$offset);;
1641 
1642         if (false !== ($newSupporters = $cache->load($cacheName))) {
1643             return $newSupporters;
1644         }
1645 
1646         $sql = '  
1647                         select 
1648                         pl.member_id as pling_member_id
1649                         ,pl.project_id                        
1650                         ,p.title
1651                         ,p.image_small
1652                         ,p.laplace_score
1653                         ,p.count_likes
1654                         ,p.count_dislikes   
1655                         ,p.member_id 
1656                         ,p.description
1657                         ,p.profile_image_url
1658                         ,p.username
1659                         ,p.cat_title 
1660                         ,p.count_comments
1661                         ,(
1662                             select max(created_at) from project_plings pt where pt.member_id = pl.member_id and pt.project_id=pl.project_id
1663                         ) as pling_created_at
1664                         ,(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
1665                         ,p.project_changed_at as changed_at
1666                         ,p.project_created_at as created_at
1667                         from project_plings pl
1668                         inner join stat_projects p on pl.project_id = p.project_id and p.status > 30                        
1669                         where pl.is_deleted = 0 and pl.is_active = 1 
1670                         order by pling_created_at desc                                                  
1671         ';
1672          if (isset($limit)) {
1673             $sql .= ' limit ' . (int)$limit;
1674         }
1675         if (isset($offset)) {
1676             $sql .= ' offset ' . (int)$offset;
1677         }
1678         $resultSet = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1679 
1680         $imagehelper = new Default_View_Helper_Image();
1681         foreach ($resultSet as &$value) {
1682             $value['image_small'] = $imagehelper->Image($value['image_small'], array('width' => 200, 'height' => 200));
1683         }
1684 
1685         $result = Zend_Json::encode($resultSet);
1686         $cache->save($result, $cacheName, array(), 300);
1687 
1688         return $result;
1689     }
1690 
1691      /**
1692      * @param int $limit
1693      *
1694      * @return array|false|mixed
1695      */
1696     public function getTopScoreUsers($limit = 120,$offset=null)
1697     {
1698         /** @var Zend_Cache_Core $cache */
1699         $cache = Zend_Registry::get('cache');
1700         $cacheName = __FUNCTION__ . '_' . md5((int)$limit).md5((int)$offset);;
1701 
1702         if (false !== ($resultMembers = $cache->load($cacheName))) {
1703             return $resultMembers;
1704         }
1705 
1706         $sql = '
1707                     select  
1708                     s.*
1709                     ,m.profile_image_url
1710                     ,m.username
1711                     from member_score s
1712                     inner join member m on s.member_id = m.member_id
1713                     order by s.score desc             
1714                 ';
1715 
1716         
1717         if (isset($limit)) {
1718             $sql .= ' limit ' . (int)$limit;
1719         }
1720         if (isset($offset)) {
1721             $sql .= ' offset ' . (int)$offset;
1722         }
1723         
1724         $resultMembers = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1725 
1726         $cache->save($resultMembers, $cacheName, array(), 300);
1727 
1728         return $resultMembers;
1729     }
1730 
1731     public function getMostPlingedProductsTotalCnt(){
1732         $sql = '
1733             select count(1) as total_count
1734             from
1735             (
1736                 select distinct pl.project_id 
1737                 from project_plings pl
1738                 inner join stat_projects p on pl.project_id = p.project_id and p.status = 100
1739                 where pl.is_deleted = 0 and pl.is_active = 1 
1740             ) t
1741         ';
1742         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1743         $totalcnt = $result[0]['total_count'];
1744         return $totalcnt;
1745     }
1746 
1747     public function getMostPlingedProducts($limit = 20,$offset = null)
1748     {
1749         /** @var Zend_Cache_Core $cache */
1750         $cache = Zend_Registry::get('cache');
1751         $cacheName = __FUNCTION__ . '_' . md5((int)$limit).md5((int)$offset);
1752 
1753         if (false !== ($newSupporters = $cache->load($cacheName))) {
1754             return $newSupporters;
1755         }
1756 
1757         $sql = '  
1758                         select pl.project_id
1759                         ,count(1) as sum_plings 
1760                         ,p.title
1761                         ,p.image_small
1762                         ,p.laplace_score
1763                         ,p.count_likes
1764                         ,p.count_dislikes   
1765                         ,p.member_id 
1766                         ,p.profile_image_url
1767                         ,p.username
1768                         ,p.cat_title as catTitle
1769                         ,p.project_changed_at
1770                         ,p.version
1771                         ,p.description
1772                         ,p.package_names
1773                         ,p.count_comments
1774                         from project_plings pl
1775                         inner join stat_projects p on pl.project_id = p.project_id and p.status = 100
1776                         where pl.is_deleted = 0 and pl.is_active = 1 
1777                         group by pl.project_id
1778                         order by sum_plings desc 
1779                                                               
1780         ';
1781         if (isset($limit)) {
1782             $sql .= ' limit ' . (int)$limit;
1783         }
1784         if (isset($offset)) {
1785             $sql .= ' offset ' . (int)$offset;
1786         }
1787         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1788 
1789         $cache->save($result, $cacheName, array(), 300);
1790 
1791         return $result;
1792     }
1793 
1794     public function getMostPlingedProductsForUser($member_id, $limit = 20,$offset = null)
1795     {
1796         /** @var Zend_Cache_Core $cache */
1797         $cache = Zend_Registry::get('cache');
1798         $cacheName = __FUNCTION__ . '_' .md5($member_id).md5((int)$limit).md5((int)$offset);
1799 
1800         if (false !== ($newSupporters = $cache->load($cacheName))) {
1801             return $newSupporters;
1802         }
1803 
1804         $sql = '  
1805                         select pl.project_id
1806                         ,count(1) as sum_plings 
1807                         ,p.title
1808                         ,p.image_small                        
1809                         ,p.cat_title as catTitle
1810                         ,p.project_changed_at                        
1811                         from project_plings pl
1812                         inner join stat_projects p on pl.project_id = p.project_id and p.status = 100
1813                         where pl.is_deleted = 0 and pl.is_active = 1 and p.member_id = :member_id
1814                         group by pl.project_id
1815                         order by sum_plings desc 
1816                                                               
1817         ';
1818         if (isset($limit)) {
1819             $sql .= ' limit ' . (int)$limit;
1820         }
1821         if (isset($offset)) {
1822             $sql .= ' offset ' . (int)$offset;
1823         }
1824         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('member_id' => $member_id))->fetchAll();
1825 
1826         $cache->save($result, $cacheName, array(), 300);
1827 
1828         return $result;
1829     }
1830 
1831 
1832     public function getMostPlingedCreatorsTotalCnt(){
1833         $sql = '
1834             select count(1) as total_count
1835             from
1836             (
1837                 select distinct p.member_id
1838                 from stat_projects p
1839                 join project_plings pl on p.project_id = pl.project_id                       
1840                 where p.status = 100 and pl.is_deleted = 0 and pl.is_active = 1 
1841             ) t
1842         ';
1843         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1844         $totalcnt = $result[0]['total_count'];
1845         return $totalcnt;
1846     } 
1847     public function getMostPlingedCreators($limit = 20,$offset = null)
1848     {
1849         /** @var Zend_Cache_Core $cache */
1850         $cache = Zend_Registry::get('cache');
1851         $cacheName = __FUNCTION__ . '_' . md5((int)$limit).md5((int)$offset);
1852 
1853         if (false !== ($newSupporters = $cache->load($cacheName))) {
1854             return $newSupporters;
1855         }
1856 
1857         $sql = '  
1858                        select p.member_id,
1859                         count(1) as cnt,
1860                         m.username,
1861                         m.profile_image_url,
1862                         m.created_at
1863                         from stat_projects p
1864                         join project_plings pl on p.project_id = pl.project_id
1865                         join member m on p.member_id = m.member_id
1866                         where p.status = 100
1867                         and pl.is_deleted = 0 and pl.is_active = 1 
1868                         group by p.member_id
1869                         order by cnt desc                        
1870                                                               
1871         ';
1872         if (isset($limit)) {
1873             $sql .= ' limit ' . (int)$limit;
1874         }
1875         if (isset($offset)) {
1876             $sql .= ' offset ' . (int)$offset;
1877         }
1878         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1879 
1880         $cache->save($result, $cacheName, array(), 300);
1881 
1882         return $result;
1883     }
1884 
1885     public function getCountActiveSupporters()
1886     {
1887         /** @var Zend_Cache_Core $cache */
1888         $cache = Zend_Registry::get('cache');
1889         $cacheName = __FUNCTION__;
1890 
1891         if (false !== ($totalcnt = $cache->load($cacheName))) {
1892             return $totalcnt;
1893         }
1894         /*$sql = '
1895                         SELECT 
1896                         count( distinct s.member_id) as total_count
1897                         from support s                         
1898                         where s.status_id = 2  
1899                         and (DATE_ADD((s.active_time), INTERVAL 1 YEAR) > now())
1900         ';*/
1901 
1902         $sql = '
1903                     SELECT 
1904                     count( distinct s.member_id) as total_count
1905                     from v_support s                         
1906                     where is_valid = 1
1907         ';
1908         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1909         $totalcnt = $result[0]['total_count'];
1910         $cache->save($totalcnt, $cacheName, array(), 300);
1911 
1912         return $totalcnt;
1913     }
1914 
1915     public function getCountAllSupporters()
1916     {
1917         /** @var Zend_Cache_Core $cache */
1918         $cache = Zend_Registry::get('cache');
1919         $cacheName = __FUNCTION__;
1920 
1921         if (false !== ($totalcnt = $cache->load($cacheName))) {
1922             return $totalcnt;
1923         }
1924         $sql = '
1925                         SELECT 
1926                         count( distinct s.member_id) as total_count
1927                         from v_support s                         
1928                                           
1929         ';
1930 
1931         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
1932         $totalcnt = $result[0]['total_count'];
1933         $cache->save($totalcnt, $cacheName, array(), 300);
1934 
1935         return $totalcnt;
1936     }
1937 
1938     public function getCountTierSupporters($tier)
1939     {        
1940         $sql = "
1941                 select count(1) as cnt from
1942                     (
1943                     select 
1944                     member_id,
1945                     max(amount),
1946                     tier
1947                     from support
1948                     where status_id = 2
1949                     group by member_id
1950                     ) t where tier = :tier
1951 
1952         ";
1953         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('tier' =>$tier))->fetchAll();
1954         return $result[0]['cnt'];
1955     }
1956     
1957     public function getCountSectionSupporters($section_id)
1958     {        
1959         $sql = "
1960                 select count(1) as cnt FROM
1961                     section_support
1962                     JOIN support ON support.id = section_support.support_id
1963                     where support.status_id = 2
1964                     AND support.type_id = 1
1965                     AND section_support.section_id = :section_id
1966         ";
1967         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('section_id' =>$section_id))->fetchAll();
1968         return $result[0]['cnt'];
1969     }
1970     
1971     
1972     public function getCountSupportedMonthsForSectionAndMember($section_id, $member_id)
1973     {        
1974         $sql = "
1975                 SELECT COUNT(1) AS num_months FROM
1976                 (
1977                 SELECT s.member_id, p.yearmonth FROM section_support_paypements p
1978                 JOIN support s ON s.id = p.support_id
1979                 WHERE member_id = :member_id
1980                 AND p.section_id = :section_id
1981                 GROUP BY s.member_id, p.yearmonth
1982                 ) A
1983         ";
1984         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('member_id' => $member_id, 'section_id' =>$section_id))->fetchRow();
1985         return $result['num_months'];
1986     }
1987     
1988     public function getSumSupporting()
1989     {        
1990         $sql = "
1991                 SELECT SUM(s.tier) as sum_tier FROM v_support v
1992                 JOIN support s ON s.member_id = v.member_id AND s.active_time = v.active_time_max
1993                 WHERE v.is_valid = 1
1994         ";
1995         $result = Zend_Db_Table::getDefaultAdapter()->query($sql)->fetchAll();
1996         return $result[0]['sum_tier'];
1997     }
1998     
1999     public function getSumPayoutForMonth($yearmonth)
2000     {        
2001         $sql = "
2002                 SELECT SUM(p.probably_payout_amount) AS sum_payout FROM member_dl_plings p
2003                 WHERE p.yearmonth = :yearmonth
2004                 AND p.paypal_mail IS NOT null
2005         ";
2006         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('yearmonth' => $yearmonth))->fetchAll();
2007         return $result[0]['sum_tier'];
2008     }
2009 
2010     public function getModeratorsList()
2011     {
2012         /** @var Zend_Cache_Core $cache */
2013         $cache = Zend_Registry::get('cache');
2014         $cacheName = __FUNCTION__;
2015 
2016         if (false !== ($newMembers = $cache->load($cacheName))) {
2017             return $newMembers;
2018         }
2019 
2020         $sql = '
2021                 SELECT 
2022                 member_id,
2023                 profile_image_url,
2024                 username,
2025                 created_at
2026                 FROM member
2027                 WHERE `is_active` = :activeVal
2028                 AND `type` = :typeVal     
2029                 and `roleid` = :roleid
2030                 ORDER BY created_at DESC             
2031             ';
2032 
2033         if (isset($limit)) {
2034             $sql .= ' limit ' . (int)$limit;
2035         }
2036 
2037         $resultMembers = Zend_Db_Table::getDefaultAdapter()->query($sql, array(
2038             'activeVal' => Default_Model_Member::MEMBER_ACTIVE,
2039             'typeVal'   => Default_Model_Member::MEMBER_TYPE_PERSON,
2040             'roleid'    => Default_Model_DbTable_Member::ROLE_ID_MODERATOR
2041         ))->fetchAll()
2042         ;
2043 
2044         $cache->save($resultMembers, $cacheName, array(), 300);
2045 
2046         return $resultMembers;
2047     }
2048 
2049 
2050     public function getCountMembers()
2051     {
2052         /** @var Zend_Cache_Core $cache */
2053         $cache = Zend_Registry::get('cache');
2054         $cacheName = __FUNCTION__;
2055 
2056         if (false !== ($totalcnt = $cache->load($cacheName))) {
2057             return $totalcnt;
2058         }
2059         $sql = "
2060                         SELECT
2061                             count(1) AS total_count
2062                         FROM
2063                             member
2064                         WHERE
2065                             is_active=1 AND is_deleted=0
2066                        ";
2067 
2068         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array())->fetchAll();
2069         $totalcnt = $result[0]['total_count'];
2070         $cache->save($totalcnt, $cacheName, array(), 300);
2071 
2072         return $totalcnt;
2073     }
2074 
2075     public function getTooptipForMember($member_id)
2076     {
2077         /** @var Zend_Cache_Core $cache */
2078         $cache = Zend_Registry::get('cache');
2079         $cacheName = __FUNCTION__ . '_' . md5($member_id);
2080 
2081         if (false !== ($tooptip = $cache->load($cacheName))) {
2082             return $tooptip;
2083         }
2084 
2085         $modelMember = new Default_Model_Member();
2086         $tblFollower = new Default_Model_DbTable_ProjectFollower();
2087         $modelProject = new Default_Model_Project();
2088         $printDate = new Default_View_Helper_PrintDate();
2089         $printDateSince = new Default_View_Helper_PrintDateSince();
2090 
2091         $cnt = $modelMember->fetchCommentsCount($member_id);
2092         $cntLikesGave = $tblFollower->countLikesHeGave($member_id);
2093         $cntLikesGot = $tblFollower->countLikesHeGot($member_id);
2094         $donationinfo = $modelMember->fetchSupporterDonationInfo($member_id);
2095         $lastactive = $modelMember->fetchLastActiveTime($member_id);
2096         $cntprojects = $modelProject->countAllProjectsForMember($member_id, true);
2097 
2098         $member = $modelMember->find($member_id)->current();
2099         $textCountryCity = $member->city;
2100         $textCountryCity .= $member->country ? ', ' . $member->country : '';
2101 
2102         $data = array(
2103             'totalComments' => $cnt,
2104             'created_at'    => $printDateSince->printDateSince($member->created_at),
2105             'username'      => $member->username,
2106             'countrycity'   => $textCountryCity,
2107             'lastactive_at' => $printDateSince->printDateSince($lastactive),
2108             'cntProjects'   => $cntprojects,
2109             'issupporter'   => $donationinfo['issupporter'],
2110             'supportMax'    => $donationinfo['active_time_max'],
2111             'supportMin'    => $donationinfo['active_time_min'],
2112             'supportCnt'    => $donationinfo['cnt'],
2113             'cntLikesGave'  => $cntLikesGave,
2114             'cntLikesGot'   => $cntLikesGot
2115         );
2116 
2117         $cache->save($data, $cacheName, array(), 3600);
2118 
2119         return $data;
2120     }
2121 
2122 
2123     public function getProbablyPayoutPlingsCurrentmonth($project_id)
2124     {
2125         $sql =
2126             " select FORMAT(probably_payout_amount, 2) as amount from member_dl_plings where project_id = :project_id and yearmonth=(DATE_FORMAT(NOW(),'%Y%m'))";
2127         $result = Zend_Db_Table::getDefaultAdapter()->fetchRow($sql, array('project_id' => $project_id));
2128 
2129         return $result['amount'];
2130     }
2131 
2132     public function getOCSInstallInstruction()
2133     {
2134         /** @var Zend_Cache_Core $cache */
2135         $cache = Zend_Registry::get('cache');
2136         $cacheName = __FUNCTION__;
2137 
2138         if (false !== ($instruction = $cache->load($cacheName))) {
2139             return $instruction;
2140         }
2141         $config = Zend_Registry::get('config')->settings->server->opencode;
2142         $readme = 'https://opencode.net/OCS/ocs-url/raw/master/docs/How-to-install.md?inline=false';
2143 
2144         $httpClient = new Zend_Http_Client($readme, array('keepalive' => true, 'strictredirects' => true));
2145         $httpClient->resetParameters();
2146         $httpClient->setUri($readme);
2147         $httpClient->setHeaders('Private-Token', $config->private_token);
2148         $httpClient->setHeaders('Sudo', $config->user_sudo);
2149         $httpClient->setHeaders('User-Agent', $config->user_agent);
2150         $httpClient->setMethod(Zend_Http_Client::GET);
2151 
2152         $response = $httpClient->request();
2153 
2154         $body = $response->getRawBody();
2155 
2156         if (count($body) == 0) {
2157             return array();
2158         }
2159         include_once('Parsedown.php');
2160         $Parsedown = new Parsedown();
2161 
2162         $readmetext = $Parsedown->text($body);
2163 
2164         $cache->save($readmetext, $cacheName, array(), 3600);
2165 
2166         return $readmetext;
2167     }
2168 
2169     public function getDiscussionOpendeskop($member_id)
2170     {
2171         $sql = "
2172                 select 
2173                  c.comment_id
2174                 ,c.comment_text
2175                 ,c.comment_member_id
2176                 ,c.comment_created_at
2177                 ,m.username          
2178                 ,p.project_id
2179                 ,p.title
2180                 ,cp.comment_member_id p_comment_member_id
2181                 ,(select username from member m where m.member_id = cp.comment_member_id) p_username
2182                 from comments c 
2183                 inner join project p on c.comment_target_id = p.project_id and p.status = 100
2184                 inner join  member m ON c.comment_member_id = m.member_id
2185                 left join comments cp on c.comment_parent_id = cp.comment_id
2186                 where c.comment_type = 0 and c.comment_active = 1
2187                 and c.comment_member_id = :member_id
2188                 ORDER BY c.comment_created_at DESC
2189                 limit 10
2190                 ";
2191         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('member_id' => $member_id))->fetchAll();
2192         return $result;
2193     }
2194 }