<?PHP
/************************************************************************/
/* هذا البرنامج من تصميم قهوة نت                                        */
/* ===========================                                          */
/*                                                                      */
/* http://www.coffee4arab.com                                           */
/* coffee4arab@yahoo.com                                                */
/* ver: 1.6                                                             */
/************************************************************************/



if ( !defined('ADMIN_FILE') )
{
	die("Illegal File Access");
}
global $prefix, $db, $admin_file;
$aid = substr("$aid", 0,25);
$row = $db->sql_fetchrow($db->sql_query("SELECT title, admins FROM ".$prefix."_modules WHERE title='Mygroups'"));
$row2 = $db->sql_fetchrow($db->sql_query("SELECT name, radminsuper FROM ".$prefix."_authors WHERE aid='$aid'"));
$admins = explode(",", $row['admins']);
$auth_user = 0;
for ($i=0; $i < sizeof($admins); $i++) {
    if ($row2['name'] == "$admins[$i]" AND $row['admins'] != "") {
        $auth_user = 1;	
    }
}

if ($row2['radminsuper'] == 1 || $auth_user == 1) {// ###########################

$tablename = $prefix . "_mygroups";
$pagetitle = "المجموعات";

function viewlist() {

	global $tablename,$dbi,$pagetitle,$picdir,$prefix,$admin_file;

	include("header.php");
    GraphicAdmin();

	$result = sql_query("select id, groupname from ". $tablename ."",$dbi);

	if (sql_num_rows($result,$dbi)>0) {
	    OpenTable();
		echo "<TABLE width=93% border=1 celladding=0 cellspacing=0>"
		 ."<tr><td colspan=3 valign=top align=center><h2><font face=\"simplified arabic\">$pagetitle</font></h2></td></tr>"
		."<tr  bgcolor=\"#CCCCCC\"><td valign=top align=center><font color=white>اسم المجموعة</font></td>"
	    ."<td align=center><font color=white>تعديل المواضيع</font></td></tr>";
	
		while (list($id, $groupname) = sql_fetch_row($result,$dbi)) {
		    echo "<tr><td align=right>$groupname</td>";
	
		    echo "<td align=center><a href=\"".$admin_file.".php?op=groupscontEdit&id=$id\">تعديل المواضيع</a></td></tr>";
	
		}
		echo "</table>";
		sql_free_result($result,$dbi);
		CloseTable ();
	}

	echo "<br>";
	OpenTable();
	echo "<center><b>
	أضافة مجموعة جديدة</b></center><br>
    <form action=\"".$admin_file.".php\" method=post>
    <table border=0 width=100%><tr><td>
    اسم المجموعة</td><td><input type=text name=articletitle size=40 maxlength=100></td></tr>
	</table>
    <input type=hidden name=op value=groupscontAdd>
    <input type=submit value=\"أضافة\">";
	echo "</form>";
	CloseTable ();

	echo "<br>";

	$result = sql_query("select id, groupname from ". $tablename ."",$dbi);
	if (sql_num_rows($result,$dbi)>0) {
		$result1 = sql_query("select topicid, topictext from ". $prefix ."_topics",$dbi);
			if (sql_num_rows($result1,$dbi)>0) {
			    OpenTable();
				echo "<b><center>اضافة مواضيع الى مجموعة</center></b>";
				echo "<form action=\"".$admin_file.".php\">\n";
				$glist = "اسم المجموعة\n";
				$glist .= "<br><select name=\"groupid\">\n";
				while (list ($id,$groupname) = sql_fetch_row ($result,$dbi)){
					$glist .= "<option value=\"$id\">$groupname</option>\n";
				}

				$glist .= "</select><br>";
				echo $glist;
				echo "اسم الموضوع\n"
					 ."<br><select name=\"thetopicid\">\n";
				while (list($topicid,$topictext) = sql_fetch_row ($result1,$dbi)){
					echo "<option value=\"$topicid\">$topictext</option>\n";
				}
				echo "</select>";
				echo "<input type=\"hidden\" name=\"op\" value=\"groupscontAddtopic\">\n"
					."<input type=\"submit\" value=\"اضافة\">\n";
				echo "</form>";
				CloseTable ();
			}

			echo "<br>";
		// edit or delete group 
		    OpenTable();
			echo "<b><center>حذف مجموعة</center></b>";
			echo "<form action=\"".$admin_file.".php\">\n";
			echo $glist;
			echo "<input type=\"hidden\" value=\"groupsEditGroupName\" name=\"op\">"
				."<input type=\"submit\" value=\"تعديل\">\n"
				."</form>";
			CloseTable ();
	}




include ("footer.php");

}


function groupscontAdd($articletitle) {

	global $tablename,$dbi,$admin_file;

	if ($articletitle=="") {
	    include ("header.php");
	    GraphicAdmin($hlpfile);
		OpenTable ();
		echo "لم تدخل اسم المجموعة<br>";
		CloseTable();
		exit;
	}
	sql_query("INSERT INTO " . $tablename . " VALUES ('','$articletitle','')",$dbi);
	Header("Location: ".$admin_file.".php?op=groupscontcont");

}


function groupscontEdit($id) {

	global $picdir,$tablename,$dbi;

    include ("header.php");
    GraphicAdmin($hlpfile);
    $result = sql_query("select groupname,topics from " . $tablename . " where id='$id'",$dbi);
    list($groupname, $topics) = mysql_fetch_row($result);

	if ($topics == "") {
		OpenTable ();
		echo "الموضوع: <b>$groupname</b><br>";
		echo "لا يوجد مواضيع";
		CloseTable ();
		include ("footer.php");
		exit;
	}

	$topiclist = explode ("|",$topics);


    OpenTable();
    echo "
    <center><font size=4><b>حذف مواضيع</b></font></center>
    الموضوع: <b>$groupname</b><br>";

	foreach ($topiclist as $value) {
	    $topicres = mysql_query ("select topictext from " . $prefix . "_topics where topicid=$value");
		list ($topicname) = mysql_fetch_row ($topicres);
		echo "<li>$topicname (<a href=\"".$admin_file.".php?op=groupsDeleteTopic&amp;topicid=$value&amp;groupid=$id\">حذف</a>)<br>";
	}

	CloseTable ();
    include("footer.php");

}

function groupsDeleteTopic ($groupid,$topicid) {
	global $tablename,$dbi,$admin_file;
	$result = mysql_query ("select topics from " . $tablename . " where id=$groupid ");
	if (mysql_num_rows ($result)>0) {
		list ($topics) = mysql_fetch_row ($result);
		$topiclist = explode ("|",$topics);
		foreach ($topiclist as $itsvalue) {
			if ($itsvalue != $topicid) $newlist .= $itsvalue . "|";
		}
		$newlist = substr($newlist, 0, strlen($newlist)-1);
		mysql_query ("update " . $tablename . " set topics='$newlist' where id=$groupid");
	}
	Header("Location: ".$admin_file.".php?op=groupscontEdit&id=$groupid");

}



function groupscontAddtopic ($groupid, $thetopicid) {
	global $picdir,$tablename,$dbi,$admin_file;
	$result = mysql_query ("select topics from " . $tablename . " where id=$groupid ");
	if (mysql_num_rows ($result)>0) {
		list ($topics) = mysql_fetch_row ($result);
		$topiclist = explode ("|",$topics);
		foreach ($topiclist as $itsvalue) {
			if ($itsvalue == $thetopicid) {
			    include ("header.php");
			    GraphicAdmin($hlpfile);
				OpenTable ();
				echo "هذا الموضوع موجود مسبقا";
				CloseTable ();
				include ("footer.php");
				exit;
			}
		}
		// it is not there so add it
		if ($topics == "") {
			$topics .= "$thetopicid";
		} else {
			$topics .= "|$thetopicid";
		}
		mysql_query ("update " . $tablename . " set topics='$topics' where id=$groupid");
	}
	Header("Location: ".$admin_file.".php?op=groupscontcont");
}


function groupsEditGroupName ($groupid) {
	global $picdir,$tablename,$dbi,$admin_file;
    include ("header.php");
    GraphicAdmin($hlpfile);
	OpenTable ();
	$result = mysql_query ("select groupname from " . $tablename . " where id=$groupid ");
	if (mysql_num_rows ($result)>0) {
		list ($groupname) = mysql_fetch_row ($result);
		echo "<center>تعديل مجموعة</center><br>"
			."<form action=\"".$admin_file.".php\" method=\"post\">\n"
			."اسم المجموعة : <input name=\"groupname\" value=\"$groupname\">\n"
			."<a href=\"".$admin_file.".php?op=groupsDeleteGroup&amp;groupid=$groupid\"> حذف </a><br>"
			."<input type=\"hidden\" value=\"groupsDoEditGroupName\" name=\"op\">"
			."<input type=\"hidden\" value=\"$groupid\" name=\"groupid\">"
			."<input type=\"submit\" value=\"تعديل\">"
			."</form>";
	} else {
		echo "هذه المجموعة غير موجودة";
	}
	CloseTable ();
}

function groupsDoEditGroupName ($groupname, $groupid) {
	global $tablename,$dbi,$admin_file;
	mysql_query ("update ". $tablename . " set groupname='$groupname' where id=$groupid");
	Header("Location: ".$admin_file.".php?op=groupscontcont");
}	


function groupsDeleteGroup ($groupid) {
	global $tablename,$dbi,$admin_file;
	mysql_query ("delete from ". $tablename . " where id=$groupid");
	Header("Location: ".$admin_file.".php?op=groupscontcont");
}



switch ($op) {

	case "groupscontcont":
			viewlist();
			break;

	case "groupscontAdd":
			groupscontAdd($articletitle);
			break;

	case "groupscontEdit":
			groupscontEdit($id);
			break;

	case "groupsDeleteTopic":
			groupsDeleteTopic ($groupid,$topicid);
			break;

	case "groupscontAddtopic":
			groupscontAddtopic ($groupid, $thetopicid);
			break;

	case "groupsEditGroupName":
			groupsEditGroupName ($groupid);
			break;

	case "groupsDoEditGroupName":
			groupsDoEditGroupName ($groupname, $groupid);
			break;

	case "groupsDeleteGroup":
			groupsDeleteGroup ($groupid);
			break;
}


} else {
    echo "Access Denied";
}
?>
