<?php
/**
 @licence GPL 2005-2009  The osCSS developers - osCSS Open Source E-commerce
  @package oscss-2 <www http://www.oscss.org>
  @version Rc-3
  @date  08/04/10, 22:41
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
   @encode UTF-8
*/

function status_flag($cond,$get,$ancre=''){
  if($cond) {
    return tep_image(DIR_WS_ICONS . 'icon_status_green.gif', IMAGE_ICON_STATUS_GREEN) . ' <a href="' . tep_href_link(FILENAME_ADMIN_MEMBERS, $get.'&flag=0' ) .$ancre. '">' . tep_image(DIR_WS_ICONS . 'icon_status_red_light.gif', IMAGE_ICON_STATUS_RED_LIGHT) . '</a>';
  } else {
    return '<a href="' . tep_href_link(FILENAME_ADMIN_MEMBERS, $get.'&flag=1') .$ancre. '">' . tep_image(DIR_WS_ICONS . 'icon_status_green_light.gif', IMAGE_ICON_STATUS_GREEN_LIGHT) . '</a> ' . tep_image(DIR_WS_ICONS . 'icon_status_red.gif', IMAGE_ICON_STATUS_RED);
  }
 }




    switch ($action) {
      case 'member_new':
        $check_email_query = tep_db_query("select admin_email_address from " . TABLE_ADMIN . "");
        while ($check_email = tep_db_fetch_array($check_email_query)) {
          $stored_email[] = $check_email['admin_email_address'];
        }

        if (in_array($_POST['admin_email_address'], $stored_email)) {
          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'page=' . $page .  '&error=email&action=new_member'));
        } else {
          function randomize() {
            $salt = "abchefghjkmnpqrstuvwxyz0123456789";
            srand((double)microtime()*1000000);
            $i = 0;
            while ($i <= 7) {
                $num = rand() % 33;
                $tmp = substr($salt, $num, 1);
                $pass = $pass . $tmp;
                $i++;
            }
            return $pass;
          }
          $makePassword = randomize();

          $sql_data_array = array('admin_groups_id' => tep_db_prepare_input($_POST['admin_groups_id']),
                                  'admin_firstname' => tep_db_prepare_input($_POST['admin_firstname']),
                                  'admin_lastname' => tep_db_prepare_input($_POST['admin_lastname']),
                                  'admin_email_address' => tep_db_prepare_input($_POST['admin_email_address']),
                                  'admin_password' => tep_encrypt_password($makePassword),
                                  'admin_created' => 'now()');

          tep_db_perform(TABLE_ADMIN, $sql_data_array);

          $myarray = array(
                           'email_use_html'=>_cst_bool('EMAIL_USE_HTML'),
                          'links'=>HTTP_SERVER . DIR_WS_ADMIN,
                          'admin_firstname'=>$_POST['admin_firstname'],
                          'admin_email_address'=>$_POST['admin_email_address'],
                          'hiddenPassword'=>$makePassword,
                          'message'=>''
                          );
          $message_final=tep_post_prepare_email('admin_members_new.tpl',$language,$myarray);

          tep_mail($_POST['admin_firstname'] , $_POST['admin_email_address'],EMAIL_TEXT_SUBJECT, $message_final, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
      // copy au porp mag
          tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS,'Creat new compte for admin', $message_final);

          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'page=' . $page ));
        }
        break;
      case 'member_edit':
        $admin_id = tep_db_prepare_input($_POST['admin_id']);
        $hiddenPassword = '-hidden-';
        $stored_email[] = 'NONE';

        $check_email_query = tep_db_query("select admin_email_address from " . TABLE_ADMIN . " where admin_id <> " . $admin_id . "");
        while ($check_email = tep_db_fetch_array($check_email_query)) {
          $stored_email[] = $check_email['admin_email_address'];
        }

        if (in_array($_POST['admin_email_address'], $stored_email)) {
          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'page=' . $page . 'mID=' . $_GET['mID'] . '&error=email&action=edit_member'));
        } else {
          $sql_data_array = array('admin_groups_id' => tep_db_prepare_input($_POST['admin_groups_id']),
                                  'admin_firstname' => tep_db_prepare_input($_POST['admin_firstname']),
                                  'admin_lastname' => tep_db_prepare_input($_POST['admin_lastname']),
                                  'admin_email_address' => tep_db_prepare_input($_POST['admin_email_address']),
                                  'admin_modified' => 'now()');

          tep_db_perform(TABLE_ADMIN, $sql_data_array, 'update', 'admin_id = \'' . $admin_id . '\'');

          $myarray = array(
//                           'email_use_html'=>_cst_bool('EMAIL_USE_HTML'),
                          'links'=>HTTP_SERVER . DIR_WS_ADMIN,
                          'admin_firstname'=>$_POST['admin_firstname'],
                          'admin_email_address'=>$_POST['admin_email_address'],
                          'hiddenPassword'=>$hiddenPassword,
                          'message'=>''
                          );
          $message_final=tep_post_prepare_email('admin_members.tpl',$language,$myarray);
          tep_mail($_POST['admin_firstname'] . ' ' . $_POST['admin_lastname'], $_POST['admin_email_address'],EMAIL_TEXT_SUBJECT, $message_final);
          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'page=' . $page . '&mID=' . $admin_id));
        }
        break;
      case 'member_delete':
        $admin_id = tep_db_prepare_input($_POST['admin_id']);
        tep_db_query("delete from " . TABLE_ADMIN . " where admin_id = '" . $admin_id . "'");

        tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'page=' . $page));
        break;
      case 'group_define':
        $selected_checkbox = $_POST['groups_to_boxes'];
        $for_grp=$_GET['gID'];
        $define_files_query = tep_db_query("select admin_files_id, admin_groups_id from " . TABLE_ADMIN_FILES . " order by admin_files_id");

        while ($define_files = tep_db_fetch_array($define_files_query)) {
          $admin_files_id= $define_files['admin_files_id'];
          $admin_groups_id = $define_files['admin_groups_id'];
          $gps_id=explode(',',$admin_groups_id);

            if (in_array ($admin_files_id, $selected_checkbox)){
            $chek=true;
//             echo "on pour:".$admin_files_id.' '.$_POST['checked_' . $admin_files_id]."<br />";
            } else  $chek=false;

            if ($chek)
              if (!in_array ($for_grp, $gps_id)) $final=preg_replace('/(^,)|(,,)/i',',',implode(',',$gps_id).','.$for_grp.',');
              else $final=preg_replace('/(^,)|(,,)/i',',',implode(',',$gps_id).',');

            if (!$chek)
              if (in_array ($for_grp, $gps_id)) {
//                 echo 'effacement:<br >';
                $id_gps=array_flip($gps_id);

                unset($id_gps[$for_grp]);
                $final=preg_replace('/(^,)|(,,)/i',',',implode(',',array_flip($id_gps)));
              }
              else $final=preg_replace('/(^,)|(,,)/i',',',implode(',',$gps_id).',');

//             echo $final.'<br >';
            $sql_data_array = array('admin_groups_id' => $final);


            tep_db_perform(TABLE_ADMIN_FILES, $sql_data_array, 'update', 'admin_files_id = \'' . tep_db_prepare_input($_POST['menu_id_' . $admin_files_id]) . '\'');
          }

        tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $_POST['admin_groups_id']));
        break;
      case 'group_delete':
        $set_groups_id = tep_db_prepare_input( $_GET['gID']);

        tep_db_query("delete from " . TABLE_ADMIN_GROUPS . " where admin_groups_id = '" . (int)$set_groups_id . "'");
