<?php
namespace WpBookingEventAdmin\app\helpers;

use WpBookingEvent\Factory;
use WpBookingEvent\Filesystem\File;
use WpBookingEvent\Utilities\Utility;

class AdminHelper
{
    public static $list_key_tour_properties=array(
      'event'=>"event",
      'package'=>'package',
      'service'=>'service',
    );
    public static function getWpBookingEventKey(){
        return self::$list_key_tour_properties['event'];
    }
    public static function getWpBookingPackageKey(){
        return self::$list_key_tour_properties['package'];
    }
    public static function getWpBookingServiceKey(){
        return self::$list_key_tour_properties['service'];
    }

    public static function getListKeyOfTourProperties(){
        return static::$list_key_tour_properties;
    }
    public static function deletePropertyByKey($key='',$id=0) {
        $db= Factory::getDBO();
        $query=$db->getQuery(true);
        $query->clear()
            ->delete(Utility::getTableName('tourproperty'))
            ->where('object_id='.(int)$id)
            ->where('type='.$query->quote($key))
        ;
        $db->setQuery($query)->execute();

    }
    public static function insertNewPropertyByKey($key='',$properties=array(), $tour_id=0) {
        $db = Factory::getDBO();
        $query =$db->getQuery(true);
        foreach ($properties as $property) {
            $query->clear()
                ->insert(Utility::getTableName('tourproperty'))
                ->set('object_id='.(int)$property)
                ->set('type='.$query->quote($key))
                ->set('tour_id='.(int)$tour_id)
            ;
            $db->setQuery($query)->execute();
        }
    }

    public static function getDirContents($dir, &$results = array(), $exclude = array(), $filter = array())
    {
        $files = scandir($dir);
        $filter = array_map('strtolower', $filter);
        foreach ($files as $key => $value) {
            if (in_array($value, $exclude)) {
                continue;
            }
            $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
            $extension = strtolower(File::getExt($path));
            if (!is_dir($path)) {
                if (empty($filter)) {
                    $results[] = $path;
                } elseif (in_array($extension, $filter)) {
                    $results[] = $path;
                }
            } else {
                if ($value != "." && $value != "..") {
                    self::getDirContents($path, $results, $exclude, $filter);
                    $results[] = $path;
                }
            }
        }
        return $results;
    }


}