Newer
Older
Conjuro / content / controls / common.php
<?php
  /**
   * Common control library 0.1 for Conjuro.
   */
  return [
    new class("html") extends Control{
      protected function renderControl($meta,$data) {
        echo $meta["html"];
      }
    },
    new class("img") extends Control{
      protected function renderControl($meta,$data) {
        c()->rhtml(
          'img',
          ['id'=>$meta['id'],'class'=>$meta['class'],'src'=>$meta['src']]
        );
      }
    },
    new class("builder") extends Control{
      protected function renderControl($meta,$data) {
        include "Conjuro/Builder.php";
      }
    },
    new class("form") extends Control{
      protected function renderControl($meta,$data) {
        $cols=$meta['columns'];
        c()->rhtml(
          'form',
          ['id'=>$meta['id'],'class'=>$meta['class'],'action'=>$meta['action'],'method'=>$meta['method']],
          fn()=>c()->rhtml(
            'table',
            ['class'=>'form-body-table'],
            function() use($meta,$data,$cols){
              echo '<tr>';
              $col=0;
              foreach($meta['content'] as $child){
                c()->renderContent($child,$data);
                if(++$col>=$cols){
                  echo '</tr><tr>';
                }
              }
              if ($meta['accept']||$meta['reset']){ //Buttons}
                if ($col!=0)
                  echo '</tr><tr>';
                echo '<td colspan='.($cols*2).' class="form-body-buttons">';
                if ($meta['accept'])
                  c()->rhtml('input',['type'=>'submit','value'=>$meta['accept']]);
                if ($meta['reset'])
                  c()->rhtml('input',['type'=>'reset','value'=>$meta['reset']]);
                echo '</td>';
            }
              echo '</tr>';
            }
          )
        );
      }
    },
    new class("form-data") extends Control{
      protected function renderControl($meta,$data) {
        if ($meta['id']){
          $lid=$meta["id"].'-label';
          $vid=$meta["id"].'-value';
        }
        c()->rhtml('td',['id'=>$lid,'class'=>'form-data-label '.$meta["class"]],fn()=>print(htmlentities($meta["label"])));
        c()->rhtml('td',['id'=>$vid,'class'=>'form-data-value '.$meta["class"]],function() use($meta,$data){
          $type=$meta['type'];
          if (!$type)
            $type='input';
          switch($type){
            case 'input':
              echo c()->htmlopen('input',['id'=>$meta['id'],'name'=>$meta['id']]);
              break;
            case 'password':
              echo c()->htmlopen('input',['id'=>$meta['id'],'name'=>$meta['id'],'type'=>'password']);
              break;
            }
        });
      }
    },
    new class("jump") extends Control{
      protected function renderControl($meta,$data) {
        $ctt=$meta["height"];
        echo "<div style='height:$ctt'></div>";
      }
    },
    new class("icon") extends Control{
      protected function renderControl($meta,$data) {
        ?><i class="<?=$meta['class']?> fa-solid fa-<?=$meta['icon']?>"></i><?php
      }
    },
    new class("table") extends Control{
      protected function renderControl($meta,$data) {
        ?><table class="tbl <?=$meta['class']?>"><tr><?php
        ?></tr></table><?php
      }
    },
    new class("list") extends Control{
      protected function renderControl($meta,$data) {
        $itemData=$data[$meta['data']];
        $itemTemplate=$meta['item'];
        if (!$itemData)
          $itemData=[];
        ?>
          <ul class="list <?=$meta['class']?>">
            <?php
              foreach($itemData as $item){
                $data['item']=$item;
                c()->renderContent($itemTemplate,$data);
              }
            ?>
          </ul>
        <?php
      }
    },
    new class("list-item") extends Control{
      protected function renderControl($meta,$data) {
        $itemData=$data['item'];
        if (!$itemData)
          $itemData=[];
        $link=$meta['link'];
        if ($link){
          if ($link[0]=='&')
            $link=substr($link,1);
          if (($link.'?')[0]!='?')
            $link="?$link";
          $link.='&';
        }else
          $link='?';
        $link.='data='.urlencode(json_encode($itemData));
        ?>
          <a href="<?=$link?>"><li class="list-item <?=$meta['class']?>"><?=$itemData[$meta['field-title']]?></li></a>
        <?php
      }
    },
  ];
?>