[ 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/gallery/dao/ -> galleryalbums.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryalbum.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );
   6      lt_include( PLOG_CLASS_PATH.'class/dao/daocacheconstants.properties.php' );
   7      
   8      /**
   9       * \ingroup Gallery
  10       *
  11       * Database access for GalleryAlbum objects.
  12       *
  13       * Please keep in mind that in the context of this library, every album has to have an user who
  14       * owns it. When this library is used within pLog, users don't own albums but blogs do so we will
  15       * use blog identifier instead of user identifiers. The reason for this change on how things are
  16       * called was that this library can also be used outside pLog, and outside pLog we will most likely
  17       * not have blogs but users.
  18       *
  19       * @see GalleryAlbum
  20       * @see Model
  21       */
  22      class GalleryAlbums extends Model
  23      {
  24  
  25          var $_childAlbums;
  26          var $resources;
  27  
  28          /**
  29           * Constructor. Calls the Model constructor and does nothing more.
  30           */
  31      	function GalleryAlbums()
  32          {
  33              $this->Model();
  34              $this->_childAlbums = Array();
  35              
  36              $this->table = $this->getPrefix()."gallery_albums";
  37          }
  38  
  39          /**
  40           * Returns an array with all the albums that belong to the given
  41           * owner
  42           *
  43           * @param ownerId The identifier of the owner whose albums we'd like to fetch
  44           * @param onlyShownAlbums Returns only those albums that have the show_album
  45           * field set to true, or all of them otherwise
  46           * @param page The page we'd like to see
  47           * @param itemsPerPage number of items per page
  48           * @return An array containing GalleryAlbum objects, representing the
  49           * albums we fetched from the db.
  50           */
  51          function getUserAlbums( $ownerId, $onlyShownAlbums = false, $page = DEFAULT_PAGING_ENABLED, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
  52          {
  53              $albums = $this->getMany( "owner_id",
  54                                        $ownerId,
  55                                        CACHE_USERALBUMS,
  56                                        Array( CACHE_GALLERYALBUM => "getId" ),
  57                                        Array( "date" => "ASC" ),
  58                                        "",
  59                                        $page,
  60                                        $itemsPerPage );
  61              if( !$albums )
  62                  return Array();
  63                  
  64              $result = Array();
  65              if( $onlyShownAlbums ) {
  66                  foreach( $albums as $album ) {
  67                      if( $album->getShowAlbum())
  68                          $result[] = $album;
  69                  }
  70              }
  71              else
  72                  $result = $albums;
  73                  
  74              return( $result );
  75          }       
  76  
  77          /**
  78           * Returns a specific album from the database.
  79           *
  80           * @param id A valid album identifier.
  81           * @param ownerId (optional) The album should have the given id _and_ it should
  82           * belong to the given user.
  83           * @param fetchResources (optional) Whether at the same time, we should also fetch the
  84           * resources that are associated to this album. Set it to 'false' if you only need
  85           * to access the album and do not need to use methods such as GalleryAlbum::getResources()
  86           * to fetch the resources that have been categorized under this album. It of course
  87           * saves resources and database queries.
  88           * @param onlyShownAlbums Forces to retrieve only the albums that have been set to appear
  89           * in the main page. If set to 'true', we will generate an error if the album exists, 
  90           * has the specified id and belongs to the specified user _but_ it is not supposed to be 
  91           * shown in the main page.
  92           * @return Returns a valid GalleryAlbum object or 'false' if the album could not be found.
  93           */
  94          function getAlbum( $id, $ownerId = -1, $fetchResources = true, $onlyShownAlbums = false )
  95          {
  96              $album = $this->get( "id", $id, CACHE_GALLERYALBUM );
  97              if( !$album )
  98                  return false;
  99              if( $ownerId > -1 && $album->getOwnerId() != $ownerId )
 100                  return false;
 101              if( $onlyShownAlbums && !$album->getShowAlbum())
 102                  return false;
 103                  
 104              return( $album );
 105          }
 106          
 107          /**
 108           * Returns a specific album from the database given its 'mangled name'
 109           *
 110           * @param name An album name
 111           * @param ownerId (optional) The album should have the given id _and_ it should
 112           * belong to the given user.
 113           * @param fetchResources (optional) Whether at the same time, we should also fetch the
 114           * resources that are associated to this album. Set it to 'false' if you only need
 115           * to access the album and do not need to use methods such as GalleryAlbum::getResources()
 116           * to fetch the resources that have been categorized under this album. It of course
 117           * saves resources and database queries.
 118           * @param onlyShownAlbums Forces to retrieve only the albums that have been set to appear
 119           * in the main page. If set to 'true', we will generate an error if the album exists, 
 120           * has the specified id and belongs to the specified user _but_ it is not supposed to be 
 121           * shown in the main page.
 122           * @return Returns a valid GalleryAlbum object or 'false' if the album could not be found.
 123           */
 124  		function getAlbumByName( $name, $ownerId = -1, $fetchResources = true, $onlyShownAlbums = false )
 125          {
 126              // there might be more than one with the same name...
 127              $albums = $this->getMany( "mangled_name", $name, CACHE_GALLERYALBUMBYNAME );
 128              if(!$albums){
 129                  return false;
 130              }
 131              foreach( $albums as $album ) {
 132                  if( $this->check( $album, $ownerId, $onlyShownAlbums )) {
 133                      return $album;
 134                  }
 135              }
 136              
 137              return false;
 138          }
 139  
 140          /**
 141           * @private
 142           */
 143  		function check( $album, $ownerId = -1, $onlyShownAlbums = false )
 144          {
 145              if( $ownerId > -1 && $album->getOwnerId() != $ownerId )
 146                  return false;
 147              if( $onlyShownAlbums && !$album->getShowAlbum())
 148                  return false;
 149              return( true );
 150          }
 151                  
 152          
 153          /**
 154           * Returns an array with all the child albums of the given album, but only
 155           * the ones at the first level (it is not recursive!)
 156           *
 157           * @param albumId The album identfitier whose children we'd like to get.
 158           * @param ownerId To whom should this album belong.
 159           * @param onlyShownAlbums Returns only those albums that have the show_album
 160           * field set to true, or all of them otherwise
 161           * @return An array of GalleryAlbum objects
 162           */
 163          function getChildAlbums( $albumId, $ownerId, $onlyShownAlbums = false )
 164          {
 165              $albums = $this->getUserAlbums( $ownerId, $onlyShownAlbums );
 166              
 167              // return an empty array if there are no albums
 168              if( !$albums )
 169                  return Array();
 170                  
 171              $result = Array();
 172              foreach( $albums as $album ) {
 173                  if( $album->getParentId() == $albumId )
 174                      $result[] = $album;
 175              }
 176              
 177              return( $result );
 178          }
 179  
 180          /**
 181           * Returns an array with all the parent album ids of the given album
 182           *
 183           * @param album The album object whose parents we'd like to get.
 184           * @return An array of GalleryAlbum Id
 185           */        
 186  		function getAllParentAlbumIds( $album ) {
 187                      
 188              $currentParentId = $album->getParentId();
 189              $parentAlbumsIds = array();
 190          
 191              while ( $currentParentId != 0 ) {
 192                  $parentAlbumsIds[] = $currentParentId;
 193                  $parentAlbum = $this->getAlbum( $currentParentId, -1, false, false );
 194                  $currentParentId = $parentAlbum->getParentId();
 195              }
 196              return $parentAlbumsIds;
 197          }
 198          
 199          /**
 200           * Adds an album to the database
 201           *
 202           * @param album A GalleryAlbum object, with all its data filled in
 203           * @return Returns true if successful or false otherwise.
 204           * @see GAlleryAlbum
 205           */
 206  		function addAlbum( &$album )
 207          {            
 208              if(( $result = $this->add( $album ))) {
 209                  $this->_cache->removeData( $album->getOwnerId(), CACHE_USERALBUMS );
 210                  $this->_cache->removeData( $album->getOwnerId(), CACHE_USERALBUMS_NESTED );                
 211                  if( $album->getParentId() > 0 ) {
 212                      // update the counters of the parent if there's any
 213                      $parent = $album->getParent();
 214                      if( $parent ) {
 215                          $parent->setNumChildren( $parent->getNumChildren() + 1 );
 216                          $this->updateAlbum( $parent );
 217                      }
 218                  }
 219              }
 220              
 221              return $result;
 222          }
 223  
 224          /**
 225           * updates an album in the db
 226           *
 227           * @param album A GalleryAlbum object that already exists in the db.
 228           * @param Returns true if successful or false otherwise.
 229           */
 230          function updateAlbum( $album )
 231          {
 232              if ($album->getId() == $album->getParentId()){
 233                  return false;
 234              }
 235              
 236              if ($album->getParentId() != 0) {
 237                  $parentAlbums = $this->getAllParentAlbumIds( $this->getAlbum( $album->getParentId(), -1, false, false ) ) ;
 238              
 239                  foreach ( $parentAlbums as $parentAlbum ) {
 240                      if ( $parentAlbum == $album->getId() )
 241                          return false;
 242                  }
 243              }
 244              
 245              // load the previous version of this album
 246              $prevVersion = $this->getAlbum( $album->getId());            
 247              
 248              $result = $this->update( $album );
 249              if( $result ) {
 250                  // remove the album from the cache
 251                  $this->_cache->removeData( $album->getId(), CACHE_GALLERYALBUM );
 252                  // remove the cached album hierarchy array
 253                  $this->_cache->removeData( $album->getOwnerId(), CACHE_USERALBUMS );            
 254                  // and the cache for nested albums too
 255                  $this->_cache->removeData( $album->getOwnerId(), CACHE_USERALBUMS_NESTED );
 256                  
 257                  // update the counters of the parent album
 258                  if( $prevVersion->getParentId() != $album->getParentId()) {
 259                      // increase the counter of the new album
 260                      $parent = $album->getParent();
 261                      if( $parent )
 262                      {
 263                          $parent->setNumChildren( $parent->getNumChildren() + 1 );
 264                          // update the objects
 265                          $this->updateAlbum( $parent );
 266                      }
 267                      // decrease the number of children in the previous parent
 268                      $prevParent = $prevVersion->getParent();
 269                      if( $prevParent )
 270                      {
 271                          $prevParent->setNumChildren( $prevParent->getNumChildren() - 1 );
 272                          // update the objects
 273                          $this->updateAlbum( $prevParent );
 274                      }
 275                  }
 276              }            
 277  
 278              return( $result );
 279          }
 280  
 281          /**
 282           * removes an album from the db
 283           *
 284           * @param albumId The album identifier
 285           * @param userId The user identifier to whom the album belongs (optional)
 286           * @return True if successful or false otherwise.
 287           */
 288          function deleteAlbum( $albumId, $userId = -1 )
 289          {
 290              // remove the cached album hierarchy array 1st .. 
 291              // too bad we need to load the album before deleting it, but this method
 292              // won't get called all to often anyway :)
 293              $album = $this->getAlbum( $albumId );
 294              if( empty( $album ) )
 295                  return false;
 296                  
 297              if( $userId > -1 )
 298                  if( $album->getOwnerId() != $userId )
 299                      return false;
 300                      
 301              $this->delete( "id", $albumId );
 302              $this->_cache->removeData( $album->getOwnerId(), CACHE_USERALBUMS );
 303              $this->_cache->removeData( $album->getOwnerId(), CACHE_USERALBUMS_NESTED );            
 304              //$this->delete( "parent_id", $album->getId());
 305              
 306              // update the counters
 307              $parent = $album->getParent();
 308              if( $parent ) {
 309                  $parent->setNumChildren( $parent->getNumChildren() - 1 );
 310                  $this->updateAlbum( $parent );
 311              }
 312              
 313              return( true );
 314          }
 315  
 316          /**
 317           * Removes all the albums from the given ownerId
 318           *
 319           * @param ownerId The blog identifier
 320           */
 321          function deleteUserAlbums( $ownerId )
 322          {
 323              $userAlbums = $this->getUserAlbums( $ownerId, false, -1);
 324  
 325              // remove resources belong to the owner one by one
 326              foreach( $userAlbums as $album ) {
 327                  $this->deleteAlbum( $album->getId(), $album->getOwnerId() );
 328              }
 329  
 330              return true;
 331          }
 332  
 333          /**
 334           * returns all the albums of the blog in an array. The key of the array is the
 335           * parent id of all the albums in the position, and each position is either an
 336           * array with all the albums that share the same parent id or empty if none
 337           *
 338           * @param userId 
 339           * @param albumId - unused
 340           * @return An associative array
 341           */
 342  		function getUserAlbumsGroupedByParentId( $userId, $albumId = 0 )
 343          {
 344              $userAlbums = $this->getUserAlbums( $userId );                        
 345              $albums = Array();
 346   
 347              if( $userAlbums ) {
 348                  foreach( $userAlbums as $album ) {
 349                      $key = $album->getParentId();
 350                      if( isset($albums["$key"] )) {
 351                          if( $albums["$key"] == "" )
 352                              $albums["$key"] = Array();
 353                      }
 354                      $albums["$key"][] = $album;                 
 355                      $ids[] = $album->getId();
 356                  }
 357              }
 358              return $albums;
 359          }
 360          
 361          /**
 362           * returns the albums of a blog ordered in the same way as they are nested. Please use
 363           * $album->getValue("level") to check their proper nesting.
 364           *
 365           * @param userId
 366           */
 367  		function getNestedAlbumList( $userId )
 368          {
 369              $nestedAlbums = $this->_cache->getData( $userId, CACHE_USERALBUMS_NESTED );
 370              if( !$nestedAlbums ) {
 371                  // cache the data for later use... this is quite a costly operation so 
 372                  // it's probably worth caching it!
 373                  $albums = $this->getUserAlbumsGroupedByParentId( $userId );
 374                  $nestedAlbums = $this->_getNestedAlbumList( $albums );
 375                  $this->_cache->setData( $userId, CACHE_USERALBUMS_NESTED, $nestedAlbums );
 376              }
 377              
 378              return $nestedAlbums;
 379          }
 380          
 381          
 382          /** 
 383           * @static
 384           * @private
 385           */
 386  		function _getNestedAlbumList( $albums, $start = 0, $level = -1 ) 
 387          {
 388              $level++;
 389              if( !array_key_exists( $start, $albums ) || $albums["$start"] == "" )
 390                  return Array();                
 391                  
 392              foreach( $albums["$start"] as $album ) {
 393                  // do the replacing
 394                  $album->setValue( "level", $level );
 395                  $results[] = $album;
 396                  
 397                  // make a recursive call                
 398                  $tmp = $this->_getNestedAlbumList( $albums, $album->getId(), $level);
 399                  foreach( $tmp as $tmpAlbum )
 400                      $results[] = $tmpAlbum;
 401              }
 402              
 403              return $results;
 404          }
 405          
 406          /**
 407           * @see Model::getSearchConditions()
 408           */
 409  		function getSearchConditions( $searchTerms )
 410          {
 411              $db =& Db::getDb();
 412              if( $db->isFullTextSupported()) {            
 413                  $query = "MATCH(normalized_name,normalized_description) AGAINST ('".Db::qstr($searchTerms)."')";
 414              }
 415              else {
 416                  $query = "name LIKE '%".Db::qstr( $searchTerms )."%' OR normalized_description LIKE '%".Db::qstr( $searchTerms )."%'";
 417              }
 418              
 419              return( $query );
 420          }
 421          
 422          /**
 423           * @see Model::mapRow()
 424           * @private
 425           */
 426  		function mapRow( $row )
 427          {
 428              $album = new GalleryAlbum( $row["owner_id"],
 429                                         $row["name"],
 430                                         $row["description"],
 431                                         $row["flags"],
 432                                         $row["parent_id"],
 433                                         $row["date"],
 434                                         unserialize($row["properties"]),
 435                                         $row["show_album"],
 436                                         $row["id"] );
 437              $album->setNumResources( $row['num_resources'] );
 438              $album->setNumChildren( $row['num_children'] );
 439              $album->setMangledName( $row['mangled_name'] );
 440              
 441              return $album;
 442          }
 443      }
 444  ?>


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