<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$pag = $page = 1; $setLimit = 20;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
if(isset($_GET["pag"])) $pag = (int)$_GET["pag"];
$pageLimit2 = ($pag * $setLimit) - $setLimit; 
$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "", "s=s_q" => "", "s=s_c" => "", "s=s_f" => "", "s=s_p" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
//====End ==============
define('TITLE','Modules');
if(userRole()>1) {
	header('location: index.php');	
}

$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
?>
<?php

if(isset($_POST['action'])) {
  if(DEMO_MODE) {
		$_SESSION['message'] = "Feature not allowed on Demo Mode";
		header('location: amodule?err=0');exit;
   }	
  if($_POST['action']=='disable') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"UPDATE `modules` SET status = '0' WHERE `id` = '$inbid'") or die(mysqli_error($server));
		mysqli_query($server,"UPDATE `paymentgateway_templates` SET status = '0' WHERE `module_id` = '$module_id'") or die(mysqli_error($server));
		mysqli_query($server,"UPDATE `api_templates` SET status = '0' WHERE `module_id` = '$module_id'") or die(mysqli_error($server));
	}	
	logEvent($userID,'Disabled some modules');
	$_SESSION['message'] = $LANG['The selected module(s) has been disabled'];
	header('location: amodule?err=0');
  }
  if($_POST['action']=='enable') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"UPDATE `modules` SET status = '1' WHERE `id` = '$inbid'") or die(mysqli_error($server));
		mysqli_query($server,"UPDATE `paymentgateway_templates` SET status = '1' WHERE `module_id` = '$module_id'") or die(mysqli_error($server));
		mysqli_query($server,"UPDATE `api_templates` SET status = '1' WHERE `module_id` = '$module_id'") or die(mysqli_error($server));
	}	
	logEvent($userID,'Enabled some modules');
	$_SESSION['message'] = $LANG['The selected module(s) has been enabled'];
	header('location: amodule?err=0');
  }
  if($_POST['action']=='uninstall') {
	foreach($_POST['bulk'] as $inbid) { 
		$query="SELECT * FROM modules WHERE id = '$inbid'"; 
		$result = mysqli_query($server,$query) or die(mysqli_error($server));  
		$profile = mysqli_fetch_assoc($result); 
		$alias = $profile['url'];
		$type = strtolower($profile['type']);	
		$module_id = $profile['module_id'];
		if($type=='sms') {
			if(file_exists('smsapi/'.$alias))deleteDir('smsapi/'.$alias);
			if(file_exists('system/eventhooks/'.$alias.'.php'))unlink('system/eventhooks/'.$alias.'.php');
		} else if($type=='payment') {
			if(file_exists('gateway/'.$alias))deleteDir('gateway/'.$alias);
			if(file_exists('system/eventhooks/'.$alias.'.php'))unlink('system/eventhooks/'.$alias.'.php');
		} else {
			if(file_exists('system/modules/'.$alias))deleteDir('system/modules/'.$alias);
			if(file_exists('system/eventhooks/'.$alias.'.php'))unlink('system/eventhooks/'.$alias.'.php');
		}
		mysqli_query($server,"DELETE FROM `modules` WHERE `id` = '$inbid'") or die(mysqli_error($server));
		mysqli_query($server,"DELETE FROM `paymentgateway_templates` WHERE `module_id` = '$module_id'") or die(mysqli_error($server));
		mysqli_query($server,"DELETE FROM `api_templates` WHERE `module_id` = '$module_id'") or die(mysqli_error($server));
	}	
	logEvent($userID,'Uninstalled some modules');
	$_SESSION['message'] = $LANG['The selected module(s) has been uninstalled'];
	header('location: amodule?err=0');
  }
  
}

?>
 <div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card blue">
            <div class="card-content white-text"><?=$LANG['module_tip']?></div>
        </div>
    </div>
 </div>
 
