<?php


////////////////////// CORE FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////
if (!function_exists('insert_photo_row')) {
function insert_photo_row($table_id,$ref) { 

    $fields1=array(
	title=>'text',
	ref=>$ref,
	status=>'Active',
	);
	$fields2=array();
	
	$editdata=db_insertdata($table_id,$fields2,$fields1,0);
	
	
	return $editdata;
} 
}
/////////////////////////// function ends /////////////////////////////////

//////////////////////////// HTML FUNCTION ENDS (c) i2d Technologies ///////////////////////////////////////
if (!function_exists('makethumb_adv')) {
function makethumb_adv($settings) { 


if (array_key_exists('file_name', $settings)) {
   $file=$settings['file_name'];
   }
if (array_key_exists('uploaded_tmp', $settings)) {
   $uploaded_path=$settings['uploaded_tmp'];
   }
if (array_key_exists('site_name_prefix', $settings)) {
   $sitename=$settings['site_name_prefix'];
   }
if (array_key_exists('record_id', $settings)) {
   $record_id=$settings['record_id'];
   }
if (array_key_exists('slip_no', $settings)) {
   $slipno=$settings['slip_no'];
   }
if (array_key_exists('table', $settings)) {
   $table=$settings['table'];
   }
if (array_key_exists('img_col', $settings)) {
   $img_col=$settings['img_col'];
   }
if (array_key_exists('id_col', $settings)) {
   $record_id_col=$settings['id_col'];
   }
if (array_key_exists('upload_dir', $settings)) {
   $path=$settings['upload_dir'];
   }
if (array_key_exists('upload_dir', $settings)) {
   $base_path=$settings['upload_dir'];
   }
if (array_key_exists('cat_id', $settings)) {
   $cat_id=$settings['cat_id'];
   }
if (array_key_exists('cat_col', $settings)) {
   $cat_col=$settings['cat_col'];
   }
if (array_key_exists('og_dir', $settings)) {
   $og_path=$settings['og_dir'];
   }
if (array_key_exists('original', $settings)) {
   $original=$settings['original'];
   }
if (array_key_exists('thumbs_req', $settings)) {
   $thumbs_req=$settings['thumbs_req'];
   }
if (array_key_exists('redirect', $settings)) {
   $redirect=$settings['redirect'];
   }
//echo dirname(__FILE__);

   

$gd1="../assets/plugins/gd/Thumbnail.class.php";
$gd2="../assets/plugins/gd/ThumbLib.inc.php";


include_once($gd1);
include_once($gd2);

$mysqli_user=uplink();    
	

	$product_image_name2=$file;
	$uploaded_file_path2 = $uploaded_path;
	

	
	if($product_image_name2 != ""){
		$filetype2 = substr($product_image_name2, -3);
		//$site_name2 = $sitename.$slipno.$thumb_folder.$record_id;
		$site_name2 = $sitename.$record_id;
		if($filetype2=="jpg" || "JPG" || "JPEG" || "jpeg" || "gif" || "png")
		{
			$product_image_name_new2 = $site_name2 . "." . strtolower($filetype2);
			$query2 = "UPDATE $table SET $img_col = '$product_image_name_new2' , $cat_col = '$cat_id' WHERE $record_id_col = '$record_id'";
			//
			$query_up=$mysqli_user->query($query2);
			//Move uploaded file to temp folder
			if (move_uploaded_file($uploaded_file_path2, $path.$product_image_name_new2))
			{
				$msg2="yes";
			}else{
				//if upload fails
				$msg2="no";
				//echo $path.$product_image_name_new2;
				die("Error uploading photo - Check File Type - Connection Slow");
	//			exit();
			}
			if($thumbs_req!=0){
			
			
			$thumb2 = PhpThumbFactory::create($base_path.$product_image_name_new2."");
			if($thumb2->resize(333, 333)){

				if(!$thumb2->save($og_path.$product_image_name_new2)){
					print "<br>Failed to Create Thumb, The required file might be moved, removed or the image is too small";
				}
			}
			
				
			}
			if($original!=0){
			if (copy($base_path.$product_image_name_new2, $base_path.$product_image_name_new2)){
			unlink($base_path.$product_image_name_new2);
			}
else{
//echo $base_path."original/".$product_image_name_new2."<br />".$base_path.$product_image_name_new2;
//exit();	
}
			}
			else{
			unlink($base_path.$product_image_name_new2);
			}
		}
	}
	else{
			$product_image_name_new2 = 'default_image.png';
			$query2 = "UPDATE $table SET $img_col = '$product_image_name_new2' , $cat_col = '$cat_id' WHERE $record_id_col = '$record_id'";
			$query78=$mysqli_user->query($query2);
			//echo $query2."<br />";
	}

	

	
} 
}

