%PDF- %PDF-
Direktori : /var/www/pn/utils/classes/ |
Current File : /var/www/pn/utils/classes/Project.php |
<?php namespace WebPappers\Project; use Exception; abstract class Project { public $wpdb; public $id; public $url; public $type; public $name; public $status; public $beneficiaryLevel;//need to clarify public $beneficiarySAI; public $intosaiRegion; public $briefDescription; public $description; public $implementingAgency; public $keywords; public $approved; public $allSaiRegionsDb; protected $dom; public function __construct($dom, $url) { global $wpdb; $this->wpdb = $wpdb; $this->dom = $dom; $this->url = $url; $this->id = $this->getProjectId(); $this->approved = $this->getApprovedStatus(); $this->type = $this->getType(); $this->implementingAgency = $this->getImplementingAgency(); $this->allSaiRegionsDb = $this->getAllSaiRegions(); $this->beneficiaryLevel = $this->getBeneficiaryLevel(); $this->beneficiarySAI = $this->getBeneficiarySAI(); $this->intosaiRegion = $this->getSaiRegionByCountry($this->beneficiarySAI); } public function getApprovedStatus() { $projectId = $this->getProjectId(); if(!$projectId){ return false; } $projectData = $this->getProjectBy('id', $projectId); return $projectData->approved; } public function getProjectId() { $project = $this->getProjectBy('url', $this->url); if(!$project){ return null; } return $project->id; } public function parseDataFromRemote($additionalData=NULL) { $this->name = $this->getName(); $this->status = $this->getStatus(); $this->beneficiarySAI = $this->getBeneficiarySAI(); $this->description = $this->getDescription(); if(!empty($additionalData['keywords'])){ $this->keywords = $additionalData['keywords']; } } public function setName($name) { $this->name = $name; } public function setStatus($status) { $this->status = $status; } public function getProjectBy($fieldName, $fieldValue) { $sql = "SELECT * from " . PROJECT_TABLE_NAME . " WHERE " . $fieldName . "='" . $fieldValue . "'"; $project = $this->wpdb->get_row( $sql, OBJECT ); return $project; } public function prepareData() { $data['fields'] = array( 'url' => $this->url, 'name' => $this->name, 'status' => $this->status, 'beneficiaryLevel' => $this->beneficiaryLevel, 'beneficiarySAI' => $this->beneficiarySAI, 'intosaiRegion' => $this->intosaiRegion, 'description' => $this->description, 'implementingAgency' => $this->implementingAgency, 'keywords' => $this->keywords, 'approved' => $this->approved, 'type' => $this->type ); // $data['fieldTypes'] = array( '%s', '%s', '%s', '%s', '%s' ); return $data; } public function save() { $data = $this->prepareData(); $data['fields']['created_at'] = date('Y-m-d H:i:s', time()); $data['fields']['updated_at'] = date('Y-m-d H:i:s', time()); $this->wpdb->insert( PROJECT_TABLE_NAME, $data['fields'], $data['fieldTypes'] ); $project = $this->getProjectBy('url', $this->url); $this->id = $project->id; return $this->id; } public function update() { $data = $this->prepareData(); $data['fields']['updated_at'] = date('Y-m-d H:i:s', time()); $project = $this->getProjectBy('url', $this->url); if(!$project){ throw new \Exception("Project with field: url and value: " . $this->url .' not found'); } $this->wpdb->update( PROJECT_TABLE_NAME, $data['fields'], array( 'ID' => $project->id ) ); } public function getProjectDataById($id) { $sql = "SELECT * FROM " . PROJECT_TABLE_NAME . " WHERE id=".$id; $projectData = $this->wpdb->get_row( $sql, OBJECT ); return $projectData; } public function updateProjectPost() { $projectData = $this->getProjectDataById($this->id); if(empty($projectData->post_id)){ return; } $projectPost = array(); $projectPost['ID'] = $projectData->post_id; $projectPost['post_title'] = $projectData->name; $projectPost['post_content'] = $projectData->description; // Обновляем данные в БД wp_update_post( wp_slash($projectPost) ); //Update ACF update_field('field_59e86f0e28f89', $projectData->status, $projectData->post_id);//project_status //Status of project* update_field('field_59e872cdc6c3d', $projectData->implementingAgency, $projectData->post_id);//imp_agencys //Implementing agency(s) } public function delete() { if(!$this->id){ throw new Exception('Project not saved yet'); } $this->wpdb->delete( PROJECT_TABLE_NAME, array( 'id' => $this->id ) ); } public function getAllSaiRegions() { $regionsPath = PARSER_PATH . '/regions.csv'; if(!file_exists($regionsPath)){ throw new Exception('File: '.$regionsPath.' with regions does not exist.'); } $regions = array(); $regionsRaw = file($regionsPath); foreach($regionsRaw as $region){ $regions[] = explode(',', $region); } return $regions; } public function getSaiRegionByCountry($country) { $country = strtolower($country); foreach($this->allSaiRegionsDb as $region){ $dbCountry = strtolower($region[0]); $dbCountry = trim($dbCountry); if($dbCountry == $country){ return trim($region[1]); } } } public function saveDataUrl($url) { $projectId = $this->getProjectId(); $this->wpdb->update( PROJECT_TABLE_NAME, array( 'data_url' => $url ), array( 'id' => $projectId ) ); } abstract public function getName(); abstract public function getStatus(); abstract public function getBeneficiarySAI(); abstract public function getDescription(); abstract public function getType(); abstract public function getImplementingAgency(); abstract public function getBeneficiaryLevel(); }