<div class="row no-m-t no-m-b">
	<div class="col s12 m12 l12">
      <div class="card card-white ">
           <div class="card-content">
                        
                <div class="row no-m-t no-m-b right"> 
                	<a href="http://sendroidultimate.ynetinteractive.com" target="_blank" class="btn cyan darken-4"><?=$LANG['Find Modules']?></a>
                	<a class='waves-effect waves-light btn green darken-4 white-text' href="#" onclick="$('#add_plan_pop').show('slow');"><?=$LANG['Install New Module']?></a>
                    <ul id='bulka' class='dropdown-content'>
                        <li><a class='red-text'  href="#" onclick="bulkaction2('uninstall','inbox-form2','<?=$LANG['delete_confirm']?>');"><?=$LANG['Uninstall Selected']?></a></li>
                        <li><a class='green-text'  href="#" onclick="bulkaction2('enable','inbox-form2','<?=$LANG['menalle_confirm']?>');"><?=$LANG['Enable Selected']?></a></li>
                        <li><a class='orange-text'  href="#" onclick="bulkaction2('disable','inbox-form2','<?=$LANG['disable_confirm']?>');"><?=$LANG['Disable Selected']?></a></li>
					</ul>
                    <a class='dropdown-button btn light-blue darken-4' href='#' data-activates='bulka' data-beloworigin='true' data-constrainwidth='false'><?=$LANG['With Selected']?></a>
                </div>             	
                <div class="row no-m-t no-m-b">   
                    <div class="col s12 m12 l12">
                       <h5><?=$LANG['Installed Modules']?></h5> 
                    <?php
                    $query="SELECT * FROM modules ORDER BY id DESC limit $pageLimit2,$setLimit";
					$result = mysqli_query($server,$query) or die(mysqli_error($server));
					$x = mysqli_num_rows($result);
                    if($x < 1) {
                    ?><table>	   
                      <div class="col s12 m12 l12">
                            <div class="card blue lighten-5">
                                <div class="card-content">
                                     <h3 class="center-align"><i class="fa fa-exclamation-circle"></i></h3>
                                     <h5 class="center-align"><?=$LANG['You have not installed any modules yet']?></h5>
                                </div>
                            </div>
                        </div>

                       </table> 
                      <?php } else { ?>  
                      <table class="responsive-table bordered">
                          <thead>
                            <tr>
                            	<th><input type="checkbox" class="filled-in" id="selectall2" /><label for="selectall2"></label></th>
                                <th data-field="ref"><?=$LANG['Title']?></th>
                                <th data-field="credit"><?=$LANG['Author']?></th>
                                <th data-field="typ"><?=$LANG['Type']?></th>
                                <th data-field="date"><?=$LANG['Privileges']?></th>
                                <th data-field="valid"><?=$LANG['Status']?></th>
                             </tr>
                          </thead>
                          <tbody>
                          <form action="amodule" method="post" id="inbox-form2">
                    	<input type="hidden" id="bulkaction2" name="action" value=""/>
                          <?php
                        while ($rows = mysqli_fetch_assoc($result)) { 
						$status = $LANG['Enabled'];
						if($rows['status'] < 1)	$status = $LANG['Disabled'];
						if(empty($rows['privileges'])) {	$privileges = 'Undifined'; }
						else {
							$privileges = '';
							$privilegest = explode(',',$rows['privileges']);	
							foreach($privilegest as $privilege) {
								$privileges .= '<span class="chip blue white-text">'.roleName($privilege).'</span> ';
							}
						}
						
						?>
                             <tr>
                                 <td><input type="checkbox" class="filled-in bulk2" id="bulk<?=$rows['id']?>" value="<?=$rows['id']?>" name="bulk[<?=$rows['id']?>]"/> <label for="bulk<?=$rows['id']?>"></label></td> 

                               	 <td><?=$rows['title']?></td>
                                 <td><?=$rows['author']?></td>
                                 <td><?=strtoupper($rows['type'])?></td>
                                 <td><?=$privileges?></td>
                                 <td><?=$status?> </td>
                             </tr>
                       <?php } ?>   
                       		</form>
                          </tbody>
                      </table>
                      <?php } ?>
                      <div class="col s12 m12 l12" style="margin-top: 5px;">
                          <?php displayPagination2($setLimit,$pag,$query,'');  ?>
                      </div>
                     </div>
                     
				</div>
            </div>
      </div>  

	</div>
</div>

<!--  //Popups  ---->
 
 <!--add rates -->
<div id="add_plan_pop" style="display: none; overflow-x: hidden; width: 99.99%; height: 99.99%; z-index: 1000; position: fixed; top: 0; left: 0; background: rgba(0,0,0,0.7);">
   	 <div class="row"><br><br>
       	<div class="col s1 m2 l3"> </div>
        <div class="col s10 m8 l6 white"> 
             <h5><?=$LANG['Install Module']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_plan_form')[0].reset();$('#add_plan_pop').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
           		 <div class="row no-m-t no-m-b">
                   <div class="col s12 m12 l12">
                     <div class="card blue lighten-5 ">
                         <div class="card-content black-text"><?=$LANG['insytall_mod_tip']?><br></div>
                    </div>
                </div>	
             </div>   
                         
             <div class="card ">
                 <div class="card-content">
                 	<form action="" method="post" id="add_plan_form" onSubmit="return false;">
                    <div class="row">
                      	<div class="row no-m-t no-m-b">
                            <div class="file-field input-field col s12">
                                <div class="btn light-blue darken-4">
                                    <span><i class="fa fa-cloud-upload"></i></span>
                                    <input id="files" type="file" name="files" accept="application/x-zip-compressed">
                                </div>
                                <div class="file-path-wrapper">
                                    <input class="file-path validate" type="text" placeholder="<?=$LANG['Upload a module zip file']?>">
                                    <input type="hidden" id="files_holder">
                               </div>
                            </div> 
                        </div> 
                            
                        <div class="card-action">
                             <button id="mod-button" class="btn green darken-4" type="submit"><?=$LANG['Upload and Install']?> </button>
                             <span id="mod-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                            <a class="btn waves-effect waves-light red" href="#" onclick="$('#add_plan_form')[0].reset();$('#add_plan_pop').hide('slow');"><?=$LANG['Cancel']?></a>
                        </div>
                    </div>   
                    </form>
                 </div>
              </div>
        </div>      
        <div class="col s1 m2 l3"> </div>
     </div>
</div>           