//////////////////////////////////////////////////////////////////////////

//////////////////////////// HTML FUNCTION ENDS (c) i2d Technologies ///////////////////////////////////////
if (!function_exists('makeform_img_dropdown')) {
function makeform_img_dropdown($settings) {
if (array_key_exists('table', $settings)) {
   $table=$settings['table'];
   }
   if (array_key_exists('textfields', $settings)) {
   $text=$settings['textfields'];
   }
   if (array_key_exists('image_alias', $settings)) {
   $image_alias=$settings['image_alias'];
   }
   if (array_key_exists('encoding', $settings)) {
   $encoding=$settings['encoding'];
   }
   if (array_key_exists('redirect', $settings)) {
   $redirect=$settings['redirect'];
   }
   if (array_key_exists('label', $settings)) {
   $headline=$settings['label'];
   }
   if (array_key_exists('suffix', $settings)) {
   $suffix=$settings['suffix'];
   }
   if (array_key_exists('return_path', $settings)) {
   $dash=$settings['return_path'];
   }
   if (array_key_exists('form_setup', $settings)) {
   $setup=$settings['form_setup'];
   }

   
?>
<script type="text/javascript">
  $(document).ready(function() {
$('.dropify').dropify({
    error: {
        'fileSize': 'The file size is too big ({{ value }} max).',
        'minWidth': 'The image width is too small ({{ value }}}px min).',
        'maxWidth': 'The image width is too big ({{ value }}}px max).',
        'minHeight': 'The image height is too small ({{ value }}}px min).',
        'maxHeight': 'The image height is too big ({{ value }}px max).',
        'imageFormat': 'The image format is not allowed ({{ value }} only).'
    },
messages: {
        'default': 'Drag and drop a file here or click',
        'replace': 'Drag and drop or click to replace',
        'remove':  'Remove',
        'error':   'Ooops, something wrong happended.'
    }	
});
	  
$("#subbtn").click(function(){
$("#subbtn").text("Please Wait...");

	  });
  });
  </script>


  
<div id="page-right-content">

                    <div class="container">
    
<div class="row">


<div class="col-sm-12">
                                <h4 class="header-title m-t-0 m-b-20"><?php echo $headline; ?></h4>
                            </div>
		
</div>	


<script type="text/javascript">
  $(document).ready(function() {
	 
$("#subbtn").click(function(){
$("#locked").show();
$(".card").css("pointer-events","none");

	  });
	 });
  </script>
  
<p>&nbsp;</p>
                    
						<div class="row">
                <div class="col-md-12">

<form enctype="multipart/form-data" method="post" novalidate>

							<?php
							////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($setup['dropdown']) && !empty($setup['dropdown'])){
$dropdown=$setup['dropdown'];	
$droploop=$setup['dropdown'];	
if(isset($settings['edit']) && $settings['edit']!="0"){	
$db_settings3=array(
'table'=>$droploop['table'],
'id'=>$settings['lead_id'],
'type'=>'0',
'to_find'=>$droploop['remote_table_field']);


$drop3=db_fetch($db_settings3);

$db_settings4=array(
'table'=>$droploop['table'],
'id'=>$drop3,
'type'=>'0',
'to_find'=>$droploop['remote_tbl_title_col']);

$drop4=db_fetch($db_settings4);


}
?>
<?php
if(isset($setup['edit']) && $setup['edit']!="0"){
$editing=array(
'editing'=>'1',
'selected_id'=>$drop3,
'selected_text'=>$drop4);
}
else{
$editing=array(
'editing'=>'1');	
}
dynamicdrop_adv($dropdown['label'],$dropdown['encoding'],$editing,$dropdown['ref'],$dropdown['table'],$dropdown['table_title'],$dropdown['table_id_col'],$dropdown['add_new'],$dropdown['other_id_col']);
?> 
<?php



}	

		?>					



<?php

if(isset($text)){
foreach ($text as $ty1 => $value){
$text1[] = $text[$ty1]['fname'];
$db[] = $text[$ty1]['fname'];

$heading=$text[$ty1]['label'];

if(isset($settings['edit']) && $settings['edit']!="0"){
$db_settings4=array(
'table'=>$table,
'id_col'=>id_col,
'id'=>$settings['record_id'],
'type'=>'0',
'to_find'=>$text[$ty1]['fname']
);

$photo=db_fetch($db_settings4);
}

?>



              
			  
                 
				 <?php if (!isset($text[$ty1]['class'])) { ?>
				 <div class="col-md-6">
				 	 <?php } 
				 else{ ?>
				<div class="<?php echo $text[$ty1]['class']; ?>">
				 <?php } ?>	 
				 <p>&nbsp;</p>
<h4><?php echo $heading; ?></h4>                    
					 	<input type="file" data-max-file-size="<?php echo $text[$ty1]['limit']; ?>" data-allowed-file-extensions="jpg png" 
						class="dropify" name="<?php echo $text[$ty1]['field']."_name"; ?>" id="upload_file_id" 
						<?php  
						if(isset($photo) && $photo!=""){
						?> 
						 data-default-file="<?php echo SITE_IMAGES.$setup['upload_folder_title']."/".$photo; ?>" 
						<?php  
                    }
						?>
						/>
				<?php if (isset($text[$ty1]['hint'])) { 
			?>			
			<small style="color:red;"><strong>
				<?php 		
	       			echo $text[$ty1]['hint'];  ?>
						
						</strong></small>
				<?php } ?>
					


    	</div>	
		
		

<?php
	  
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

?>
<p>&nbsp;</p>
<div class="col-md-12 text-center gap-items-5">


                    <button type="reset" class="btn btn-lg btn-label btn-danger" onclick="history.go(-1);">Cancel
					<label><i class="ti-close"></i></label>
					</button>
                    
					<button class="btn btn-lg btn-label btn-success" type="submit" name="add_itembtn" id="subbtn">Insert
					<label><i class="ti-shift-right"></i></label>
					</button>
                    <input type="hidden" name="add_item" value="add_item">
                    <hr class="d-lg-none">
                    

					

				  	  
                
            
				  </div>
			
				  </div>
				  </div>
				 
				  <?php
///////////////////////////////////////////////////////////////////
if(isset($_POST['add_item'])){

if(isset($setting['edit']) && $setting['edit']!="0"){
$lead_id=$lead_id;
}
else{
$lead_id=insert_photo_row($table,$image_alias);	
}	
	
if(isset($setup['dropdown']) && !empty($setup['dropdown'])){	
//$dropfound2=$_POST[$dropdown['ref']."_dropname"];
$dropfound=$dropdown['ref']."_dropname";
$dropfound2=$_POST[$dropfound];
}
else{
$dropfound2="0";	
}	
$count=count($text1);
$i = 0;
$ret=array_combine($text1, $db);
foreach ($ret as $name => $col){
$i++;
//echo "here".$i;
	
$imgsettings=array(
'file_name'=>$_FILES[$name."_name"]['name'],
'uploaded_tmp'=>$_FILES[$name."_name"]['tmp_name'],
'site_name_prefix'=>$suffix.'-'.$name."-",
'record_id'=>$lead_id,
'slip_no'=>$lead_id."-",
'table'=>$table,
'img_col'=>$col,
'id_col'=>$setup['id_col'],
'cat_id'=>$dropfound2,
'cat_col'=>$setup['upload_category_col'],
'upload_dir'=>$setup['upload_dir'],
'og_dir'=>$setup['og_dir'],
'original'=>$setup['keep_original_size_image'],
'thumbs_req'=>$setup['og_thumbs_req'],
'redirect'=>$setup['redirect_url']);	

makethumb_adv($imgsettings);
}

app_redirect_adv($redirect);

}



?>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>

</div>

<?php
 
} 
}
/////////////////////////// function ends /////////////////////////////////


























?>