//         tep_db_query("update  table " . TABLE_ADMIN_FILES . " change admin_groups_id set( " .  (int)$set_groups_id . " ) NOT NULL DEFAULT '1' ");
        tep_db_query("delete from " . TABLE_ADMIN . " where admin_groups_id = '" . (int) $set_groups_id . "'");

        tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=groups'));
        break;
      case 'group_edit':
        $admin_groups_name = ucwords(strtolower(tep_db_prepare_input($_POST['admin_groups_name'])));
        $name_replace = preg_replace ("/\40/", "%", $admin_groups_name);

        if (($admin_groups_name == '' || NULL) || (strlen($admin_groups_name) <= 5) ) {
          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $_GET[gID] . '&gName=false&action=action=edit_group'));
        } else {
          $check_groups_name_query = tep_db_query("select admin_groups_name as group_name_edit from " . TABLE_ADMIN_GROUPS . " where admin_groups_id <> " . $_GET['gID'] . " and admin_groups_name like '%" . $name_replace . "%'");
          $check_duplicate = tep_db_num_rows($check_groups_name_query);
          if ($check_duplicate > 0){
            tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $_GET['gID'] . '&gName=used&action=edit_group'));
          } else {
            $admin_groups_id = $_GET['gID'];
            tep_db_query("update " . TABLE_ADMIN_GROUPS . " set admin_groups_name = '" . $admin_groups_name . "' where admin_groups_id = '" . $admin_groups_id . "'");
            tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $admin_groups_id));
          }
        }
        break;
      case 'group_new':
        $admin_groups_name = ucwords(strtolower(tep_db_prepare_input($_POST['admin_groups_name'])));
        $name_replace = preg_replace ("/\40/", "%", $admin_groups_name);

        if (($admin_groups_name == '' || NULL) || (strlen($admin_groups_name) <= 5) ) {
          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $_GET[gID] . '&gName=false&action=new_group'));
        } else {
          $check_groups_name_query = tep_db_query("select admin_groups_name as group_name_new from " . TABLE_ADMIN_GROUPS . " where admin_groups_name like '%" . $name_replace . "%'");
          $check_duplicate = tep_db_num_rows($check_groups_name_query);
          if ($check_duplicate > 0){
            tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $_GET['gID'] . '&gName=used&action=new_group'));
          } else {
            $sql_data_array = array('admin_groups_name' => $admin_groups_name);
            tep_db_perform(TABLE_ADMIN_GROUPS, $sql_data_array);
            $admin_groups_id = tep_db_insert_id();
/*
            $set_groups_id = tep_db_prepare_input($_POST['set_groups_id']);
            $add_group_id = $set_groups_id . ',\'' . $admin_groups_id . '\'';
            tep_db_query("alter table " . TABLE_ADMIN_FILES . " change admin_groups_id admin_groups_id set( " . $add_group_id . ") NOT NULL DEFAULT '1' ");*/

            tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gID=' . $admin_groups_id));
          }
        }
        break;

        case 'setflag':

          if($_GET['gPath']==1)  tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gPath=' . $_GET['gPath'] .'&action=define_group'));
          if(isset($_GET['admin_boxes_id'])) {
            $ID=$_GET['admin_boxes_id'].'.php';
            $key='admin_files_name';
            $where= " and admin_files_is_boxes='1' ";
          } else {
            $ID=$_GET['admin_files_id'];
            $key='admin_files_id';
            $where=" and admin_files_is_boxes='0' ";
          }

            $define_files_query = tep_db_query($sql="select * from " . TABLE_ADMIN_FILES . " where ".$key."='".$ID."' ".$where." LIMIT 1");
            $define_files = tep_db_fetch_array($define_files_query);


        $t=explode(',',$define_files['admin_groups_id']);

        if($_GET['flag'] ==0) {
          foreach($t as $v){
            if($_GET['gPath'] != $v && !empty($v) )$ts[]=$v;
          }
        }
        elseif(!in_array($_GET['gPath'] ,$t)) {
          $ts=$t;
          $ts[]=$_GET['gPath'];
        }else $ts=$t;

        $define_files_query = tep_db_query($sql="update " . TABLE_ADMIN_FILES . " set admin_groups_id = '".implode(',',$ts)."' where admin_files_id='".$define_files['admin_files_id']."'");

          tep_redirect(tep_href_link(FILENAME_ADMIN_MEMBERS, 'gPath=' . $_GET['gPath'] .'&action=define_group'));
        break;
	default:
	if (isset($_GET['gPath'])) $titre_h3=HEADING_TITLE;
	elseif (isset($_GET['gID'])){
	  $titre_h3=HEADING_TITLE_GRP;

	  $db_groups_query = tep_db_query("select * from " . TABLE_ADMIN_GROUPS . " order by admin_groups_id");
	  $list_g=array();
	  $add_groups_prepare = '\'0\'' ;
	  $del_groups_prepare = '\'0\'' ;
	  $count_groups = 0;
	  while ($groups = tep_db_fetch_array($db_groups_query)) {
	    $add_groups_prepare .= ',\'' . $groups['admin_groups_id'] . '\'' ;
	    $count_groups++;
	    $class_s="";
	    if (((!$_GET['gID']) || ($_GET['gID'] == $groups['admin_groups_id']) || ($_GET['gID'] == 'groups')) && (!isset($gInfo)) ) {
	    $gInfo = new objectInfo($groups);

// 	    if ((is_object($gInfo)) && ($groups['admin_groups_id'] == $gInfo->admin_groups_id) ) $class_s=" Selected";
	    }
	    $list_g[]=$groups;
	  }


	}else {
	  $titre_h3=HEADING_TITLE_MEMBERS;

  	  $db_admin_query_raw = "select * from " . TABLE_ADMIN . " order by ".((isset($_GET['sort_by'])) ? $_GET['sort_by'] : 'admin_firstname ');
	  $db_admin_split = new splitPageResults($page, MAX_DISPLAY_SEARCH_RESULTS, $db_admin_query_raw, $db_admin_query_numrows);
	  $db_admin_query = tep_db_query($db_admin_query_raw);
	  $list_m=array();
	  while ($admin = tep_db_fetch_array($db_admin_query)) {
	    if ($admin['admin_groups_id']==0) $admin['admin_groups_id']=1;
	    $admin_group_query = tep_db_query("select admin_groups_name from " . TABLE_ADMIN_GROUPS . " where admin_groups_id = '" . $admin['admin_groups_id'] . "' " );
	    $class_s="";
	    $admin_group = tep_db_fetch_array ($admin_group_query);

	    $admin=array_merge($admin, $admin_group);
	    if (((!isset($_GET['mID'])) || ($_GET['mID'] == $admin['admin_id'])) && (!isset($mInfo)) ) {
	      $mInfo = new objectInfo($admin);
	    }
// 	      if ( (is_object($mInfo)) && ($admin['admin_id'] == $mInfo->admin_id) ) $class_s=" Selected";

	    $list_m[]=$admin;

	  }

	}

    }

?>