<?php
require_once __DIR__ . "/System.php";
if(!class_exists("%CLASS_NAME%")){
  class %CLASS_NAME% extends System{

    // This function uses the System search() function to fetch all columns from the db
		public function info($id){ 
		    return $this->search("*", "%TABLE_NAME%", "id", "=", $id); // $select, from $table, where $column $sign $value
		}

    // This function is used so create & edit can both filter out any data the same exact way
    public function prepareData($data, $protocol){
			if($protocol == "create"){
%PROTOCOL_CREATE_PHP%
			}
			
%PREP_DATA_SANITIZE_PHP%
      return $data; // the data is checked & prepared when applicable (ex: password hash)
    }

    public function create($data){
			if(($data = $this->prepareData($data, "create")) == false){
        return false; 
      }
      $time = time();
      $sql = $this->db->prepare("INSERT INTO %TABLE_NAME% (created_time,creator_user_id, %TABLE_COLUMNS_STRING%) VALUES (?,?,%TABLE_BIND_CREATE_QUESTIONMARKS%)");
      $sql->bind_param("si%TABLE_BIND_PARAM_TYPES%", $time,$_SESSION['userID'],%TABLE_BIND_PARAM_VARIABLES%);
      $sql->execute();
      if($sql->affected_rows > 0){
        $Activities = $this->loadClass("Activities");
        $activity_data = array(
    	    "created_time"=>$time,
    	    "creator_user_id"=>$_SESSION['userID'],
    	    "activity_label"=>"Created %LANG_SINGULAR%",
    	    "feature_uid"=>$this->feature_uid,
    	    "item_id"=>$sql->insert_id
    	    );
				$Activities->create($activity_data); 
        return true;
      } else {
        $this->submitError("The system could not insert the new %LANG_SINGULAR% into the database.");
        return false;
      }
      $this->submitError("An unusual error was detected. Please try again");
      return false;
    }

    public function edit($id, $data){
      if(($info = $this->info($id)) !== false){ // get existing info to add to our edit
        foreach($data as $column => $value){
          if($value == $info[0][$column]){
            unset($data[$column]); // so we don't overwrite values / resanitize for no reason 
          }
        }
        if(count($data) == 0){ // Nothing to update, all values are the same...
          $this->submitError("You have not changed any %LANG_SINGULAR% settings to update.");
          return false; // nothing to update
        } else if(($data = $this->prepareData($data, "edit")) == false){ // prepare and check the data
          return false; // use echo $%CLASS_NAME%->error in controller to check for the error msg
        }
        $data = array_merge($info[0], $data); // use new data sent to func, but use info() data if missing
        $sql = $this->db->prepare("UPDATE %TABLE_NAME% SET %TABLE_BIND_EDIT_QUESTIONMARKS% WHERE id = ?");
        $sql->bind_param("%TABLE_BIND_PARAM_TYPES%i", %TABLE_BIND_PARAM_VARIABLES%, $id);
        $sql->execute();
        if($sql->affected_rows > 0){
          return true;
        } else {
          echo $sql->error;
          $this->submitError("The system was unable to modify this %LANG_SINGULAR%.");
          return false;
        }
      } else {
        $this->submitError("We could not find that %LANG_SINGULAR% with the supplied ID");
        return false;
      }
      $this->submitError("An unknown error was detected. Please try again.");
      return false;
    }


    public function delete($id){
      if(($info = $this->info($id)) == false || !is_array($info) || count($info) == 0){
        $this->submitError("We could not find this %LANG_SINGULAR% in the database. There was nothing to delete.");
        return false;
      } else {
          $sql = $this->db->prepare("DELETE FROM %TABLE_NAME% WHERE id = ?");
          $sql->bind_param("i", $id);
          $sql->execute();
          if($sql->affected_rows > 0){
            $Activities = $this->loadClass("Activities");
            $activity_data = array(
        	    "created_time"=>$time,
        	    "creator_user_id"=>$_SESSION['userID'],
        	    "activity_label"=>"Removed %LANG_SINGULAR% ID #$id",
        	    "feature_uid"=>$this->feature_uid,
        	    "item_id"=>$id
        	    );
    				$Activities->create($activity_data); 
            return true;
          } else {
            $this->submitError("There was an error attempting to delete this %LANG_SINGULAR% in the database. Please try again.");
            return false;
          }
      }
      $this->submitError("An unusual error was detected. Please try again.");
      return false;
    }
		
		public function htmlDropdown($preselect = NULL){
			$list = $this->query("SELECT id, %PRIMARY_COLUMN% FROM %TABLE_NAME%");
			if(is_array($list) && ($numResults = count($list)) > 0){
				for($x=0;$x<$numResults;$x++){
						if($preselect && $preselect == $list[$x]['id']){
								$results .= "<option value='$preselect' selected='selected'>{$list[$x]['%PRIMARY_COLUMN%']}</option>";
						} else {
								$results .= "<option value='{$list[$x]['id']}'>{$list[$x]['%PRIMARY_COLUMN%']}</option>";
						}
				}
				if($preselect == NULL){
					$results = "<option disabled='disabled' selected='selected'>Please Choose...</option>".$results;
				}
				return $results;
			}  else {
				$this->submitError("No results were found.");
				return "<option selected='selected' disabled='disabled'>No Available %LANG_PLURAL%</option>";
			}
			return false;
		}

    public function __construct(){
      parent::__construct();
      $this->feature_uid = "%UUID%";
    }

  }
}
?>