<?php 
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.
*/
# ***************************************
# Display portal
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************
$not_access=1;
$granted="I";

$id = (isset($_GET['id']))      ?       $_GET['id']       :       0;
$group = $_GET['group'];
require_once('includes_mobile.php');
require_once('../../../../db_layer/'.__DBTYPE.'/portal.php');
require_once('../includes/template.inc.php');
require_once('../../../../db_layer/'.__DBTYPE.'/enterprise.php');
require_once('../../../../includes/mail.inc.php');

if ( (isset($_POST['group'])) ) {
    $desc = $_POST['notes'];
    $id = $_POST['friend'];
    
    // get friend Email
    $DB->getResults($get_user_info, $DB->escape($id));
    $row = $DB->fetch(0);
    $friendEmail = $row['email'];
    $friendName = $row['long_name'];
    $DB->freeResults();
    
    // create link and send email
    if (__NETWORK_TYPE == 'R') {
        //add waiting user link
        $DB->execute($scrnetworkadduser_addUser,
                        $DB->escape($_SESSION['user_id']),
                        $DB->escape($id),
                        $DB->noHTML($desc),
                        $DB->quote('W')
        );
        //add invitation
        $DB->execute($scrnetworkadduser_addUser,
                        $DB->escape($id),
                        $DB->escape($_SESSION['user_id']),
                        "''",
                        $DB->quote('I')
        );
        $notif_subject = lg('notificationNetworkInvitation',$_SESSION['longname'],__APPNAME);
        $notif_message = lg('notificationNetworkInvitationBody',$_SESSION['longname'],__APPNAME,__APPNAME,__LOCALFOLDER);
    }
    else {
        //add new user in network
        $DB->execute($scrnetworkadduser_addUser,
                        $DB->escape($_SESSION['user_id']),
                        $DB->escape($id),
                        $DB->noHTML($desc),
                        $DB->quote('O')
        );
        $notif_subject = lg('notificationFollowing',$friendName,__APPNAME);
        $notif_message = lg('notificationFollowingBody',$friendName,__APPNAME,__APPNAME,__LOCALFOLDER);
    }
    //create an alert about this user adding
    $DB->execute($scrAlertAdd,
                    $DB->escape($id),
                    '1',
                    $DB->escape($_SESSION['user_id']),
                    $DB->quote($_SESSION['longname'])
    );
    
    // send email notification / invitation
    $s_mail = new mail();
    $s_mail->addSender(__SUPPORTEMAIL);
    $s_mail->addSubject($notif_subject);
    $s_mail->addMessage($notif_message);
    $s_mail->configArray($friendEmail,'2');
    $s_mail->sendMail();    

	//add keywords linked to user
	if ($_POST["kw"]!="") {
		$keyword=explode(",",$kw);
		$keywordSimplified=explode(",",$kwformated);
		for ($i=0;$i<count($keyword);$i++)
		{
			$selkw=$keywordSimplified[$i];
			$DB->getResults($scrnetworkadduser_getKeyword,$DB->noHTML($selkw));
			if ($DB->nbResults()==0) {
				$DB->execute($scrnetworkadduser_addNewKeyword,$DB->noHTML($keyword[$i]),$DB->noHTML($selkw));
				$kwid=$DB->getId();
			}
			else {
				$row = $DB->fetch(0);
				$kwid = $row["id"];
			}
			$DB->freeResults();
			$DB->execute($scrnetworkadduser_insertKeyword,$DB->escape($_SESSION['user_id']),$DB->escape($id),$kwid);
		}
	}
    header('Location: group_members.php?id='.$_POST['group']);
    exit;
}

$selectedTab = '';
if (isset($_GET['tab']) && !empty($_GET['tab'])) {
	$selectedTab = $_GET['tab'];
}

$DB->getResults($mobNotebookArticle_getUserInfo,$DB->escape($_SESSION['user_id']),$DB->escape($id));
$row = $DB->fetch(0);
$pic_user = $row['picture'];
$name_user = $row['long_name'];
$stat_user = $row['stat'];
$isfriend = $row['friend_id'];
$DB->freeResults();

$options = defineHeader();
    
echo page_begining();

echo '<div id="header">';
    echo generate_header($options,$message);
echo '</div>';

echo '<div id="wrapper">';
echo '<div id="page">';		

echo '<div class="page_title"><span>'.lg('addToYourNetwork').'</span></div>';

echo '<div class="users">
        <div class="title">
            <img src="../../../'.displayPicture($row['picture']).'" class="avatar" width="50" height="50" style="float: left;margin-right: 5px;">
            <span>'.$row['long_name'].' '.$row['firstname'].'</span>
        </div>';
            if ($row['stat']!='') echo '<div class="subtitle">'.$row['stat'].'</div>';  
echo '</div>';

echo '<form method="post" action="network_add_user.php">
        <br /><span<b>'.lg("tags").' ('.lg("comaSeparated").')</b></span><br />
        <input type="text" name="kw" style="width: 90%"><br /><br />
        <span><b>'.lg("personalNotes").'</b></span><br />
        <textarea name="notes" rows="10" style="width: 90%"></textarea><br /><br />
        <input type="hidden" name="group" value="'.$group.'">
        <input type="hidden" name="friend" value="'.$id.'">
        <center>
            <input type="submit" class="minibtn" value="'.lg("add").'" />&nbsp;&nbsp;
            <a href="group_members.php?id='.$group.'" name="back"><input type="button" class="minibtn" value="'.lg("cancel").'"></a>
        </center>
	  </form>';

echo "</div>";
echo "</div>";

echo generate_footer("network");
?>