<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__)
  .' is not allowed.' );
/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 */

//include_once( JPATH_ROOT."/components/com_medialibrary/compat.joomla1.5.php" );

if(!defined( '_JLEGACY' )){
    $GLOBALS['path'] = $mosConfig_live_site."/components/com_medialibrary/images/";
} else{
    $GLOBALS['path'] = $mosConfig_live_site."/administrator/components/com_medialibrary/images/";
}
$path = $GLOBALS['path'];


class DMInstallHelper{ 
     static function getComponentId(){
          global $database;
          if (version_compare(JVERSION, "1.6.0", "lt")){
              $query = "SELECT c.id FROM #__components AS c WHERE c.option='com_medialibrary'";
          } else if (version_compare(JVERSION, "1.6.0", "ge")){
              $query = "SELECT e.extension_id FROM #__extensions AS e WHERE e.element='com_medialibrary'";
          } else {echo "Sanity test. Error version check!"; exit;}
      $database->setQuery($query);
      $id = $database->loadResult();
      return $id;
    }

      
   static function getParentId(){
        $id = DMInstallHelper::getComponentId();
        global $database;
        if (version_compare(JVERSION, "1.6.0", "lt")){
            //
        } else if (version_compare(JVERSION, "1.6.0", "ge")){
          $database->setQuery("SELECT id FROM #__menu 
                      WHERE title='MediaLibrary' and level=1 and parent_id=1 and component_id=".$id );
          $parent_id =$database->loadResult();
            return $parent_id;
        } else {echo "Sanity test. Error version check!"; exit;}
    }
   
       

    static function setAdminMenuImages(){
      global $database, $path;

        $id = DMInstallHelper::getComponentId();
      if (version_compare(JVERSION, "1.6.0", "lt")){

            // Main menu
            $database->setQuery("UPDATE #__components SET admin_menu_img = '".$path
                ."dm_component_16.png' WHERE id=$id");
            $database->query();

            // Submenus
        $submenus = array();
        $submenus[] = array( 'image' => $path.'dm_edit_16.png', 'title'=>'Media' );
        $submenus[] = array( 'image' => $path.'dm_component_16.png', 'title'=>'Categories' );
        $submenus[] = array( 'image' => $path.'dm_component_16.png', 'title'=>'Reviews' );
        $submenus[] = array( 'image' => $path.'dm_component_16.png', 'title'=>'Lend Requests' );
        $submenus[] = array( 'image' => $path.'dm_component_16.png', 'title'=>'User Lend History' );
        $submenus[] = array( 'image' => $path.'dm_component_16.png', 'title'=>'Import/Export' );
        $submenus[] = array( 'image' => 'class:config', 'title'=>'Settings' );
        $submenus[] = array( 'image' => 'class:info', 'title'=>'About' );
            foreach($submenus as $submenu){
                $database->setQuery("UPDATE #__components SET admin_menu_img = '".$submenu['image']  
                                      ."' WHERE parent=$id AND name = '".$submenu['title']."';");
                $database->query();
            }
        } else if (version_compare(JVERSION, "1.6.0", "ge")){
            $parent_id = DMInstallHelper::getParentId();

            // Main menu
            $database->setQuery("UPDATE #__menu SET img = 'class:component'   
                                 WHERE title='MediaLibrary' and level=1 and parent_id=1 and component_id=$id");
            $database->query();

            // Submenus
           $submenus = array();
            $submenus[] = array( 'img' => 'class:component', 'title' => 'MediaLibrary','alias'=>'MediaLibrary');
            $submenus[] = array( 'img' => 'class:module', 'title'=>'Media','alias'=>'Media' );
            $submenus[] = array( 'img' => 'class:module', 'title'=>'People','alias'=>'People' );
            $submenus[] = array( 'img' => 'class:category', 'title'=>'Categories','alias'=>'Categories' );
            $submenus[] = array( 'img' => 'class:writemess', 'title'=>'Reviews','alias'=>'Reviews' );
            $submenus[] = array( 'img' => 'class:writemess', 'title'=>'Lend Requests','alias'=>'Lend Requests');
            $submenus[] = array( 'img' => 'class:writemess', 
                                 'title'=>'User Lend History','alias'=>'User Lend History' );
            $submenus[] = array( 'img' => 'class:config', 'title'=>'Import/Export','alias'=>'Import/Export' );
            $submenus[]=array( 'img' => 'class:writemess','title'=>'Language Manager','alias'=>'Language Manager');
            $submenus[]=array( 'img' => 'class:config','title'=>'Settings','alias'=>'Settings' );
            $submenus[]=array( 'img' => 'class:info', 'title'=>'About','alias'=>'About' );            

            foreach($submenus as $submenu){
                $database->setQuery("UPDATE #__menu SET img = '".$submenu['img']
                    ."' WHERE component_id=$id AND parent_id = '"  
                    .$parent_id."' and level=2  AND title = '".$submenu['title']."';");
                $database->query();
                $database->setQuery("UPDATE #__menu SET alias = '" . $submenu['alias'] . "'"   
                                      . "\n WHERE component_id=$id AND title = '" . $submenu['title'] . "';");
                $database->query();
            }



        }else {echo "Sanity test. Error version check!"; exit;}
    }    
    
}


