Your IP : 216.73.216.91


Current Path : /var/www/html/llcfapp/api/controller/
Upload File :
Current File : /var/www/html/llcfapp/api/controller/Family.php

<?php
class Family extends BaseController implements IValidateParams{
	public function createOne($data){
		unset($data['id']);
		$this->response = $this->insert(Config::getTable('family'), $data, false);
		return $this->response;
	}
	public function getOneById($data){
		$this->response = $this->select(Config::getTable('family'), ["*"], "WHERE id = ?", [$data['id']]);
		return $this->response;
	}
	public function getOneByCnic($data){
		$this->response = $this->select(Config::getTable('family'), ["*","id as family_id"], "WHERE father_cnic = ?", [$data['father_cnic']]);
		return $this->response;
	}
	public function getServiceById($data){
		$table = Config::getTable('family');
		$tableFamSer = Config::getTable('family_service');
		$qry = <<<QRY
			SELECT 
				fs.*, 
				f.father_cnic, 
				f.father_name, 
				f.father_phone, 
				f.father_occup, 
				f.address,
				f.mother_name,
				f.children_count,
				f.religion,
				f.zakat
			FROM {$tableFamSer} fs
			INNER JOIN {$table} f 
				on f.id = fs.family_id
			WHERE fs.id = ?
		QRY;
		$this->response = $this->customSelect($qry, [$data['service_id']]);
		return $this->response;
	}
	public function updateOne($data){
		$id = $data['id'];
		unset($data['id']);
		$this->response = $this->update(Config::getTable('family'), $data, "WHERE id = ?", [$id]);
		return $this->response;
	}
	public function getAll($data){
		$table = Config::getTable('family');		
		$DTTotalCount = $this->customSelect("select count(*) as count from {$table}")['result'][0]->count;
		if(!empty($data['search']['value'])){
			$extraCols = [
				"(select group_concat(CONCAT('GR# ', gr_num) SEPARATOR ' | ') from student where family_id = family.id) as admitted"
			];
			$fullTextDT = $this->fullTextSearchDTQuery($data, $table, $extraCols);
			$this->response = $this->customSelect($fullTextDT['paging']);
			$DTFilteredCount = $this->customSelect($fullTextDT['where'])['rowsAffected'];
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}else{
			$DTDataQry = "Select f.*, f.id as DT_RowId, (select group_concat(CONCAT('GR# ', gr_num) SEPARATOR ' | ') from student where family_id = f.id) as admitted from {$table} f {$this->transformDTQuery($data)['paging']}";
			$DTFilteredCount = $this->customSelect("select count(*) as count from {$table} {$this->transformDTQuery($data)['where']}")['result'][0]->count;
			$this->response = $this->customSelect($DTDataQry);
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}
		if($this->response['status'] == 'failure'){
			$DTResponse['error'] = $this->response['msg'];
		}
		return $DTResponse;
	}
	public function getAllServices($data){
		$table = Config::getTable('family');		
		$tableFamSer = Config::getTable('family_service');		
		$DTTotalCountQry = <<<QRY
				SELECT 
					count(*) as count
						FROM {$tableFamSer}
					left join {$table}
						on {$table}.id = {$tableFamSer}.family_id
			QRY;
		$DTTotalCount = $this->customSelect($DTTotalCountQry)['result'][0]->count;
		// $DTTotalCount = $this->customSelect("select count(*) as count from {$table}")['result'][0]->count;
		if(!empty($data['search']['value'])){
			$gs = $this->globalSearch($data, $tableFamSer);
			$this->response = $this->customSelect($gs['paging']);
			$this->response = $this->prefixColumnWithTableAndMapReqParamInResponse($data['columns'], $this->response);
			$DTFilteredCount = $this->customSelect($gs['where'])['rowsAffected'];
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}else{
			$DTDataQry = <<<QRY
				SELECT 
					{$tableFamSer}.id as '{$tableFamSer}||id',
					{$tableFamSer}.id as '{$tableFamSer}||service_id',
					{$tableFamSer}.service as '{$tableFamSer}||service',
					{$tableFamSer}.date as '{$tableFamSer}||date', 
					{$tableFamSer}.family_id as '{$tableFamSer}||family_id', 
					{$table}.father_cnic as '{$table}||father_cnic', 
					{$table}.father_name as '{$table}||father_name', 
					{$table}.father_phone as '{$table}||father_phone', 
					{$table}.father_occup as '{$table}||father_occup', 
					{$table}.address as '{$table}||address',
					{$table}.mother_name as '{$table}||mother_name',
					{$table}.children_count as '{$table}||children_count',
					{$table}.religion as '{$table}||religion',
					{$table}.zakat as '{$table}||zakat'
				FROM {$tableFamSer}
				INNER JOIN {$table}
					on {$table}.id = {$tableFamSer}.family_id
				{$this->transformDTQuery($data)['paging']}
			QRY;
			// $DTDataQry = "Select *, id as DT_RowId from {$table} {$this->transformDTQuery($data)['paging']}";
			$this->response = $this->customSelect(trim($DTDataQry));

			$DTFilteredCountQry = <<<QRY
				SELECT 
					count(*) as count
						FROM {$tableFamSer}
					left join {$table}
						on {$table}.id = {$tableFamSer}.family_id
					{$this->transformDTQuery($data)['where']}
			QRY;
			$DTFilteredCount = $this->customSelect(trim($DTFilteredCountQry))['result'][0]->count;
			// $DTFilteredCount = $this->customSelect("select count(*) as count from {$table} {$this->transformDTQuery($data)['where']}")['result'][0]->count;
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}
		if($this->response['status'] == 'failure'){
			$DTResponse['error'] = $this->response['msg'];
		}
		return $DTResponse;
	}
	public function createService($data){
		$tableFamSer = Config::getTable('family_service');
		$data = $this->unsetArrayKeys($data, ['service_id', 'father_cnic', 'father_name', 'father_phone', 'father_occup', 'address', 'mother_name', 'children_count', 'religion', 'zakat']);
		$this->response = $this->insert($tableFamSer, $data);
		return $this->response;
	}
	public function updateService($data){
		$tableFamSer = Config::getTable('family_service');
		$id = $data['service_id'];
		$data = $this->unsetArrayKeys($data, ['service_id', 'father_cnic', 'father_name', 'father_phone', 'father_occup', 'address', 'mother_name', 'children_count', 'religion', 'zakat']);
		$this->response = $this->update($tableFamSer, $data, "WHERE id = ?", [$id]);
		return $this->response;
	}
	public function validateRequestsParams(){
		$this->mendetoryParamsAgainstEachMethod = [
			'createOne' => [
				"father_cnic", 
				"father_name",
				"father_phone",
				"father_occup",
				"address",
				"children_count",
				"mother_name",
				"mother_occup",
				"emerg_name",
				"emerg_relation",
				"emerg_num",
			],
			'updateOne' => [
				"father_cnic", 
				"father_name",
				"father_phone",
				"father_occup",
				"address",
				"children_count",
				"mother_name",
				"mother_occup",
				"emerg_name",
				"emerg_relation",
				"emerg_num",
			],
			'getOneById' => ['id'],
			'getOneByCnic' => ['father_cnic'],
			'getAll' => [],
			'createService' => [
				'family_id',
				'service',
				'date'
			],
			'updateService' => [
				'family_id',
				'service',
				'date'
			],
			'getAllServices' => [],
			'getServiceById' => ['service_id'],
		];
		return $this->mendetoryParamsAgainstEachMethod;
	}
}
?>