<?php
/*
  Conjuro-Web, Easy web builder: Single control render module

  Copyright (c) 2022 XWolf Override

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/
return new class($conf) extends Module{
  /**
   * Array of pages
   */
  private function listPages(){
    return $this->recursivedir('content/pages','*.json');
  }

  /**
   * Array of users
   */
  private function listUsers(){
    return c()->DBlist('users');
  }

  /**
   * Tool for recursive directory listings
   */
  private function recursivedir($dir,$pattern,$cut=-1){
    if ($cut<0)
      $cut=strlen($dir)+1;
    $result=[];
    foreach (glob("$dir/*",GLOB_ONLYDIR) as $ddir) {
      foreach($this->recursivedir($ddir,$pattern,$cut) as $inner)
        $result[]=$inner;
    }
    foreach (glob("$dir/$pattern") as $file) {
      $result[]=substr($file,$cut);
    }
    return $result;
  }

  public function init(){
    $this->register('get-panel-links',function(){
      return [
        ["title"=>"Dashboard","link"=>"?page=admin/dashboard","icon"=>"gauge"],
        ["title"=>"Usuarios","link"=>"?page=admin/users","icon"=>"user"],
        ["title"=>"Paginas","link"=>"?page=admin/pages","icon"=>"file"],
        ["title"=>"Test","link"=>"?page=admin/test","icon"=>"vial-virus"],
      ];
    });
    $this->register('controller-page-list',function($page){
      $pageList=[];
      foreach($this->listPages() as $p){
        $pageList[]=['id'=>$p];
      }      
      $page->setData('page-list',$pageList);
    });
    $this->register('controller-page-edit',function($page){
      $pname=$_GET['id'];
      $page->setData('page-id',$pname);
      $page->setData('page',Conjuro::loadJson("content/pages/$pname"));
    });
    $this->register('controller-user-list',function($page){
      //$page->setData('users',$this->listUsers());
    });
    $this->register('controller-user-edit',function($page){
      $inData=json_decode($_GET['data'],true);
      $page->setData('user-id',$inData['id']);
      $page->setData('user-name',$inData['name']);
    });
  }
}?>