Commit 58f89e67 authored by Andrius Mickus's avatar Andrius Mickus

couple properties

parent 14d9b453
......@@ -30,6 +30,8 @@ class JobsImporter
'comment_status' => 'closed'
];
protected $primaryTerms = [];
public function __construct($data)
{
$this->data = $data;
......@@ -74,6 +76,8 @@ class JobsImporter
foreach ($this->data as $ad) {
$this->primaryTerms = [];
// Set language code
$lang = $this->getAdLang($ad);
// Set relations mapping
......@@ -94,12 +98,6 @@ class JobsImporter
$relations
);
// if ($ad['properties'][$this->primaryKey] == 14531) {
//
// var_dump($ad);
// var_dump($adData);
// }
// Unique key of post
$primaryKey = $adData[$this->primaryKey];
......@@ -118,6 +116,7 @@ class JobsImporter
print_r("Error while updating record ($this->idKey: $primaryKey). Error: $e->getMessage()<br>");
}
} else {
try {
......@@ -132,6 +131,11 @@ class JobsImporter
print_r("Error while creating record ($this->primaryKey: $primaryKey). Error: $e->getMessage()<br>");
}
}
foreach ($this->primaryTerms as $term => $termKey) {
update_post_meta($podId, "_yoast_wpseo_primary_{$term}", $termKey);
}
}
}
......@@ -147,9 +151,23 @@ class JobsImporter
{
$result = [];
foreach ($terms as $termKey => $termData) {
$slug = $this->getSlug($termKey, $termData, $lang);
$result[$termKey] = $this->getTerm($termKey, $slug, $termData['name'], $lang);
foreach ($terms as $termKey => $termsData) {
foreach ($termsData as $key => $data) {
$primTerm = false;
if (isset($data['__primary'])) {
$primTerm = true;
unset($data['__primary']);
}
$data['slug'] = $this->getSlug($termKey, $data, $lang);
$term = $this->getTerm($termKey, $data, $lang);
$result[$termKey][] = $term;
if ($primTerm) {
$this->primaryTerms[$termKey] = $term;
}
}
}
return $result;
......@@ -196,41 +214,34 @@ class JobsImporter
* Find and return taxonomy id, if not exist, create it
*
* @param string $key Taxonomy key name
* @param string $slug Taxonomy slug0
* @param string $value Taxonomy name
* @param $data
* @param string $lang Taxonomy language code
*
* @return int Taxonomy id
*/
private function getTerm($key, $slug, $value, $lang)
private function getTerm($key, $data, $lang)
{
$term = pods($key, $slug);
$term = pods($key, $data['slug']);
if (!$term->exists()) {
try {
$termId = pods($key)->add([
'name' => $value,
'slug' => $slug,
]);
$termId = pods($key)->add($data);
} catch (Exception $e) {
print_r("Error while creating taxonomies ($key => $slug). Error: $e->getMessage()<br>");
print_r("Error while creating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
}
} else {
try {
$termId = $term->save([
'name' => $value,
'slug' => $slug,
]);
$termId = $term->save($data);
} catch (Exception $e) {
print_r("Error while updating taxonomies ($key => $slug). Error: $e->getMessage()<br>");
print_r("Error while updating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
}
}
......
......@@ -23,7 +23,12 @@ class JsonDataCollector
protected $terms = [
'city' => [
'slug' => 'miestas_id',
'name' => 'regionas'
'name' => 'regionas',
'__primary' => true,
'additionalCities' => [
'slug' => 'city_id',
'name' => 'city',
]
],
'field' => [
'slug' => 'kategorija',
......@@ -46,7 +51,17 @@ class JsonDataCollector
'title' => 'padalinys_name',
'slug' => 'padalinys'
],
];
protected $collections = [
'city' => [
'name' => 'additionalCities',
'map' => [
'slug' => 'city_id',
'name' => 'city',
],
'append' => 'terms:city'
]
];
// Language map
......@@ -124,6 +139,7 @@ class JsonDataCollector
$item['posts'] = $this->getPosts($ad);
$item['relations'] = $this->relationsMap;
// foreach ($this->dataMap as $mapKey => $dataKey) {
// $dataValue = $ad->{$dataKey};
// $itemValue = $this->getValue($dataValue);
......@@ -167,10 +183,18 @@ class JsonDataCollector
$result = [];
foreach ($this->terms as $termKey => $termProperties) {
foreach ($termProperties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) {
$dataValue = $ad->{$propertyValue};
$result[$termKey][$propertyKey] = $this->getValue($dataValue);
$result[$termKey][] = $this->getEntity($ad, $termProperties);
$appends = array_filter($termProperties, function($item) {
return is_array($item);
});
foreach ($appends as $propertyKey => $propertyValues) {
if (isset($ad->{$propertyKey})) {
foreach ($ad->{$propertyKey} as $data) {
$result[$termKey][] = $this->getEntity($data, $propertyValues);
}
}
}
}
......@@ -178,6 +202,26 @@ class JsonDataCollector
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 = [];
......@@ -194,6 +238,29 @@ class JsonDataCollector
return $result;
}
private function getCollection($ad, $collectionMap)
{
$result = [];
foreach ($this->collections as $collection) {
if (isset($ad->{$collection['name']})) {
$data = $ad->{$collection['name']};
}
$entity = [];
foreach ($collection['map'] as $propertyKey => $propertyValue) {
if (preg_match('/^\_\_/', $propertyKey)) {
$entity[$propertyKey] = $propertyValue;
}
elseif (isset($data[$propertyValue])) {
$dataValue = $data[$propertyValue];
$entity[$propertyKey] = $this->getValue($dataValue);
}
}
}
}
private function getValue($value)
{
return (is_array($value)) ? $value : html_entity_decode($value);
......
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