Commit 99eccbaf authored by Simon's avatar Simon

Salary type added

parent 06bd4cc3
<?php
class JsonDataCollector
{
// Job properties data map
protected $properties = [
'livas-id' => 'id',
'valid' => 'galioja',
'post_date' => 'data',
'post_modified' => 'atnaujinta',
'title' => 'pareigos',
'post_content' => 'aprasymas',
'slug' => 'seo_url',
'contact' => 'contacts',
'salary_from' => 'atlygis_nuo',
'salary_to' => 'atlygis_iki',
'video_url' => 'video_url',
];
// Taxonomies data map
protected $terms = [
'city' => [
'slug' => 'miestas_id',
'name' => 'regionas',
'__primary' => true,
'additionalCities' => [
'slug' => 'city_id',
'name' => 'city',
]
],
'field' => [
'slug' => 'kategorija',
'name' => 'sritis',
'__primary' => true,
'additionalCategories' => [
'slug' => 'category_id',
'name' => 'category_name',
]
],
'company' => [
'name' => 'imone'
],
'type' => [
'types' => [
'slug' => 'type_id',
'name' => 'type_name'
]
],
];
// Yoast SEO properties
protected $seoProperties = [
'metadesc' => 'seo_description',
'title' => 'seo_title'
];
// Custom posts data map
protected $posts = [
'division' => [
'title' => 'padalinys_name',
'slug' => 'padalinys'
],
];
// Language map
protected $lang = 'kalba';
// Relations mapping
protected $relationsMap = [
'division-id' => 'division',
];
// Source data
protected $sourceData;
protected $data;
public function __construct($source)
{
$this->setData($source);
$this->data = $this->transformData();
}
/**
* Set data source:
* get data from source and transform into array
*
* @param $source
*/
protected function setData($source)
{
$this->sourceData = $this->loadData($source);
}
/**
* Parse xml data
*
* @param $source
*
* @return SimpleXMLElement
*/
protected function loadData($source)
{
$context = stream_context_create([
'http' => [
'header' => 'Accept: application/json',
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
$json = file_get_contents($source, false, $context);
return json_decode($json);
}
/**
* Get data
*
* @return mixed
*/
public function getData()
{
return $this->data;
}
protected function transformData()
{
$result = [];
foreach ($this->sourceData as $ad) {
$item = [];
$item['properties'] = $this->getProperties($ad);
$item['lang'] = $ad->{$this->lang};
$item['terms'] = $this->getTerms($ad);
$item['posts'] = $this->getPosts($ad);
$item['relations'] = $this->relationsMap;
$item['seo_properties'] = $this->getSeoProperties($ad);
$result[] = $item;
};
return $result;
}
private function getProperties($ad)
{
$result = [];
foreach ($this->properties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
}
$result[$propertyKey] = $this->getValue($dataValue);
}
return $result;
}
private function getSeoProperties($ad)
{
$result = [];
foreach ($this->seoProperties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
}
$result[$propertyKey] = $this->getValue($dataValue);
}
return $result;
}
private function getTerms($ad)
{
$result = [];
foreach ($this->terms as $termKey => $termProperties) {
$entity = $this->getEntity($ad, $termProperties);
if ( ! empty($entity)) {
$result[$termKey][] = $entity;
}
$appends = array_filter($termProperties, function($item) {
return is_array($item);
});
foreach ($appends as $propertyKey => $propertyValues) {
if (isset($ad->{$propertyKey}) && !empty($ad->{$propertyKey})) {
foreach ($ad->{$propertyKey} as $data) {
$result[$termKey][] = $this->getEntity($data, $propertyValues);
}
}
}
}
return $result;
}
private function getEntity($ad, $propertyMap)
{
$entity = [];
foreach ($propertyMap as $propertyKey => $propertyValue) {
if ( ! is_array($propertyValue)) {
if (preg_match('/^\_\_/', $propertyKey)) {
$entity[$propertyKey] = $propertyValue;
}
elseif (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
$entity[$propertyKey] = $this->getValue($dataValue);
}
}
}
return $entity;
}
private function getPosts($ad)
{
$result = [];
foreach ($this->posts as $postKey => $postProperties) {
foreach ($postProperties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
$result[$postKey][$propertyKey] = $this->getValue($dataValue);
}
}
}
return $result;
}
private function getValue($value)
{
return (is_array($value)) ? $value : html_entity_decode($value);
}
}
<?php
class JsonDataCollector
{
// Job properties data map
protected $properties = [
'livas-id' => 'id',
'valid' => 'galioja',
'post_date' => 'data',
'post_modified' => 'atnaujinta',
'title' => 'pareigos',
'post_content' => 'aprasymas',
'slug' => 'seo_url',
'contact' => 'contacts',
'salary_from' => 'atlygis_nuo',
'salary_to' => 'atlygis_iki',
'video_url' => 'video_url',
'salary_type' => 'salary_type',
];
// Taxonomies data map
protected $terms = [
'city' => [
'slug' => 'miestas_id',
'name' => 'regionas',
'__primary' => true,
'additionalCities' => [
'slug' => 'city_id',
'name' => 'city',
]
],
'field' => [
'slug' => 'kategorija',
'name' => 'sritis',
'__primary' => true,
'additionalCategories' => [
'slug' => 'category_id',
'name' => 'category_name',
]
],
'company' => [
'name' => 'imone'
],
'type' => [
'types' => [
'slug' => 'type_id',
'name' => 'type_name'
]
],
];
// Yoast SEO properties
protected $seoProperties = [
'metadesc' => 'seo_description',
'title' => 'seo_title'
];
// Custom posts data map
protected $posts = [
'division' => [
'title' => 'padalinys_name',
'slug' => 'padalinys'
],
];
// Language map
protected $lang = 'kalba';
// Relations mapping
protected $relationsMap = [
'division-id' => 'division',
];
// Source data
protected $sourceData;
protected $data;
public function __construct($source)
{
$this->setData($source);
$this->data = $this->transformData();
}
/**
* Set data source:
* get data from source and transform into array
*
* @param $source
*/
protected function setData($source)
{
$this->sourceData = $this->loadData($source);
}
/**
* Parse xml data
*
* @param $source
*
* @return SimpleXMLElement
*/
protected function loadData($source)
{
$context = stream_context_create([
'http' => [
'header' => 'Accept: application/json',
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
$json = file_get_contents($source, false, $context);
return json_decode($json);
}
/**
* Get data
*
* @return mixed
*/
public function getData()
{
return $this->data;
}
protected function transformData()
{
$result = [];
foreach ($this->sourceData as $ad) {
$item = [];
$item['properties'] = $this->getProperties($ad);
$item['lang'] = $ad->{$this->lang};
$item['terms'] = $this->getTerms($ad);
$item['posts'] = $this->getPosts($ad);
$item['relations'] = $this->relationsMap;
$item['seo_properties'] = $this->getSeoProperties($ad);
$result[] = $item;
};
return $result;
}
private function getProperties($ad)
{
$result = [];
foreach ($this->properties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
}
$result[$propertyKey] = $this->getValue($dataValue);
}
return $result;
}
private function getSeoProperties($ad)
{
$result = [];
foreach ($this->seoProperties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
}
$result[$propertyKey] = $this->getValue($dataValue);
}
return $result;
}
private function getTerms($ad)
{
$result = [];
foreach ($this->terms as $termKey => $termProperties) {
$entity = $this->getEntity($ad, $termProperties);
if ( ! empty($entity)) {
$result[$termKey][] = $entity;
}
$appends = array_filter($termProperties, function($item) {
return is_array($item);
});
foreach ($appends as $propertyKey => $propertyValues) {
if (isset($ad->{$propertyKey}) && !empty($ad->{$propertyKey})) {
foreach ($ad->{$propertyKey} as $data) {
$result[$termKey][] = $this->getEntity($data, $propertyValues);
}
}
}
}
return $result;
}
private function getEntity($ad, $propertyMap)
{
$entity = [];
foreach ($propertyMap as $propertyKey => $propertyValue) {
if ( ! is_array($propertyValue)) {
if (preg_match('/^\_\_/', $propertyKey)) {
$entity[$propertyKey] = $propertyValue;
}
elseif (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
$entity[$propertyKey] = $this->getValue($dataValue);
}
}
}
return $entity;
}
private function getPosts($ad)
{
$result = [];
foreach ($this->posts as $postKey => $postProperties) {
foreach ($postProperties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
$result[$postKey][$propertyKey] = $this->getValue($dataValue);
}
}
}
return $result;
}
private function getValue($value)
{
return (is_array($value)) ? $value : html_entity_decode($value);
}
}
<?php
/*
Plugin Name: Biuro Jobs importer
description: A plugin for import jobs ads from Biuro endpoint
Version: 1.0.0
*/
set_time_limit(300);
// Data collector: prepare data for import
require_once 'JsonDataCollector.php';
// Data importer: imports data to WP posts
require_once 'JobsImporter.php';
// Initial data importer
require_once 'importInitData.php';
if (function_exists('importer_admin_menu')) {
// Add item to admin menu
add_action('admin_menu', 'importer_admin_menu');
}
/**
* Set admin menu item for plugin
*/
function importer_admin_menu()
{
add_submenu_page(
'options-general.php',
'Biuro Jobs importer',
'Biuro Jobs importer',
'data_importer',
'jobs-importer',
'do_import'
);
}
/**
* Imports posts from Biuro endpoint
*/
function do_import()
{
$env = getEnvironment();
$inputFile = getSource($env);
print_r("<div style='background: lightgrey;'>
<small style='float: right;'>Working in <strong>$env</strong> mode</small>
<div style='clear: both;'></div>
</div>");
echo "<pre>";
print_r('<h4>Biuro Jobs importer</h4>');
// if (pll_current_language() != '')
// {
// print_r("<h5 class='warning'>Polylang must be set in 'All languages' mode.</h5>");
// pll_the_languages( array( 'dropdown' => 1 ) );
// exit();
// }
print_r("Data source: $inputFile<br><br>");
// JSON reader
$ads = (new JsonDataCollector($inputFile))->getData();
// print_r($ads);
print_r("Found " . count($ads) . " ads from Biuro.<br>");
if (count($ads) > 0) {
if (empty($_POST)) {
// Form for import start, set polylang to 'All lang.' for action
print_r("<form method='post' action='" . current_location() . "&lang=all'>");
print_r("<div style='margin-top: 10px;'><button name='import' value='true'>Import</button></div>");
print_r("</form>");
}
// for cron, needs add to url ...&lang=all&proceed=1
if (isset($_POST['import']) || isset($_GET['proceed'])) {
print_r("importing....<br><br>");
// Posts (via Pods framework) creator
(new JobsImporter($ads))->import();
}
} else {
print_r("<br>There is nothing more to do.");
}
if (isset($_GET['init'])) {
print_r("<p><h3>Importing initial data:</h3>");
(new importInitData())->import();
print_r("</p>");
}
echo "</pre>";
}
function getSource($env)
{
$url = parse_url(site_url());
$exploded = explode('.', $url['host']);
$site = strtoupper(end($exploded));
switch ($site) {
case 'LT':
$script = 'new_wp.php';
break;
case 'LV':
$script = 'new_wp_lv.php';
break;
case 'EE':
$script = 'new_wp_ee.php';
break;
}
// Set data source path by environment type
if ($env === 'dev') {
// $inputFile = plugin_dir_path(__FILE__) . "source_data/wp_biuro.php.xml";
// $inputFile = "https://base.biuro.lt/_export/wp_biuro.php";
$inputFile = "https://base.biuro.lt/_export/{$script}";
} elseif ($env === 'prod') {
// $inputFile = "http://export.biuro.lt/wp_biuro.php";
$inputFile = "http://export.biuro.lt/{$script}";
}
return $inputFile;
}
function getEnvironment()
{
// Set environment type
$env = 'prod';
//Set dev env. only on dev subdomain
$subDomain = strstr($_SERVER['SERVER_NAME'], '.biuro', true);
if ($subDomain == 'dev') {
$env = 'dev';
}
return $env;
}
function current_location()
{
if (isset($_SERVER['HTTPS']) &&
($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
<?php
/*
Plugin Name: Biuro Jobs importer
description: A plugin for import jobs ads from Biuro endpoint
Version: 1.0.0
*/
set_time_limit(300);
// Data collector: prepare data for import
require_once 'JsonDataCollector.php';
// Data importer: imports data to WP posts
require_once 'JobsImporter.php';
// Initial data importer
require_once 'importInitData.php';
if (function_exists('importer_admin_menu')) {
// Add item to admin menu
add_action('admin_menu', 'importer_admin_menu');
}
/**
* Set admin menu item for plugin
*/
function importer_admin_menu()
{
add_submenu_page(
'options-general.php',
'Biuro Jobs importer',
'Biuro Jobs importer',
'data_importer',
'jobs-importer',
'do_import'
);
}
/**
* Imports posts from Biuro endpoint
*/
function do_import()
{
$env = getEnvironment();
$inputFile = getSource($env);
print_r("<div style='background: lightgrey;'>
<small style='float: right;'>Working in <strong>$env</strong> mode</small>
<div style='clear: both;'></div>
</div>");
echo "<pre>";
print_r('<h4>Biuro Jobs importer</h4>');
// if (pll_current_language() != '')
// {
// print_r("<h5 class='warning'>Polylang must be set in 'All languages' mode.</h5>");
// pll_the_languages( array( 'dropdown' => 1 ) );
// exit();
// }
print_r("Data source: $inputFile<br><br>");
// JSON reader
$ads = (new JsonDataCollector($inputFile))->getData();
// print_r($ads);
print_r("Found " . count($ads) . " ads from Biuro.<br>");
if (count($ads) > 0) {
if (empty($_POST)) {
// Form for import start, set polylang to 'All lang.' for action
print_r("<form method='post' action='" . current_location() . "&lang=all'>");
print_r("<div style='margin-top: 10px;'><button name='import' value='true'>Import</button></div>");
print_r("</form>");
}
// for cron, needs add to url ...&lang=all&proceed=1
if (isset($_POST['import']) || isset($_GET['proceed'])) {
print_r("importing....<br><br>");
// Posts (via Pods framework) creator
(new JobsImporter($ads))->import();
}
} else {
print_r("<br>There is nothing more to do.");
}
if (isset($_GET['init'])) {
print_r("<p><h3>Importing initial data:</h3>");
(new importInitData())->import();
print_r("</p>");
}
echo "</pre>";
}
function getSource($env)
{
$url = parse_url(site_url());
$exploded = explode('.', $url['host']);
$site = strtoupper(end($exploded));
switch ($site) {
case 'LT':
$script = 'new_wp.php';
break;
case 'LV':
$script = 'new_wp_lv.php';
break;
case 'EE':
$script = 'new_wp_ee.php';
break;
}
// Set data source path by environment type
if ($env === 'dev') {
// $inputFile = plugin_dir_path(__FILE__) . "source_data/wp_biuro.php.xml";
// $inputFile = "https://base.biuro.lt/_export/wp_biuro.php";
$inputFile = "https://base.biuro.lt/_export/{$script}";
} elseif ($env === 'prod') {
// $inputFile = "http://export.biuro.lt/wp_biuro.php";
$inputFile = "http://export.biuro.lt/{$script}";
}
return $inputFile;
}
function getEnvironment()
{
// Set environment type
$env = 'prod';
//Set dev env. only on dev subdomain
$subDomain = strstr($_SERVER['SERVER_NAME'], '.biuro', true);
if ($subDomain == 'dev') {
$env = 'dev';
}
return $env;
}
function current_location()
{
if (isset($_SERVER['HTTPS']) &&
($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
......@@ -276,7 +276,23 @@ get_header();
echo __('to', 'biuro') . ' ' . $pod->field( 'salary_to' ) . ' ';
endif;
_e('(neto)', 'biuro');
switch ($pod->field('salary_type')):
case 'neto_month':
_e('(neto per month)', 'biuro');
break;
case 'bruto_month':
_e('(bruto per month)', 'biuro');
break;
case 'neto_hour':
_e('(neto per hour)', 'biuro');
break;
case 'bruto_hour':
_e('(bruto per hour)', 'biuro');
break;
default:
_e('(neto)', 'biuro');
break;
endswitch;
endif;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment