<?php

    /**
 * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
 * CC-BY License - http://creativecommons.org/licenses/by/3.0/
 */

    require_once(BX_DIRECTORY_PATH_CLASSES . 'BxDolAlerts.php');

    class BxPommoResponseProfiles extends BxDolAlertsResponse
    {
        var $_oModule;

        function BxPommoResponseProfiles($oModule)
        {
            parent::BxDolAlertsResponse();

            $this->_oModule = $oModule;
        }

        function response(&$oAlert)
        {
            $iSenderId = $oAlert->iSender;

            $iRecipientId = $oAlert->iObject;
            $sRecipientName = getNickName($iRecipientId);
            $sRecipientLink = getProfileLink($iRecipientId);
            $sRecipientEmail = db_value("SELECT Email FROM `Profiles` WHERE `ID`='$iRecipientId'");
            $sAddToPommo = db_value("SELECT EmailNotify FROM `Profiles` WHERE `ID`='$iRecipientId'");
           
            $iCommentId = 0;
            $aParams = array();
            switch($oAlert->sUnit . '_' .$oAlert->sAction ) {
                
                case 'profile_join' :
                
                 if ($sAddToPommo == "1")
                   {
                   mysql_query("INSERT INTO `pommo_subscribers` VALUES('$iRecipientId', '$sRecipientEmail', '', '', '0','000-000-000', '1')");
                   } 
                   else
                   {
                   mysql_query("INSERT INTO `pommo_subscribers` VALUES('$iRecipientId', '$sRecipientEmail', '', '', '0','000-000-000', '0')");
                   } 
                 $iSenderId = $oAlert -> iObject;
                 $iRecipientId = 0;
                 break;

                case 'profile_edit' :
                   mysql_query("UPDATE `pommo_subscribers` SET `email` = '$sRecipientEmail' WHERE `subscriber_id` = '$iRecipientId'");
                 if ($sAddToPommo == "1")
                   {
                   mysql_query("UPDATE `pommo_subscribers` SET `status` = '1' WHERE `subscriber_id` = '$iRecipientId'");
                   } 
                   else
                   {
                   mysql_query("UPDATE `pommo_subscribers` SET `status` = '0' WHERE `subscriber_id` = '$iRecipientId'");
                   }
                    $iRecipientId = 0;
                    break;


                case 'profile_delete':
                
                   mysql_query("DELETE FROM `pommo_subscribers` WHERE `subscriber_id` = '$iRecipientId'");
                             
                    break;

            }

            if(empty($aParams))
                return;

            // create new activity;
            $aParams['pommo_type'] = 'profiles_activity';

            $iActivityId = 0;
            if($iSenderId || (!$iSenderId && $this->_oModule->_oConfig->bTrackGuestsActivites))
                $iActivityId = $this->_oModule->_oDb->createActivity(
                    $oAlert->sUnit,
                    $oAlert->sAction,
                    $oAlert->iObject,
                    $iCommentId,
                    $iSenderId,
                    $iRecipientId,
                    $aParams
                );

            if(!$iActivityId)
                return;

            // try to define all profile's friends;
            $aFriends = getMyFriendsEx($iSenderId);
            if(empty($aFriends) || !is_array($aFriends))
                return;

            // attach activity to friend;
            foreach($aFriends as $iFriendId => $aItems)
                $this->_oModule -> _oDb -> attachFriendEvent($iActivityId, $iSenderId, $iFriendId);
        }

        function _getSenderInfo($iSenderId)
        {
            $sSenderNickName = getNickName($iSenderId);
            $sSenderLink = $sSenderNickName ? getProfileLink($iSenderId) : 'javascript:void(0)';
            if(!$sSenderNickName)
                $sSenderNickName = _t('_Guest');

            $aRet = array(
                'NickName' => $sSenderNickName,
                'Link' => $sSenderLink,
            );

           return $aRet;
        }
    }
