<?php

namespace Types;

class DocType {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function getOrInsert($x) { 
    
    
    // --- BLOCK: main ---
    
		$id = \FOP\Database::query('SELECT '.qi('ID').' FROM '.qi('docType').' WHERE name = ' .  \FOP\Database::quote($x))->fetchColumn();
		if (!$id) {
			// Element nicht vorhanden => anlegen
			$new = \Tables\DocType::getDefaults();
			$new['name'] = $x;
			\Tables\DocType::sanitizeDefaults($new, false);
			$id = \Tables\DocType::create($new);
		}
		return $id;
	
    
  }

  public static function validate($x) { 
    
    
    // --- BLOCK: main ---
    
		if ($x != 0 && \FOP\Database::query('SELECT 1 FROM '.qi('docType').' WHERE ((('.\FOP\Database::quote(\FOP\User::getDokument()).'))) AND '.qi('ID').' = ' . intval($x))->fetchColumn() != 1) {
			throw new \FOP\AccessDeniedException(_('invalidinput'));
		}
	
    
  }

  public static function parse($x) { 
    
    
    // --- BLOCK: main ---
    
		
		$result = \FOP\Database::query('SELECT '.qi('ID').' FROM '.qi('docType').' WHERE ('.qi('docType').'.'.qi('name').') = ' . \FOP\Database::quote($x))->fetchColumn();
		if ($result) return $result;
		$result = \FOP\Database::query('SELECT '.qi('ID').' FROM '.qi('docType').' WHERE '.qi('ID').' = ' . \FOP\Database::quote($x))->fetchColumn();
		if ($result) return $result;
		return null;
	
    
  }

  public static function descriptionAction($id) { 
    
    
    // --- BLOCK: access ---
    if(!(((\FOP\User::getDokument())))) throw new \FOP\AccessDeniedException();
    // --- BLOCK: main ---
    
		$data = \FOP\Database::query('SELECT '.qi('docType').'.'.qi('name').' AS description, (('.\FOP\Database::quote(\FOP\User::getDokument()).')) AS '.qi('canView').' FROM '.qi('docType').' WHERE '.qi('ID').' = ' . \FOP\Database::quote($id))->fetch(\PDO::FETCH_ASSOC);
		return $data['canView'] ? $data['description'] : null;
	
    
  }

  public static function getItems($deps, $value = null , $search = '' , $conditionSQL = '' ) { 
    
    
    // --- BLOCK: main ---
    
		
		$limit = 200;
		$sqlbase = 'SELECT '.qi('ID').' AS '.qi('ID').', '.qi('docType').'.'.qi('name').' AS description, 0 AS priority FROM '.qi('docType').' WHERE ' . ($conditionSQL ?: '(('.\FOP\Database::quote(\FOP\User::getDokument()).'))');
		$sqladdon = '';
		if (!is_null($deps)) {
			// bei Filtern werden die Conditions außen vor gelassen (TODO: abhängig davon machen, ob eine ver-AND-ete Condition ein deps:: enthält)
			$sqlbase .= ' AND (true)';
			$sqladdon .= ' AND (true)';
		}
		$sqladdon .= ' ORDER BY '.qi('docType').'.'.qi('sortableOrder').' ';
		$result = \FOP\Database::query($sqlbase . $sqladdon . ' LIMIT ' . intval($limit))->fetchAll(\PDO::FETCH_ASSOC);
		
			// Null-Wert am Anfang hinzufügen
			array_unshift($result, [
				'ID' => '0',
				'description' => _('-'),
				'description1' => _('-'),
				'description2' => _('-'),
				'priority' => 0
			]);
		
		$values = [];
		foreach ($result AS $item) {
			$values[$item['ID']] = true; // build up a lookup list
		}
		if (count($result) == $limit) {
			// render '...' when limit is reached
			array_push($result, ['ID' => '_more', 'description' => '...', 'description1' => '...', 'description2' => '...', 'priority' => -1]);
		}
		if ($search) {
			// add searched items
			$addons = \FOP\Database::query($sqlbase . ' AND ('.qi('docType').'.'.qi('name').' '.(\FOP\Database::getDriver() == 'pgsql' ? 'ILIKE' : 'LIKE').' '.\FOP\Database::quote('%'.$search.'%').')' . $sqladdon . ' LIMIT ' . intval($limit))->fetchAll(\PDO::FETCH_ASSOC);
			foreach ($addons AS $item) {
				if (!isset($values[$item['ID']])) {
					array_push($result, $item);
				}
			}
		}
		if ($value != -1 && !isset($values[$value])) {
			// $value was not in list: prepend
			$ownValue = \FOP\Database::query($sqlbase . ' AND '.qi('ID').' = ' . \FOP\Database::quote($value ?: 0))->fetch(\PDO::FETCH_ASSOC);
			if ($value != '0') {
			if (!$ownValue) {
				// eigener Wert ist verboten, wird aber zur Speicherung mit hinzugefügt
				$ownValue = [
					'ID' => $value,
					'description' => (_('pSelect')),
					'description1' => (_('pSelect')),
					'description2' => (_('pSelect')),'priority' => -1,
					'forbidden' => true
				];
			}
			}
			if ($ownValue) {
				array_unshift($result, $ownValue);
			}
		}
		return $result;
	
    
  }


  
  

  
  
}



