<?php

namespace Types;

class Campaign {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function validate($x) { 
    
    
    // --- BLOCK: main ---
    
		if (\FOP\Database::query('SELECT 1 FROM '.qi('campaign').' WHERE ((('.\FOP\Database::quote(\FOP\User::getCampaign()).'))) 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('campaign').' WHERE ('.qi('campaign').'.'.qi('name').') = ' . \FOP\Database::quote($x))->fetchColumn();
		if ($result) return $result;
		$result = \FOP\Database::query('SELECT '.qi('ID').' FROM '.qi('campaign').' WHERE '.qi('ID').' = ' . \FOP\Database::quote($x))->fetchColumn();
		if ($result) return $result;
		return null;
	
    
  }

  public static function descriptionAction($id) { 
    
    
    // --- BLOCK: access ---
    if(!(((\FOP\User::getCampaign())))) throw new \FOP\AccessDeniedException();
    // --- BLOCK: main ---
    
		$data = \FOP\Database::query('SELECT '.qi('campaign').'.'.qi('name').' AS description, (('.\FOP\Database::quote(\FOP\User::getCampaign()).')) AS '.qi('canView').' FROM '.qi('campaign').' 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('campaign').'.'.qi('name').' AS description, 0 AS priority FROM '.qi('campaign').' WHERE ' . ($conditionSQL ?: '(('.\FOP\Database::quote(\FOP\User::getCampaign()).'))');
		$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('campaign').'.'.qi('pinned').' DESC, '.qi('campaign').'.'.qi('lastUpdate').' DESC';
		$result = \FOP\Database::query($sqlbase . $sqladdon . ' LIMIT ' . intval($limit))->fetchAll(\PDO::FETCH_ASSOC);
		
		$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('campaign').'.'.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 (!$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;
	
    
  }


  
  

  
  
}



