<?php
    lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );    

    class UnusedBlogs extends Blogs
    {
    	function UnusedBlogs()
    	{
    		$this->Blogs();
    	}

         /**
          * Returns all the blogs defined for the site in an array, sorted by its
          * blog identifier.
          *
          * @param last_update
          * @param total_posts_less_than
          * @param status
          * @param blogCategoryId
          * @param searchTerms
          * @param page
          * @param itemsPerPage
          *
          * @return Returns an array with all the blogs defined for this site. The array
          * is sorted by the blog identifier, so that $blogs[$blogId] will give us the information
          * of the blog with $blogId as its identifier.
          */
         function getAllBlogs( $last_update,
							   $total_posts_less_than,
                               $status = BLOG_STATUS_ALL,
                               $blogCategoryId = ALL_BLOG_CATEGORIES,
                               $searchTerms = "", 
                               $page = -1, 
                               $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
        {
            $lastUpdateCond = "last_update_date < ".$this->_getStartTime( $last_update );
			$totalPostLessThanCond = "num_posts < ".$total_posts_less_than;
			$where = $lastUpdateCond." AND ".$totalPostLessThanCond;
			
            if( $status != BLOG_STATUS_ALL )
                $where .= " AND status = '".Db::qstr($status)."'";

			if( $blogCategoryId != ALL_BLOG_CATEGORIES ) {
				$where .= " AND blog_category_id = '".Db::qstr($blogCategoryId)."'";
			}

			if( $searchTerms != "" ){
				$where .= " AND " .$this->getSearchConditions( $searchTerms );
            }
				
            $query = "SELECT * FROM ".$this->getPrefix()."blogs WHERE $where ORDER BY id DESC";

            $result = $this->Execute( $query, $page, $itemsPerPage );

            if( !$result )
                return false;

            $blogs = Array();
            while( $row = $result->FetchRow()) {
				// map the row we just loaded
                $blog = $this->mapRow( $row );
                $blogs[$blog->getId()] = $blog;
				
				// and cache whatever we loaded for later use, just in case
				$this->_cache->setData( $blog->getId(), CACHE_BLOGINFOS, $blog );
				$this->_cache->setData( $blog->getMangledBlogName(), CACHE_BLOGIDBYNAME, $blog );				
				$this->_cache->setData( $blog->getCustomDomain(), CACHE_BLOGIDBYDOMAIN, $blog );				
            }
            $result->Close();			

            return $blogs;									
        }    

        /**
         * returns the total number of blogs in the site
         *
         * @param last_update
         * @param total_posts_less_than
         * @param status
         * @param blogCategoryId
         * @param searchTerms
         *
         * @return The number of blogs
         */
        function getNumBlogs( $last_update = 30, $total_posts_less_than = 5, $status = BLOG_STATUS_ALL,
                              $blogCategoryId = ALL_BLOG_CATEGORIES, $searchTerms = "" )
        {	
            $lastUpdateCond = "last_update_date < ".$this->_getStartTime( $last_update );
			$totalPostLessThanCond = "num_posts < ".$total_posts_less_than;
			$where = $lastUpdateCond." AND ".$totalPostLessThanCond;

             if( $status != BLOG_STATUS_ALL )
                 $where .= " AND status = '".Db::qstr($status)."'";

			if( $blogCategoryId != ALL_BLOG_CATEGORIES )
				$where .= " AND blog_category_id = '".Db::qstr($blogCategoryId)."'";				
										
			if( $searchTerms != "" ) {				
				$searchCond = $this->getSearchConditions( $searchTerms );
 				$where .= " AND " . $searchCond;
            }
			return( $this->getNumItems( $this->getPrefix()."blogs", $where ));
        }

		/** 
		 * @private
		 */
		function _getStartTime( $duration ) 
		{
			lt_include( PLOG_CLASS_PATH.'class/data/timestamp.class.php' );

            $time = new Timestamp();
            $time->subtractSeconds( $duration * 24 * 60 * 60 );
            return $time->getTimestamp();
		}
		
    }
?>