[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/dao/ -> blogs.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" );    
   4      lt_include( PLOG_CLASS_PATH."class/dao/blogstatus.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/dao/daocacheconstants.properties.php" );
   6      
   7      define( "ALL_BLOG_CATEGORIES", 0 );   
   8      
   9      /**
  10       * \ingroup DAO
  11       * DAO class for BlogInfo objects, which represent a blog.
  12       */
  13      class Blogs extends Model
  14      {
  15      	function Blogs()
  16          {
  17              $this->Model();        
  18              $this->table = $this->getPrefix()."blogs";
  19          }
  20  
  21          /**
  22           * Returns information about a blog.
  23           *
  24           * @param blogId Identifier of the blog we want to get information
  25           * @param extendedInfo
  26           * @return Returns a BlogInfo object containing information about the blog
  27           */
  28          function getBlogInfo( $blogId )
  29          {
  30              lt_include( PLOG_CLASS_PATH . 'class/dao/bloginfo.class.php' );
  31              return( $this->get( "id", $blogId,
  32                                  CACHE_BLOGINFOS,
  33                                  Array( CACHE_BLOGIDBYNAME => "getMangledBlogName",
  34                                         CACHE_BLOGIDBYDOMAIN => "getCustomDomain")));
  35          }
  36  
  37          /**
  38           * Returns information about a blog.
  39           *
  40           * @param blogName Identifier of the blog we want to get information
  41           * @return Returns a BlogInfo object containing information about the blog
  42           */
  43          function getBlogInfoByName( $blogName, $extendedInfo = false )
  44          {
  45              lt_include( PLOG_CLASS_PATH . 'class/dao/bloginfo.class.php' );        
  46              return( $this->get( "mangled_blog", $blogName,
  47                                  CACHE_BLOGIDBYNAME,
  48                                  Array( CACHE_BLOGINFOS => "getId" )));
  49          }
  50  
  51          function getBlogInfoByDomain($blogDomain, $extendedInfo = false){
  52              lt_include(PLOG_CLASS_PATH . 'class/dao/bloginfo.class.php');
  53              return( $this->get( "custom_domain", $blogDomain,
  54                                  CACHE_BLOGIDBYDOMAIN,
  55                                  Array( CACHE_BLOGINFOS => "getId" )));
  56          }
  57          
  58          /**
  59           * @see Model::getSearchConditions
  60           */
  61  		function getSearchConditions( $searchTerms )
  62          {
  63              return( "blog LIKE '%".Db::qstr( $searchTerms )."%'" );
  64          }
  65  
  66          /**
  67           * Updates blog category counters
  68           *
  69           * @private
  70           */
  71  		function updateBlogCategoriesLink( $blogInfo, $oldBlogInfo = null )
  72          {
  73              lt_include( PLOG_CLASS_PATH."class/dao/blogcategories.class.php" );
  74              $blogCategories = new BlogCategories();
  75              $blogCategory = $blogInfo->getBlogCategory();
  76              
  77              if( $blogCategory ) {
  78                  $blogCategory->setNumActiveBlogs($this->getNumItems( $this->getPrefix().'blogs', 'blog_category_id = '.$blogCategory->getId().' AND status = '.BLOG_STATUS_ACTIVE ));
  79                  $blogCategory->setNumBlogs($this->getNumItems( $this->getPrefix().'blogs', 'blog_category_id = '.$blogCategory->getId()));
  80                  $blogCategories->updateBlogCategory( $blogCategory );
  81              }
  82              
  83              if( $oldBlogInfo ) {
  84                  $oldBlogCategory = $oldBlogInfo->getBlogCategory();
  85                  if ($oldBlogCategory)
  86                  {
  87                      $oldBlogCategory->setNumActiveBlogs($this->getNumItems( $this->getPrefix().'blogs', 'blog_category_id = '.$oldBlogCategory->getId().' AND status = '.BLOG_STATUS_ACTIVE ));
  88                      $oldBlogCategory->setNumBlogs($this->getNumItems( $this->getPrefix().'blogs', 'blog_category_id = '.$oldBlogCategory->getId()));
  89                      $blogCategories->updateBlogCategory( $oldBlogCategory );
  90                  }        
  91              }
  92              
  93              return( true );
  94          }
  95          
  96          /**
  97           * Updates the configuration of a blog
  98           *
  99           * @param blogId Blog identifier
 100           * @param blogInfo A BlogInfo object containing all the information of the blog
 101           * @param return Returns true if everything's ok or false otherwise
 102           */
 103          function updateBlog( &$blog )
 104          {
 105              // load the previous version of this blog
 106              $prevVersion = $this->getBlogInfo( $blog->getId());
 107              
 108              // check that the mangled_blog field is unique
 109              $mangledBlog = $blog->getMangledBlogName();
 110              $i = 1;
 111              // check if there already is a blog with the same mangled name
 112              $done = false;
 113              while(( $tmpBlog = $this->getBlogInfoByName( $mangledBlog )) && !$done)
 114              {
 115                  if( $tmpBlog->getId() != $blog->getId()) {
 116                      $i++;
 117                      $mangledBlog = substr($mangledBlog, 0,
 118                                     ($i > 2) ? strlen($mangledBlog)-strlen($i-1) : strlen($mangledBlog)).$i;
 119                  }
 120                  else {
 121                      $done = true;
 122                  }
 123              }
 124              $blog->setMangledBlogName($mangledBlog);                
 125              
 126              if( ($result = $this->update( $blog ))) {
 127                  // reset the caches
 128                  $this->_cache->removeData( $blog->getCustomDomain(), CACHE_BLOGIDBYDOMAIN );
 129                  $this->_cache->removeData( $blog->getMangledBlogName(), CACHE_BLOGIDBYNAME );
 130                  $this->_cache->removeData( $blog->getId(), CACHE_BLOGINFOS );
 131                                      
 132                  // update blog categories
 133                  $this->updateBlogCategoriesLink( $blog, $prevVersion );
 134                  
 135                  // some settings in the BlogSettings object might affect other database items, such
 136                  // as the sorting order of categories and link cateegories. Hence, we should reset
 137                  // those caches too
 138                  $this->_cache->removeData( $blog->getId(), CACHE_ARTICLE_CATEGORIES_BLOG );
 139                  $this->_cache->removeData( $blog->getId(), CACHE_MYLINKCATEGORIES_ALL );
 140              }
 141  
 142              // always return true,
 143              // $result is only false if nothing was changed, but that really isn't an error?
 144              return true;
 145          }
 146  
 147           /**
 148            * Adds a new blog to the database.
 149            *
 150            * @param blog A BlogInfo object with the necessary information
 151            * @see BlogInfo
 152            * @return False if unsuccessful or true otherwise. It will also set the database id of the
 153            * parameter passed by reference in case it is successful.
 154            */
 155           function addBlog( &$blog )
 156           {
 157              // source classes
 158              lt_include( PLOG_CLASS_PATH."class/dao/bayesianfilterinfos.class.php" );
 159              lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
 160  
 161              $blogSettings = $blog->getSettings();
 162              if( !$blogSettings )
 163                  $blogSettings = new BlogSettings();
 164  
 165              // check that the mangled_blog field is unique
 166              $mangledBlog = $blog->getMangledBlogName();
 167              $i = 1;
 168              // check if there already is a blog with the same mangled name
 169              while($this->getBlogInfoByName( $mangledBlog ))
 170              {
 171                  $i++;
 172                  $mangledBlog = substr($mangledBlog, 0,
 173                                 ($i > 2) ? strlen($mangledBlog)-strlen($i-1) : strlen($mangledBlog)).$i;
 174              }
 175              $blog->setMangledBlogName($mangledBlog);
 176  
 177              $blogId = $this->add( $blog );
 178              
 179              // update blog categories
 180              $this->updateBlogCategoriesLink( $blog );
 181  
 182              // create the row for the bayesian filter info
 183              $bayesianFilterInfo = new BayesianFilterInfos();
 184              $bayesianFilterInfo->insert( $blogId );
 185              
 186              $this->_cache->setData( $blogId, CACHE_BLOGINFOS, $blog );
 187  //            $this->_cache->setData( $blog->getMangledBlogName(), CACHE_BLOGIDBYNAME );
 188  
 189              // and return the blog identifier
 190              return $blogId;
 191           }
 192  
 193           /**
 194            * Returns all the blogs defined for the site in an array, sorted by its
 195            * blog identifier.
 196            *
 197            * @param status
 198            * @param searchTerms
 199            * @param page
 200            * @param itemsPerPage
 201            *
 202            * @return Returns an array with all the blogs defined for this site. The array
 203            * is sorted by the blog identifier, so that $blogs[$blogId] will give us the information
 204            * of the blog with $blogId as its identifier.
 205            */
 206           function getAllBlogs( $status = BLOG_STATUS_ALL, 
 207                                 $blogCategoryId = ALL_BLOG_CATEGORIES,
 208                                 $searchTerms = "", 
 209                                 $page = -1, 
 210                                 $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
 211          {
 212              $statusCond = "";
 213              if( $status != BLOG_STATUS_ALL )
 214                  $statusCond = "status = '".Db::qstr($status)."'";
 215              $where = $statusCond;
 216              
 217              if( $blogCategoryId != ALL_BLOG_CATEGORIES )
 218                  $where .= " AND blog_category_id = '".Db::qstr($blogCategoryId)."'";
 219  
 220              $searchCond = "";
 221              if( $searchTerms != "" ){
 222                  $searchCond = $this->getSearchConditions( $searchTerms );
 223                  if( $where != "" )
 224                      $where .= " AND ";
 225                  $where .= $searchCond;
 226              }
 227                  
 228              if( $where != "" )
 229                  $where = " WHERE $where";
 230  
 231              $query = "SELECT * FROM ".$this->getPrefix()."blogs $where ORDER BY id DESC";
 232  
 233              $result = $this->Execute( $query, $page, $itemsPerPage );
 234  
 235              if( !$result )
 236                  return false;
 237  
 238              $blogs = Array();
 239              while( $row = $result->FetchRow()) {
 240                  // map the row we just loaded
 241                  $blog = $this->mapRow( $row );
 242                  $blogs[$blog->getId()] = $blog;
 243                  
 244                  // and cache whatever we loaded for later use, just in case
 245                  $this->_cache->setData( $blog->getId(), CACHE_BLOGINFOS, $blog );
 246                  $this->_cache->setData( $blog->getMangledBlogName(), CACHE_BLOGIDBYNAME, $blog );                
 247                  $this->_cache->setData( $blog->getCustomDomain(), CACHE_BLOGIDBYDOMAIN, $blog );                
 248              }
 249              $result->Close();            
 250  
 251              return $blogs;                                    
 252          }    
 253  
 254          /**
 255           * returns only an array with all the blog ids
 256           *
 257           * @return an array with blog ids
 258           */
 259          function getAllBlogIds()
 260          {
 261              $query = "SELECT id FROM ".$this->getPrefix()."blogs";
 262  
 263              $result = $this->Execute( $query );
 264              if( !$result )
 265                  return Array();
 266  
 267              $blogIds = Array();
 268              while( $row = $result->FetchRow()) {
 269                  $blogIds[] = $row['id'];
 270              }
 271              $result->Close();            
 272  
 273              return $blogIds;
 274          }
 275  
 276          /**
 277           * returns the total number of blogs in the site
 278           *
 279           * @param status
 280           * @param searchTerms
 281           *
 282           * @return The number of blogs
 283           */
 284          function getNumBlogs( $status = BLOG_STATUS_ALL, $blogCategoryId = ALL_BLOG_CATEGORIES, $searchTerms = "" )
 285          {    
 286              $where = "";
 287              $statusCond = "";
 288              if( $status != BLOG_STATUS_ALL )
 289                  $statusCond = "status = '".Db::qstr($status)."'";
 290              $where .= $statusCond;            
 291  
 292              if( $blogCategoryId != ALL_BLOG_CATEGORIES )
 293                  $where .= " AND blog_category_id = '".Db::qstr($blogCategoryId)."'";                
 294                                          
 295              if( $searchTerms != "" ) {                
 296                  $searchCond = $this->getSearchConditions( $searchTerms );
 297                  if( $where != "" )
 298                      $where .= " AND";
 299                   $where .= " ".$searchCond;
 300              }
 301  
 302              return( $this->getNumItems( $this->getPrefix()."blogs", $where ));
 303          }
 304  
 305          /**
 306           * Removes a blog from the database. It also removes all its posts, its posts categories
 307           * its links, its links categories, its trackbacks and its comments
 308           *
 309           * @param blogId the id of the blog we'd like to delete
 310           * @return boolean success of the operation
 311           */
 312          function deleteBlog( $blogId )
 313          {
 314              // update blog categories
 315              $blog = $this->getBlogInfo( $blogId );
 316              $this->updateBlogCategoriesLink( $blog );    
 317  
 318              // source class
 319              lt_include( PLOG_CLASS_PATH."class/template/templatesets/templatesets.class.php" );
 320      
 321              // delete the blog template sets
 322              $templateSets = new TemplateSets();
 323              $tsStorage = new TemplateSetStorage();
 324              $blogTemplates = $templateSets->getBlogTemplates( $blogId );
 325              foreach( $blogTemplates as $template ) {
 326                  $tsStorage->removeBlogTemplate( $template, $blogId );
 327              }
 328              // when done, remove the parent "blog_X" folder
 329              File::deleteDir( TemplateSetStorage::getBlogBaseTemplateFolder( $blogId ));
 330  
 331              // and finally, remove the cache and delete the blog
 332              $this->_cache->removeData( $blog->getCustomDomain(), CACHE_BLOGIDBYDOMAIN );
 333              $this->_cache->removeData( $blog->getMangledBlogName(), CACHE_BLOGIDBYNAME );
 334              $this->_cache->removeData( $blog->getId(), CACHE_BLOGINFOS );
 335              return( $this->delete( "id", $blogId ));
 336          }
 337  
 338          /**
 339           * @private
 340           */
 341          function mapRow( $row )
 342          {
 343              // source class
 344              lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
 345  
 346              // create new BlogInfo
 347              $blogInfo = new BlogInfo( stripslashes($row["blog"]),
 348                                        $row["owner_id"],
 349                                        stripslashes($row["about"]),
 350                                        unserialize($row["settings"]),
 351                                        $row["id"] );
 352  
 353              // load information about the blog status
 354              $blogInfo->setStatus( $row["status"] );
 355              // load information abotu the blog category id
 356              $blogInfo->setBlogCategoryId( $row["blog_category_id"] );
 357              // counters
 358              $blogInfo->setTotalPosts( $row['num_posts'] );
 359              $blogInfo->setTotalTrackbacks( $row['num_trackbacks'] );
 360              $blogInfo->setTotalComments( $row['num_comments'] );
 361              // mangled blog
 362              $blogInfo->setMangledBlogName( $row['mangled_blog'], false );
 363              $blogInfo->setCustomDomain( $row['custom_domain'] );
 364              // show in summary or not
 365              $blogInfo->setShowInSummary( $row['show_in_summary'] );
 366              // create date and update date
 367              $blogInfo->setCreateDate( $row['create_date'] );
 368              $blogInfo->setUpdateDate( $row['last_update_date'] );
 369  
 370              return $blogInfo;
 371          }
 372      }
 373  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics