Ignore:
Timestamp:
Feb 28, 2022, 8:53:22 AM (2 years ago)
Author:
chronos
Message:
  • Modified: Do not determine form submit from URL but directly from submit variable and filled form variables.
  • Added: Use system config from /etc/isp-central directory if standard config is not available.
  • Modified: Form file code cleanup. FormManager class moved to separate file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Common/Form/Form.php

    r914 r922  
    22
    33include_once(dirname(__FILE__).'/Types/Type.php');
     4include_once(dirname(__FILE__).'/FormManager.php');
    45
    56/*
     
    1920*/
    2021
    21 
    2222class Form
    2323{
     
    6767  {
    6868    $Item = $this->Definition['Items'][$Index];
    69     if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    70     {
    71       if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    72         $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]);
    73       if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
    74         $UseType = 'OneToMany';
    75       else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
    76         $UseType = 'Enumeration';
    77     } else $UseType = $Item['Type'];
     69    $UseType = $this->GetItemType($Item);   
    7870    return $this->FormManager->Type->ExecuteTypeEvent($UseType, $Event,
    79         array('Value' => $this->Values[$Index], 'Name' => $Index,
    80         'Type' => $Item['Type'], 'Values' => $this->Values,
    81         'Filter' => $this->Values[$Index]));
     71      array('Value' => $this->Values[$Index], 'Name' => $Index,
     72      'Type' => $Item['Type'], 'Values' => $this->Values,
     73      'Filter' => $this->Values[$Index]));
    8274  }
    8375
     
    9486    ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
    9587    {
    96       if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    97       {
    98         if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    99           $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]);
    100         if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
    101           $UseType = 'OneToMany';
    102         else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
    103           $UseType = 'Enumeration';
    104       } else $UseType = $Item['Type'];
     88      $UseType = $this->GetItemType($Item);
    10589      $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnView',
    10690        array('Value' => $this->Values[$Index], 'Name' => $Index,
     
    150134          else unset($Parameters['OnPreset']);
    151135
    152         if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    153         {
    154           if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    155             $this->FormManager->Type->RegisterType($Item['Type'], '',
    156               $this->FormManager->FormTypes[$Item['Type']]);
    157           if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
    158           {
    159             $UseType = 'OneToMany';
    160           } else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
    161             $UseType = 'Enumeration';
    162         } else $UseType = $Item['Type'];
     136        $UseType = $this->GetItemType($Item);
    163137        $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnEdit', $Parameters);
    164138        if (array_key_exists('Suffix', $Item)) $Edit .= $Item['Suffix'];
     
    182156  }
    183157
     158  function GetItemType($Item): string
     159  {   
     160    if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
     161    {
     162      if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
     163        $this->FormManager->Type->RegisterType($Item['Type'], '',
     164          $this->FormManager->FormTypes[$Item['Type']]);
     165      if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
     166        $UseType = 'OneToMany';
     167      else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
     168        $UseType = 'Enumeration';
     169    } else $UseType = $Item['Type'];
     170   
     171    return $UseType;
     172  }
     173
    184174  function LoadValuesFromDatabase(string $Id): void
    185175  {
    186176    foreach ($this->Definition['Items'] as $Index => $Item)
    187     if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
    188     (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
    189     ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
    190     {
    191         if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    192         {
    193           if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    194             $this->FormManager->Type->RegisterType($Item['Type'], '',
    195               $this->FormManager->FormTypes[$Item['Type']]);
    196           if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
    197             $UseType = 'OneToMany';
    198           else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
    199             $UseType = 'Enumeration';
    200         } else $UseType = $Item['Type'];
    201       if (!array_key_exists('SQL', $Item)) $Item['SQL'] = '';
    202         else $Item['SQL'] = str_replace('#Id', $Id, $Item['SQL']);
    203       $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery',
    204         array('Name' => $Index, 'Type' => $Item['Type'], 'SQL' => $Item['SQL']));
     177    {
     178      if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
     179      (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
     180      ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
     181      {     
     182        $UseType = $this->GetItemType($Item);
     183        if (!array_key_exists('SQL', $Item)) $Item['SQL'] = '';
     184          else $Item['SQL'] = str_replace('#Id', $Id, $Item['SQL']);
     185        $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery',
     186          array('Name' => $Index, 'Type' => $Item['Type'], 'SQL' => $Item['SQL']));
     187      }
    205188    }
    206189    $Columns = implode(',', $Columns);
     
    211194    $DbRow = $DbResult->fetch_array();
    212195    foreach ($this->Definition['Items'] as $Index => $Item)
    213     if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
    214     (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
    215     ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
    216     {
    217         if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    218         {
    219           if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    220             $this->FormManager->Type->RegisterType($Item['Type'], '',
    221               $this->FormManager->FormTypes[$Item['Type']]);
    222           if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
    223             $UseType = 'OneToMany';
    224           else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
    225             $UseType = 'Enumeration';
    226         } else $UseType = $Item['Type'];
     196    {
     197      if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
     198      (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
     199      ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
     200      {
     201        $UseType = $this->GetItemType($Item);     
    227202        $this->Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoadDb',
    228             array('Value' => $DbRow[$Index], 'Name' => $Index,
    229             'Type' => $Item['Type'], 'Values' => $this->Values));
     203          array('Value' => $DbRow[$Index], 'Name' => $Index,
     204          'Type' => $Item['Type'], 'Values' => $this->Values));
    230205        $this->ValuesFilter[$Index] = $DbRow[$Index.'_Filter'];
     206      }
    231207    }
    232208  }
     
    244220        $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index,
    245221          'Type' => $Item['Type'], 'Values' => $this->Values);
    246 
    247         if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    248         {
    249           if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    250             $this->FormManager->Type->RegisterType($Item['Type'], '',
    251               $this->FormManager->FormTypes[$Item['Type']]);
    252           if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
    253           {
    254             $UseType = 'OneToMany';
    255           }
    256           else if ($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
    257             $UseType = 'Enumeration';
    258         } else $UseType = $Item['Type'];
     222        $UseType = $this->GetItemType($Item);
    259223        $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnSaveDb', $Parameters);
    260224        if (($Item['Type'] == 'Password') and ($Values[$Index] == '')) unset($Values[$Index]);
     
    264228    {
    265229      $Values['Id'] = $Id;
    266       $DbResult = $this->Database->insert($this->Definition['Table'], $Values);
     230      $this->Database->insert($this->Definition['Table'], $Values);
    267231    } else
    268       $DbResult = $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values);
     232      $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values);
     233  }
     234
     235  function HasAllPostVariables(): bool
     236  {
     237    $Result = array_key_exists('submit', $_POST);
     238
     239    foreach ($this->Definition['Items'] as $Index => $Item)
     240    {     
     241      if (!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false;
     242      if ($Item['ReadOnly'] == false)
     243      if (!array_key_exists('Hidden', $Item) or ($Item['Hidden'] == false))
     244      if (!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
     245      (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
     246      ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
     247      {
     248        $UseType = $this->GetItemType($Item);
     249        if (!$this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnCanLoad',
     250          array('Value' => $this->Values[$Index], 'Name' => $Index,
     251          'Type' => $Item['Type'], 'Values' => $this->Values,
     252          'Filter' => $this->Values[$Index])))       
     253        {         
     254          $Result = false;
     255          break;
     256        }
     257      }     
     258    }
     259
     260    return $Result;
    269261  }
    270262
     
    290282        {
    291283          //if (array_key_exists($Context.$Index, $_POST))
    292           if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    293           {
    294             if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    295               $this->FormManager->Type->RegisterType($Item['Type'], '',
    296                 $this->FormManager->FormTypes[$Item['Type']]);
    297             $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type'];
    298             if ($CustomType == 'Reference')
    299               $UseType = 'OneToMany';
    300             else if ($CustomType == 'Enumeration')
    301               $UseType = 'Enumeration';
    302           } else $UseType = $Item['Type'];
     284          $UseType = $this->GetItemType($Item);
    303285          $Parameters = array('Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values);
    304286          if (array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null'];
     
    309291      } else
    310292      {
    311         if (isset($Item['Default'])) {
     293        if (isset($Item['Default']))
     294        {
    312295          if (isset($Item['Null']) and ($Item['Null'] == true))
    313296            $Values[$Index] = null;
     
    319302  }
    320303
     304  // Check if filled value is in valid form
    321305  function Validate(): bool
    322306  {
    323307    $Valid = true;
    324308    foreach ($this->Definition['Items'] as $Index => $Item)
    325     if ((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
    326     (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
    327     ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and
    328     (!array_key_exists('ReadOnly', $Item) or
    329     (array_key_exists('ReadOnly', $Item) and
    330     ($Item['ReadOnly'] != true))))
    331     {
     309    {
     310      if ((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
     311      (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
     312      ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and
     313      (!array_key_exists('ReadOnly', $Item) or
     314      (array_key_exists('ReadOnly', $Item) and
     315      ($Item['ReadOnly'] != true))))
     316      {
    332317        //if (array_key_exists($Context.$Index, $_POST))
    333         if (array_key_exists($Item['Type'], $this->FormManager->FormTypes))
    334         {
    335           if (!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
    336             $this->FormManager->Type->RegisterType($Item['Type'], '',
    337               $this->FormManager->FormTypes[$Item['Type']]);
    338           $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type'];
    339           if ($CustomType == 'Reference')
    340             $UseType = 'OneToMany';
    341           else if ($CustomType == 'Enumeration')
    342             $UseType = 'Enumeration';
    343         } else $UseType = $Item['Type'];
    344 
     318        $UseType = $this->GetItemType($Item);
    345319        $Parameters = array('Value' => $this->Values[$Index]);
    346320        if (array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null'];
    347321          else $Parameters['Null'] = false;
    348322        if (!$this->FormManager->Type->ExecuteTypeEvent($UseType, 'Validate',
    349           $Parameters)) {
    350             $this->ValuesValidate[$Index] = true;
    351             $Valid = false;
    352           }
    353     }
     323          $Parameters))
     324        {
     325          $this->ValuesValidate[$Index] = true;
     326          $Valid = false;
     327        }
     328      }
     329    }
    354330    if ($Valid == false) throw new Exception('not validated');
    355331    return $Valid;
    356332  }
    357333}
    358 
    359334
    360335function MakeLink(string $Target, string $Title): string
     
    386361  return $Result;
    387362}
    388 
    389 class FormManager
    390 {
    391   public array $Classes;
    392   public array $FormTypes;
    393   public Database $Database;
    394   public Type $Type;
    395   public string $RootURL;
    396   public bool $ShowRelation;
    397 
    398   function __construct(Database $Database)
    399   {
    400     $this->Database = &$Database;
    401     $this->Classes = array();
    402     $this->FormTypes = array();
    403     $this->Type = new Type($this);
    404     $this->ShowRelation = false;
    405   }
    406 
    407   function RegisterClass(string $Name, array $Class): void
    408   {
    409     $this->Classes[$Name] = $Class;
    410   }
    411 
    412   function UnregisterClass(string $Name): void
    413   {
    414     unset($this->Classes[$Name]);
    415   }
    416 
    417   function RegisterFormType(string $Name, array $Class): void
    418   {
    419     $this->FormTypes[$Name] = $Class;
    420   }
    421 
    422   function UnregisterFormType(string $Name): void
    423   {
    424     unset($this->FormTypes[$Name]);
    425   }
    426 
    427   function UpdateSQLMeta(): void
    428   {
    429     $this->Database->query('DELETE FROM ModelField');
    430     $this->Database->query('DELETE FROM Model');
    431     $this->Database->query('DELETE FROM DataType WHERE Parent IS NOT NULL');
    432     $this->Database->query('DELETE FROM DataType');
    433 
    434     foreach ($this->Type->TypeDefinitionList as $Name => $Type)
    435     {
    436       $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Name.'"');
    437       if ($DbResult->num_rows == 0)
    438       {
    439         $this->Database->insert('DataType', array('Name' => $Name,
    440           'Title' => $Type['Class']));
    441       } else
    442       {
    443         $DbRow = $DbResult->fetch_assoc();
    444         $this->Database->update('DataType', 'Id='.$DbRow['Id'], array('Name' => $Name,
    445           'Title' => $Type['Class']));
    446       }
    447     }
    448 
    449     foreach ($this->Classes as $Class)
    450     if (!array_key_exists('SQL', $Class) and ($Class['Table'] != ''))
    451     {
    452       if (!$this->Database->TableExists($Class['Table'])) continue;
    453 
    454       echo($Class['Table'].'<br/>');
    455       $Module = 1;
    456       $DbResult = $this->Database->select('Model', 'Id', 'Name="'.$Class['Table'].'"');
    457       if ($DbResult->num_rows == 0)
    458       {
    459         $this->Database->insert('Model', array('Name' => $Class['Table'], 'Title' => $Class['Title'], 'Module' => $Module));
    460         $Model = $this->Database->insert_id;
    461       } else
    462       {
    463         $DbRow = $DbResult->fetch_assoc();
    464         $Model = $DbRow['Id'];
    465         $this->Database->update('Model', 'Id='.$DbRow['Id'], array('Name' => $Class['Table'],
    466           'Title' => $Class['Title'], 'Module' => $Module));
    467       }
    468 
    469       foreach ($Class['Items'] as $Name => $Field)
    470       {
    471         echo($Name.', ');
    472         $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Field['Type'].'"');
    473         if ($DbResult->num_rows > 0)
    474         {
    475           $DbRow = $DbResult->fetch_assoc();
    476           $Type = $DbRow['Id'];
    477         } else {
    478           $Type = $this->FormTypes[$Field['Type']];
    479 
    480           // Search parent type
    481           $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Type['Type'].'"');
    482           if ($DbResult->num_rows > 0)
    483           {
    484             $DbRow = $DbResult->fetch_assoc();
    485             $ParentType = $DbRow['Id'];
    486           } else $ParentType = null;
    487 
    488           $this->Database->insert('DataType', array('Name' => $Field['Type'],
    489             'Title' => '', 'Parent' => $ParentType));
    490           $Type = $this->Database->insert_id;
    491         }
    492 
    493         $DbResult = $this->Database->select('ModelField', 'Id', '(Name="'.$Name.'") AND (Model='.$Model.')');
    494         if ($DbResult->num_rows == 0)
    495         {
    496           $this->Database->insert('ModelField', array('Name' => $Name,
    497             'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));
    498         } else
    499         {
    500           $DbRow = $DbResult->fetch_assoc();
    501           $this->Database->update('ModelField', 'Id='.$DbRow['Id'], array('Name' => $Name,
    502             'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));
    503         }
    504       }
    505       echo('<br/>');
    506     }
    507   }
    508 }
Note: See TracChangeset for help on using the changeset viewer.