<?php
include(dirname(__FILE__)."/common.ini.php");
include("catalog_head.html");
//--------------------------------------------------------//
function category_parse($_categories,$_nbsp="",$category=array())
{
foreach($_categories as $_category)
{
if($_category['id']==@$category['id'])continue;
?>
<option value="<?php echo $_category['id']?>" <?php echo ($_category['id']==@$category['parent_id'])?"selected":"";?> ><?php echo $_nbsp.$_category['title']?></option>
<?php
if(count($_category['childs']))
category_parse($_category['childs'],$_nbsp." ",$category);
}
}
//-------------------------------------------------------------//
function scan_ftp_upload($catalog,$src_dir,$categ_id=0,$level=0)
{
$dst_dir = "../rwx/";
$upload_category = (int)$categ_id;
clearstatcache();
$folder_content=opendir($src_dir);
while($file = readdir($folder_content))
{
if($file == "." || $file == "..")continue;
if(is_dir($src_dir.$file))
{
Misc::_show_message_color("<b>Detected folder: ".$src_dir.$file."</b>");
$fields = $catalog->categs->model->get("WHERE title='".Misc::_safe_db_text($file)."'");
if((int)@$fields["id"] > 0)
Misc::_show_message_color("in the database already - skip","YELLOW");
else
{
$fields["title"] = Misc::_safe_db_text($file);
$fields["parent_id"] = $categ_id;
$fields["is_enabled"] = 1;
$fields["updated_at"] = date("Y-m-d H:i:s");
$fields["rank"] = 1;
$fields["id"] = $catalog->categs->model->save($fields);
}
scan_ftp_upload($catalog,$src_dir.$file."/",$fields["id"],$level+1);
}
else if(is_file($src_dir.$file))
{
Misc::_show_message_color("Detected file: ".$src_dir.$file);
$fields = array();
$fields["title"] = Misc::_safe_db_text($file);
$fields["title"] = preg_replace("#(\.\S+)$#","",$file);
$fields["id"] = $catalog->items->model->get("WHERE title='".$fields["title"]."'");
if((int)@$fields["id"] > 0)
Misc::_show_message_color("'".$fields["title"]."' in the database already - skip","YELLOW");
else
{
$fields["categ_id"] = $categ_id;
//$fields["rank"] = $catalog->items->get_category_max_rank($categ_id);
$fields["is_enabled"] = 1;
$fields["thumb_static"] = "N";
$fields["updated_at"] = date("Y-m-d H:i:s");
$fields["price"] = 0.01;
$catalog->items->htpf["file"] = array("name"=>$file,"tmp_name"=>$src_dir.$file,"error"=>"");
$result = $catalog->items->update($fields);
if($result)
Misc::_show_message_color("Added: ".$catalog->items->model->fields["title"],"GREEN");
else
Misc::_show_message_color("The file $src_dir$file is not added to $dst_dir$file_new","YELLOW");
}
}
flush();
}//while
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function show_tree($dir_src,$level=0)
{
$folder_contents=opendir($dir_src);
while($file = readdir($folder_contents))
{
if($file == "." || $file == "..")continue;
$br = "\n<br>";
for($i=0;$i<$level;$i++)
$br.=" ";
if(is_dir($dir_src."/".$file))
{
echo "$br <b> $file </b>";
show_tree($dir_src."/".$file,$level+1);
}
else
echo "$br $file";
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
if (!is_writable("../rwx/"))
Misc::_show_message_color("<b>Warning:</b> ../rwx/ directory is not writable","RED");
$src_dir = "../ftp/";
if( isset($postget["action"]) && $postget['action']=="scan" )
{
Misc::_show_message_color("<h3>Scanning $src_dir directory...</h3>");
scan_ftp_upload($catalog,$src_dir,$postget["categ_id"]);
Misc::_show_message_color("<h3>DONE - now you may manually clean up directory $src_dir</h3>");
}
else
{
?>
<form method="get" action="">
<b>Assign new files from <?php echo $src_dir?> directory to the category:</b>
<select name="categ_id">
<option value="0">/</option>
<?php
$html["categs"] = $catalog->categs->gets();
category_parse($html["categs"])?>
</select>
<input type="submit" value="Submit">
<input type="hidden" name="action" value="scan">
</form>
<hr><?php show_tree($src_dir);?>
<?php
}
include("catalog_foot.html");
?>