<?php

// Multiple Inclusion Protection
if (!defined("CLASS_PROFILE_INC"))
{
define("CLASS_PROFILE_INC",1);
// Multiple Inclusion Protection

  // for editing profiles we create a custom attribute
  class profileAttribute extends atkAttribute
  {
    function profileAttribute($name, $flags)
    {
      $this->atkAttribute($name, $flags);
    }
    
    function load($db, $record)
    {
      $query = "SELECT * FROM accessright WHERE entity='".$record["id"]."'";
      atkdebug($query);
      $result = Array();
      $rows = $db->getrows($query);
      for ($i=0;$i<count($rows);$i++)
      {     
        $result[$rows[$i]["node"]][] = $rows[$i]["action"];
      }
      return $result;
    }
    
    function store($db, $record, $mode)
    {
      $delquery = "DELETE FROM accessright WHERE entity='".$record["id"]."'";
      atkdebug($delquery);
      $db->query($delquery);
      
      $checked = $record[$this->fieldName()];
                  
      while (list($node, $actions)=each($checked))
      {
        for ($i=0;$i<count($actions);$i++)
        {
          $query = "INSERT INTO accessright (node, action, entity) ";
          $query.= "VALUES ('".$node."','".$actions[$i]."','".$record["id"]."')";
          atkdebug($query);
          $db->query($query);
        }
      }
    }
    
    function edit($record)
    {
      //var_dump($record);
      require "config_modules.inc";
      
      $checked = $record[$this->fieldName()];
      
      while (list($modname, $actionarr) = each($modules))
      {
        $checked_actions = $checked[$modname];
        if (is_array($actionarr))
        {
          $result.="<b>".text($modname)."</b><br>";
          for ($i=0;$i<count($actionarr);$i++)
          {
            $action = $actionarr[$i];
            if (substr($action,0,1)=="*") $action=substr($action,1);
            $result.='<input type="checkbox" name="'.$this->formName().'[]" value="'.$modname.".".$action.'" ';
            if (is_array($checked_actions)&&in_array($action, $checked_actions)) $result.=" checked";
            $result.='> '.text($action).'&nbsp;&nbsp;&nbsp;';
          }
          $result.="<br><br>";
        }
      }
      return $result;
    }
    
    function fetchValue($postvars)
    {
      $checkboxes = $postvars[$this->fieldName()];
      $actions = Array();
      for($i=0;$i<count($checkboxes);$i++)
      {
        list($mod, $action) = split("\.",$checkboxes[$i]);
        $actions[$mod][] = $action;            
      }
      return $actions;
    }
  }


  class profile extends atkNode
  {
    function profile()
    {
      $this->atkNode("profile", NF_EDITAFTERADD); // node() constructor is *not* called automatically!

      $this->addAttribute(new atkAttribute("id",AF_PRIMARY|AF_AUTO_INCREMENT|AF_HIDE));
      $this->addAttribute(new atkAttribute("name",AF_OBLIGATORY|AF_UNIQUE|AF_SEARCHABLE));      
      $this->addAttribute(new atkDummyAttribute("profile_explanation",text("profile_explanation"),AF_HIDE_LIST|AF_HIDE_ADD));
      $this->addAttribute(new profileAttribute("accessrights",AF_HIDE_LIST|AF_HIDE_ADD|AF_BLANKLABEL));

      $this->setTable("profile","profile");
      $this->setOrder("name");
      
    }

    function descriptor_def()
    {
      return "[name]";
    }
   
  }

} // endif CLASS_PROFILE_INC;

?>
