[ 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/ -> articlenotifications.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/articlenotification.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/template/templateservice.class.php" );
   7  
   8      define( "EMAILNOTIFIER_TEMPLATE", "email_notifier" );
   9  
  10      /**
  11       * \ingroup DAO
  12       * Accesses the database to create and fetch article notifications.
  13       */
  14      class ArticleNotifications extends Model 
  15      {
  16          /**
  17           * Constructor. Does nothing yet.
  18           */
  19      	function ArticleNotifications()
  20          {
  21              $this->Model();
  22  
  23              $this->table = $this->getPrefix()."articles_notifications";
  24          }
  25  
  26          /**
  27           * Returns all the notifications for a given article from a given blog.
  28           *
  29           * @param artId Article identifier.
  30           * @param blogId Blog identifier
  31           * @return An array of ArticleNotification objects containing information about all the
  32           * notifications for the given article.
  33           */
  34          function getArticleNotifications( $artId, $blogId )
  35          {
  36              $query = "SELECT * FROM ".$this->getPrefix()."articles_notifications WHERE article_id = ".$artId." AND blog_id = ".$blogId.";";
  37  
  38              $result = $this->Execute( $query );
  39  
  40              if( !$result )
  41                  return false;
  42  
  43              $notifications = Array();
  44              while( $row = $result->FetchRow()) {
  45                  array_push( $notifications, $this->mapRow( $row ));
  46              }
  47              $result->Close();            
  48  
  49              return $notifications;
  50          }
  51  
  52          /**
  53           * Internal function
  54           */
  55          function mapRow( $query_result )
  56          {
  57              $notification = new ArticleNotification( $query_result["user_id"],
  58                                                       $query_result["blog_id"],
  59                                                       $query_result["article_id"],
  60                                                       $query_result["id"] );
  61  
  62              return $notification;
  63           }
  64  
  65           /**
  66            * Notifies a user of a new comment in an article.
  67            *
  68            * @param notification The ArticleNotification object.
  69            * @param userInfo An UserInfo object with information about the user (we
  70            * mainly will need the email address!)
  71            * @param subject Subject of the message that will be sent to the user.
  72            * @param body Message that will be sent to the user.
  73            * @param charset the encoding that will be used in the message (it should be based
  74            * on the locale of the blog who is sending this message) It defaults to iso-8859-1
  75            * @return Returns true if the user was correctly notified or false otherwise.
  76            */
  77           function notifyUser( $notification, $userInfo, $subject, $body, $locale )
  78           {
  79              lt_include( PLOG_CLASS_PATH."class/mail/emailservice.class.php" );
  80              lt_include( PLOG_CLASS_PATH."class/mail/emailmessage.class.php" );
  81              lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
  82  
  83              $config =& Config::getConfig();
  84              $message = new EmailMessage();
  85              $message->setFrom( $config->getValue( "post_notification_source_address" ));
  86              $message->addTo( $userInfo->getEmail());
  87              $message->setSubject( $locale->tr("notification_subject" ) );
  88              $message->setBody( $body );
  89              $message->setCharset( $locale->getCharset());
  90  
  91              $service = new EmailService();
  92              return $service->sendMessage( $message );
  93           }
  94  
  95           /**
  96            * Notifies all the users of new comments in a post
  97            *
  98            * @param postId The post we want to check if there are notifications
  99            * @param blogId Just in case, the blog to which that post belongs.
 100            */
 101           function notifyUsers( $postId, $blogInfo)
 102           {
 103              lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
 104              
 105              
 106               $blogId = $blogInfo->getId();
 107               $artNotifs = $this->getArticleNotifications( $postId, $blogId );
 108  
 109              if( empty($artNotifs))
 110                  return;
 111  
 112              $articles = new Articles();
 113              $article  = $articles->getArticle( $postId );
 114              
 115              // get the correct character set
 116              $blogLocale =& $blogInfo->getLocale();
 117  
 118              $users = new Users();
 119              foreach( $artNotifs as $notif ) {
 120                  $userInfo = $users->getUserInfoFromId( $notif->getUserId());
 121                  $message = $this->renderMessageTemplate( $article, $blogInfo );
 122                  $this->notifyUser( $notif, $userInfo, $blogLocale->tr("notification_subject" ), $message, $blogLocale );
 123              }
 124           }
 125  
 126           /**
 127            * Renders the template at templates/misc/email_notifier.template
 128            */
 129           function renderMessageTemplate( $article, $blogInfo )
 130           {
 131               // create a new template service
 132               $ts = new TemplateService();
 133              $messageTemplate = $ts->Template( EMAILNOTIFIER_TEMPLATE, "misc" );
 134              // add these two useful objects
 135              $rg = $blogInfo->getBlogRequestGenerator();
 136              // disable the xhtml mode, as some email clients cannot deal with it
 137              $rg->setXHTML( false );
 138              $messageTemplate->assign( "url", $rg );
 139              $messageTemplate->assign( "post", $article );
 140              // render and return the contents
 141              return $messageTemplate->fetch();
 142           }
 143  
 144           /**
 145            * Adds a new notification to the database.
 146            * <b>NOTE:</b> It doesn't check if there is already a notification for that post, blog and user!!
 147            * Should we implement that checking it here or simply by making the primary key to include the three
 148            * values?
 149            *
 150            * @param artId Article
 151            * @param userId User identifier
 152            * @param blogId Blog identifier
 153            * @return Returns true if all correct or false otherwise.
 154            */
 155           function addNotification( $artId, $blogId, $userId )
 156           {
 157               $query = "INSERT INTO ".$this->getPrefix()."articles_notifications ( article_id, user_id, blog_id ) VALUES ( ".$artId.",".$userId.",".$blogId.");";
 158  
 159              $result = $this->Execute( $query );
 160  
 161              return $result;
 162           }
 163  
 164           /**
 165            * Removes a notification from the database.
 166            *
 167            * @param artId Article
 168            * @param blogId Blog identifier
 169            * @param userId User identifier
 170            * @return True if all correct or false otherwise.
 171            */
 172           function deleteNotification( $artId, $blogId, $userId )
 173           {
 174               $query = "DELETE FROM ".$this->getPrefix()."articles_notifications WHERE article_id = ".$artId." AND user_id = ".$userId." AND blog_id = ".$blogId.";";
 175  
 176              $result = $this->Execute( $query );
 177  
 178              return $result;
 179           }
 180           
 181  
 182           /**
 183            * Sends a weblogsUpdate.ping xmlrpc call to notifiy of changes in this blog
 184            *
 185            * @param blogInfo The BlogInfo object containing information about the blog
 186            * @return Returns true if successful or false otherwise.
 187            */
 188           function updateNotify( $blogInfo )
 189           {
 190              // source classes
 191              lt_include( PLOG_CLASS_PATH."class/net/xmlrpcclient.class.php" );
 192              lt_include( PLOG_CLASS_PATH . 'class/config/config.class.php' );            
 193  
 194              // if this feature is not enabled, we quit
 195              $config =& Config::getConfig();
 196              if( !$config->getValue( "xmlrpc_ping_enabled" ))
 197                  return;
 198  
 199              // get the array which contains the hosts
 200              $hosts = $config->getValue( "xmlrpc_ping_hosts" );
 201  
 202              // if it is not an array, quit
 203              if( !is_array($hosts))
 204                  return;
 205  
 206              // if no hosts, we can quit too
 207              if( empty($hosts))
 208                  return;
 209  
 210              // otherwise, we continue
 211              $xmlrpcPingResult = Array();
 212              $rg = $blogInfo->getBlogRequestGenerator();
 213              $blogLink = $rg->blogLink();
 214              foreach( $hosts as $host ) {
 215                  $client = new XmlRpcClient( $host );
 216                  $result = $client->ping( $blogInfo->getBlog(), $blogLink);
 217                  //print("result = ".$result. "is Error = ".$client->isError()." message: ".$client->getErrorMessage()."<br/>");
 218                  //$xmlrpcPingResult[$result["host"]
 219                  $xmlrpcPingResult=array_merge($xmlrpcPingResult, $result);
 220              }
 221  
 222              return $xmlrpcPingResult;
 223           }         
 224  
 225           /**
 226            * Returns the notifications but only for a particular user, a particular blog and
 227            * a particular blog.
 228            *
 229            * @param artId The article identifier.
 230            * @param blogId The blog identifier.
 231            * @param userId The user identifier.
 232            * @return The ArticleNotification object with the information or false otherwise.
 233            */
 234           function getUserArticleNotification( $artId, $blogId, $userId )
 235           {
 236               $query = "SELECT * FROM ".$this->getPrefix()."articles_notifications WHERE article_id = ".$artId." AND blog_id = ".$blogId." AND user_id = ".$userId.";";
 237  
 238              $result = $this->Execute( $query );
 239  
 240              if( !$result )
 241                  return false;
 242  
 243              if( $result->RecordCount() == 0 ){
 244                  $result->Close();            
 245                  return false;
 246              }
 247  
 248              $notification = $this->mapRow( $result->fetchRow());
 249              $result->Close();            
 250  
 251              return $notification;
 252           }
 253  
 254          /**
 255           * Deletes all notifications from the given blog
 256           *
 257           * @param blogId
 258           */
 259  		function deleteBlogNotifications( $blogId )
 260          {            
 261              return( $this->delete( "blog_id", $blogId ));
 262          }
 263       }
 264  ?>


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