Guest User

Untitled

a guest
Dec 11th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.61 KB | None | 0 0
  1. <?php
  2.  
  3. class tamagochi_class_reputation_cache extends class_reputation_cache {
  4.     public function addRate( $type, $type_id, $rating, $message='', $member_id=0, $app='' )
  5.     {
  6.         $this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_global' ), 'core' );
  7.        
  8.         /* Online? */
  9.         if ( ! $this->rep_system_on )
  10.         {
  11.             $this->error_message = $this->lang->words['reputation_offline'];
  12.             return false;
  13.         }
  14.        
  15.         /* INIT */
  16.         $app       = ( $app ) ? $app : ipsRegistry::$current_application;
  17.         $rating    = intval( $rating );
  18.        
  19.         if ( ! $this->memberData['member_id'] )
  20.         {
  21.             $this->error_message = $this->lang->words['reputation_guest'];
  22.             return false;
  23.         }
  24.        
  25.         if ( $rating != -1 && $rating != 1 )
  26.         {
  27.             $this->error_message = $this->lang->words['reputation_invalid'];
  28.             return false;
  29.         }
  30.        
  31.         /* Check for existing rating */
  32.         $currentRating = $this->getCurrentMemberRating( array( 'app' => $app, 'type' => $type, 'id' => $type_id, 'memberId' => $this->memberData['member_id'] ) );
  33.        
  34.         /* Check the point types */
  35.         if ( $rating == -1 && IPSMember::canRepDown( $currentRating, $this->memberData ) === false )
  36.         {
  37.             $this->error_message = $this->lang->words['reputation_invalid'];
  38.             return false;
  39.         }
  40.        
  41.         if ( $rating == 1 && IPSMember::canRepUp( $currentRating, $this->memberData ) === false )
  42.         {
  43.             $this->error_message = $this->lang->words['reputation_invalid'];
  44.             return false;
  45.         }
  46.        
  47.         /* Day Cutoff */
  48.         $day_cutoff = time() - 86400;
  49.  
  50.         /* Check Max Positive Votes */
  51.         if( $rating == 1 )
  52.         {
  53.             if ( intval( $this->memberData['g_rep_max_positive'] ) === 0 )
  54.             {
  55.                 $this->error_message = $this->lang->words['reputation_quota_pos'];
  56.                 return false;              
  57.             }
  58.            
  59.             $total = $this->DB->buildAndFetch( array( 'select' => 'count(*) as votes',
  60.                                                       'from'   => 'reputation_index',
  61.                                                       'where'  => 'member_id=' . $this->memberData['member_id'] . ' AND rep_rating=1 AND rep_date > ' . $day_cutoff )   );
  62.                    
  63.             if ( $total['votes'] >= $this->memberData['g_rep_max_positive'] )
  64.             {
  65.                 $this->error_message = $this->lang->words['reputation_quota_pos'];
  66.                 return false;              
  67.             }
  68.             $this->DB->query('update members set `tamagochi` = `tamagochi` + 5');
  69.         }
  70.        
  71.         /* Check Max Negative Votes if not like mode */
  72.         if ( $rating == -1 AND ! $this->isLikeMode() )
  73.         {
  74.             if ( intval( $this->memberData['g_rep_max_negative'] ) === 0 )
  75.             {
  76.                 $this->error_message = $this->lang->words['reputation_quota_neg'];
  77.                 return false;              
  78.             }
  79.            
  80.             $total = $this->DB->buildAndFetch( array( 'select' => 'count(*) as votes',
  81.                                                       'from'   => 'reputation_index',
  82.                                                       'where'  => 'member_id=' . $this->memberData['member_id'] . ' AND rep_rating=-1 AND rep_date > ' . $day_cutoff )  );
  83.                                                    
  84.             if( $total['votes'] >= $this->memberData['g_rep_max_negative'] )
  85.             {
  86.                 $this->error_message = $this->lang->words['reputation_quota_neg'];
  87.                 return false;              
  88.             }
  89.         }      
  90.        
  91.         /* If no member id was passed in, we have to query it using the config file */
  92.         if( ! $member_id )
  93.         {
  94.             /* Reputation Config */
  95.             if( is_file( IPSLib::getAppDir( $app ) . '/extensions/reputation.php' ) )
  96.             {
  97.                 $rep_author_config = array();
  98.                 require( IPSLib::getAppDir( $app ) . '/extensions/reputation.php' );/*maybeLibHook*/
  99.             }
  100.             else
  101.             {
  102.                 $this->error_message = $this->lang->words['reputation_config'];
  103.                 return false;
  104.             }
  105.            
  106.             if( ! $rep_author_config[$type]['column'] || ! $rep_author_config[$type]['table'] )
  107.             {
  108.                 $this->error_message = $this->lang->words['reputation_config'];
  109.                 return false;
  110.             }
  111.            
  112.             $_col   = $rep_author_config[$type]['id_field'] ? $rep_author_config[$type]['id_field'] : $type;
  113.            
  114.             /* Query the content author */
  115.             $content_author = $this->DB->buildAndFetch( array( 'select' => "{$rep_author_config[$type]['column']} as id",
  116.                                                                'from'   => $rep_author_config[$type]['table'],
  117.                                                                'where'  => "{$_col}={$type_id}" )   );
  118.            
  119.             $member_id = $content_author['id'];
  120.         }
  121.        
  122.         if( ! ipsRegistry::$settings['reputation_can_self_vote'] && $member_id == $this->memberData['member_id'] )
  123.         {
  124.             $this->error_message = $this->lang->words['reputation_yourown'];
  125.             return false;
  126.         }
  127.        
  128.         /* Query the member group */
  129.         if( ipsRegistry::$settings['reputation_protected_groups'] )
  130.         {
  131.             $member_group = $this->DB->buildAndFetch( array( 'select' => 'member_group_id', 'from' => 'members', 'where' => "member_id={$member_id}" ) );
  132.            
  133.             if( in_array( $member_group['member_group_id'], explode( ',', ipsRegistry::$settings['reputation_protected_groups'] ) ) )
  134.             {
  135.                 $this->error_message = $this->lang->words['reputation_protected'];
  136.                 return false;          
  137.             }
  138.         }
  139.        
  140.         /* Build the insert array */
  141.         $db_insert = array( 'member_id'  => $this->memberData['member_id'],
  142.                             'app'        => $app,
  143.                             'type'       => $type,
  144.                             'type_id'    => $type_id,
  145.                             'rep_date'   => time(),
  146.                             'rep_msg'    => $message,
  147.                             'rep_rating' => $rating );                             
  148.        
  149.         /* Insert */
  150.         if ( $currentRating )
  151.         {
  152.             if ( $rating == -1 && $this->isLikeMode() )
  153.             {
  154.                 $this->DB->delete( 'reputation_index', "app='{$app}' AND type='{$type}' AND type_id={$type_id} AND member_id=".$this->memberData['member_id'] );
  155.                 $this->DB->query('update members set `tamagochi` = `tamagochi` - 5');
  156.             }
  157.         }
  158.         else
  159.         {
  160.             $this->DB->replace( 'reputation_index', $db_insert, array( 'app', 'type', 'type_id', 'member_id' ) );
  161.         }
  162.        
  163.         /* Update cache */
  164.         $this->updateCache( $app, $type, $type_id );
  165.  
  166.         /* Get authors current rep */
  167.         $author_points = $this->DB->buildAndFetch( array( 'select' => 'pp_reputation_points',
  168.                                                           'from'   => 'profile_portal',
  169.                                                           'where'  => "pp_member_id={$member_id}" )  );
  170.        
  171.         /* Figure out new rep */
  172.         if( $currentRating['rep_rating'] == -1 )
  173.         {
  174.             $author_points['pp_reputation_points'] += 1;
  175.         }
  176.         else if ( $currentRating['rep_rating'] == 1 )
  177.         {
  178.             $author_points['pp_reputation_points'] -= 1;
  179.         }
  180.        
  181.         /* now add on new rating if we're not like mode-ing */
  182.         if ( ( ! $this->isLikeMode() ) || ( empty( $currentRating['rep_rating'] ) && $this->isLikeMode() ) )
  183.         {
  184.             $author_points['pp_reputation_points'] += $rating;
  185.         }
  186.  
  187.         $this->DB->update( 'profile_portal', array( 'pp_reputation_points' => $author_points['pp_reputation_points'] ), "pp_member_id={$member_id}" );
  188.        
  189.         /* Notification */
  190.         if ( $rating == 1 && $this->isLikeMode() && $app == 'forums' && $type == 'pid' )
  191.         {
  192.             /* Check for class_forums */
  193.             if ( ! $this->registry->isClassLoaded( 'class_forums' ) )
  194.             {
  195.                 $classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'forums' ) . "/sources/classes/forums/class_forums.php", 'class_forums', 'forums' );
  196.                 $this->registry->setClass( 'class_forums', new $classToLoad( $this->registry ) );
  197.                 $this->registry->strip_invisible = 0;
  198.                 $this->registry->class_forums->forumsInit();
  199.             }
  200.        
  201.             $classToLoad   = IPSLib::loadLibrary( IPS_ROOT_PATH . '/sources/classes/member/notifications.php', 'notifications' );
  202.             $notifyLibrary = new $classToLoad( $this->registry );
  203.            
  204.             if ( ! $this->registry->isClassLoaded('topics') )
  205.             {
  206.                 $classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'forums' ) . "/sources/classes/topics.php", 'app_forums_classes_topics', 'forums' );
  207.                 $this->registry->setClass( 'topics', new $classToLoad( $this->registry ) );
  208.             }
  209.            
  210.             $post        = $this->registry->getClass('topics')->getPostById( $type_id );
  211.            
  212.             /* Set topic data */
  213.             $this->registry->getClass('topics')->setTopicData( $post );
  214.            
  215.             /* Quick check */
  216.             if ( ! $post['author_id'] OR $post['author_id'] == $this->memberData['member_id'] )
  217.             {
  218.                 return true;
  219.             }
  220.            
  221.             $_toMember   = IPSMember::load( $post['author_id'] );
  222.            
  223.             /* Set language */
  224.             $_toMember['language'] = $_toMember['language'] == "" ? IPSLib::getDefaultLanguage() : $_toMember['language'];
  225.            
  226.             /* Quick permission check */
  227.             if ( $this->registry->getClass('topics')->canView() !== true )
  228.             {
  229.                 return true;
  230.             }
  231.            
  232.             $url = $this->registry->output->buildSEOUrl( "showtopic={$post['topic_id']}&amp;view=findpost&amp;p={$post['pid']}", "publicNoSession", $post['title_seo'], 'showtopic' );
  233.            
  234.             IPSText::getTextClass('email')->getTemplate( "new_likes", $_toMember['language'] );
  235.        
  236.             IPSText::getTextClass('email')->buildMessage( array('MEMBER_NAME'   => $this->memberData['members_display_name'],
  237.                                                                 'SHORT_POST'    => IPSText::truncate( IPSText::getTextClass( 'bbcode' )->stripAllTags( $post['post'] ), 300 ),
  238.                                                                 'URL'           => $url ) );
  239.    
  240.             IPSText::getTextClass('email')->subject = sprintf(  IPSText::getTextClass('email')->subject,
  241.                                                                 $this->registry->output->buildSEOUrl( 'showuser=' . $this->memberData['member_id'], 'publicNoSession', $this->memberData['members_seo_name'], 'showuser' ),
  242.                                                                 $this->memberData['members_display_name'],
  243.                                                                 $url,
  244.                                                                 $this->registry->output->buildSEOUrl( "showtopic={$post['topic_id']}", "publicNoSession", $post['title_seo'], 'showtopic' ),
  245.                                                                 IPSText::truncate( $post['topic_title'], 30 ) );
  246.    
  247.             $notifyLibrary->setMember( $_toMember );
  248.             $notifyLibrary->setFrom( $this->memberData );
  249.             $notifyLibrary->setNotificationKey( 'new_likes' );
  250.             $notifyLibrary->setNotificationUrl( $url );
  251.             $notifyLibrary->setNotificationText( IPSText::getTextClass('email')->message );
  252.             $notifyLibrary->setNotificationTitle( IPSText::getTextClass('email')->subject );
  253.            
  254.             try
  255.             {
  256.                 $notifyLibrary->sendNotification();
  257.             }
  258.             catch( Exception $e ){}
  259.         }
  260.        
  261.         return true;       
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment