diff --git a/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/README.md b/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/README.md index 1910965..ce20118 100644 --- a/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/README.md +++ b/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/README.md @@ -15,7 +15,9 @@ class Chapter << (F,LightGreen) >> #Green { # Verses $verses + __construct(Load $load, Insert $insert, ...) + sync(string $translation, int $book, ...) : bool + + names(array $books) : bool - chapter(string $translation, int $book, ...) : bool + - chapters(string $translation, int $book, ...) : bool - verses(string $translation, int $book, ...) : bool - update(string $translation, int $book, ...) : bool - hash(string $hash) : bool @@ -34,7 +36,7 @@ note right of Chapter::__construct Verses $verses end note -note right of Chapter::sync +note left of Chapter::sync Sync the target being watched since: 2.0.1 @@ -46,7 +48,14 @@ note right of Chapter::sync int $chapter end note -note right of Chapter::chapter +note right of Chapter::names + Update translation book chapter names + + since: 2.0.1 + return: bool +end note + +note left of Chapter::chapter Load the chapter numbers since: 2.0.1 @@ -58,7 +67,19 @@ note right of Chapter::chapter int $chapter end note -note right of Chapter::verses +note right of Chapter::chapters + Trigger the update of the chapters of a translation-book + + since: 2.0.1 + return: bool + + arguments: + string $translation + int $book + bool $updateHash = false +end note + +note left of Chapter::verses Load verses since: 2.0.1 @@ -82,7 +103,7 @@ note right of Chapter::update int $chapter end note -note right of Chapter::hash +note left of Chapter::hash Trigger the update of a chapter hash value since: 2.0.1 diff --git a/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.php b/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.php index da1d03c..7e8e3e2 100644 --- a/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.php +++ b/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.php @@ -111,6 +111,32 @@ final class Chapter extends Watcher return false; } + /** + * Update translation book chapter names + * + * @param array $books The books ids. + * + * @return bool True on success + * @since 2.0.1 + */ + public function names(array $books): bool + { + foreach ($books as $book) + { + if (($_book = $this->load->item(['id' => $book], 'book')) === null) + { + return false; + } + + if (!$this->chapters($_book->abbreviation, $_book->nr)) + { + return false; + } + } + + return true; + } + /** * Load the chapter numbers * @@ -156,6 +182,80 @@ final class Chapter extends Watcher return false; } + /** + * Trigger the update of the chapters of a translation-book + * + * @param string $translation The translation. + * @param int $book The book number. + * @param bool $updateHash The switch to update the hash. + * + * @return bool True if update was a success + * @since 2.0.1 + */ + private function chapters(string $translation, int $book, bool $updateHash = false): bool + { + // load all the verses from the local database + $inserted = false; + + // get verses from the API + if (($api = $this->chapters->list($translation, $book)) === null) + { + return false; + } + + if (($chapters = $this->load->items( + ['abbreviation' => $translation, 'book_nr' => $book], + 'chapter' + )) !== null) + { + $update = []; + $insert = []; + $match = ['key' => 'book_nr', 'value' => '']; + + // dynamic update all verse objects + foreach ($api as $chapter) + { + // hash must only update/set if + // verses are also updated/set + if (!$updateHash) + { + unset($chapter->sha); + } + + // check if the verse exist + $match['value'] = (string) $chapter->book_nr; + if (($object = $this->getTarget($match, $chapters)) !== null) + { + $chapter->id = $object->id; + $chapter->modified = $this->today; + $update[] = $chapter; + } + else + { + $insert[] = $chapter; + } + } + + // check if we have values to insert + if ($insert !== []) + { + $inserted = $this->insert->items($insert, 'chapter'); + } + + // update the local chapters + if ($update !== [] && $this->update->items($update, 'id', 'chapter')) + { + return true; + } + } + else + { + $inserted = $this->insert->items((array) $api, 'chapter'); + } + + return $inserted; + } + /** * Load verses * @@ -208,16 +308,19 @@ final class Chapter extends Watcher private function update(string $translation, int $book, int $chapter): bool { // load all the verses from the local database + $inserted = false; + + // get verses from the API + if (($api = $this->verses->get($translation, $book, $chapter)) === null) + { + return false; + } + if (($verses = $this->load->items( ['abbreviation' => $translation, 'book_nr' => $book, 'chapter' => $chapter], 'verse' )) !== null) { - // get verses from the API - if (($api = $this->verses->get($translation, $book, $chapter)) === null) - { - return false; - } $update = []; $insert = []; @@ -228,7 +331,7 @@ final class Chapter extends Watcher { // check if the verse exist $match['value'] = (string) $verse->verse; - if (($object = $this->getTarget($match, $verses)) !== null) + if ($verses !== null && ($object = $this->getTarget($match, $verses)) !== null) { $verse->id = $object->id; $verse->modified = $this->today; @@ -243,7 +346,12 @@ final class Chapter extends Watcher // check if we have values to insert if ($insert !== []) { - $this->insert->items($insert, 'verse'); + $_insert = ['book_nr' => $book, 'abbreviation' => $translation]; + array_walk($insert, function ($item, $key) use ($_insert) { + foreach ($_insert as $k => $v) { $item->$k = $v; } + }); + + $inserted = $this->insert->items($insert, 'verse'); } // update the local verses @@ -252,8 +360,17 @@ final class Chapter extends Watcher return true; } } + else + { + $insert = ['book_nr' => $book, 'abbreviation' => $translation]; + array_walk($api->verses, function ($item, $key) use ($insert) { + foreach ($insert as $k => $v) { $item->$k = $v; } + }); - return false; + $inserted = $this->insert->items((array) $api->verses, 'verse'); + } + + return $inserted; } /** diff --git a/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.power b/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.power index 55a46f4..8ce2c0d 100644 --- a/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.power +++ b/src/07d3888a-5f35-4ba7-977f-fb2f5cf99061/code.power @@ -82,6 +82,32 @@ return false; } + /** + * Update translation book chapter names + * + * @param array $books The books ids. + * + * @return bool True on success + * @since 2.0.1 + */ + public function names(array $books): bool + { + foreach ($books as $book) + { + if (($_book = $this->load->item(['id' => $book], 'book')) === null) + { + return false; + } + + if (!$this->chapters($_book->abbreviation, $_book->nr)) + { + return false; + } + } + + return true; + } + /** * Load the chapter numbers * @@ -127,6 +153,80 @@ return false; } + /** + * Trigger the update of the chapters of a translation-book + * + * @param string $translation The translation. + * @param int $book The book number. + * @param bool $updateHash The switch to update the hash. + * + * @return bool True if update was a success + * @since 2.0.1 + */ + private function chapters(string $translation, int $book, bool $updateHash = false): bool + { + // load all the verses from the local database + $inserted = false; + + // get verses from the API + if (($api = $this->chapters->list($translation, $book)) === null) + { + return false; + } + + if (($chapters = $this->load->items( + ['abbreviation' => $translation, 'book_nr' => $book], + 'chapter' + )) !== null) + { + $update = []; + $insert = []; + $match = ['key' => 'book_nr', 'value' => '']; + + // dynamic update all verse objects + foreach ($api as $chapter) + { + // hash must only update/set if + // verses are also updated/set + if (!$updateHash) + { + unset($chapter->sha); + } + + // check if the verse exist + $match['value'] = (string) $chapter->book_nr; + if (($object = $this->getTarget($match, $chapters)) !== null) + { + $chapter->id = $object->id; + $chapter->modified = $this->today; + $update[] = $chapter; + } + else + { + $insert[] = $chapter; + } + } + + // check if we have values to insert + if ($insert !== []) + { + $inserted = $this->insert->items($insert, 'chapter'); + } + + // update the local chapters + if ($update !== [] && $this->update->items($update, 'id', 'chapter')) + { + return true; + } + } + else + { + $inserted = $this->insert->items((array) $api, 'chapter'); + } + + return $inserted; + } + /** * Load verses * @@ -179,16 +279,19 @@ private function update(string $translation, int $book, int $chapter): bool { // load all the verses from the local database + $inserted = false; + + // get verses from the API + if (($api = $this->verses->get($translation, $book, $chapter)) === null) + { + return false; + } + if (($verses = $this->load->items( ['abbreviation' => $translation, 'book_nr' => $book, 'chapter' => $chapter], 'verse' )) !== null) { - // get verses from the API - if (($api = $this->verses->get($translation, $book, $chapter)) === null) - { - return false; - } $update = []; $insert = []; @@ -199,7 +302,7 @@ { // check if the verse exist $match['value'] = (string) $verse->verse; - if (($object = $this->getTarget($match, $verses)) !== null) + if ($verses !== null && ($object = $this->getTarget($match, $verses)) !== null) { $verse->id = $object->id; $verse->modified = $this->today; @@ -214,7 +317,12 @@ // check if we have values to insert if ($insert !== []) { - $this->insert->items($insert, 'verse'); + $_insert = ['book_nr' => $book, 'abbreviation' => $translation]; + array_walk($insert, function ($item, $key) use ($_insert) { + foreach ($_insert as $k => $v) { $item->$k = $v; } + }); + + $inserted = $this->insert->items($insert, 'verse'); } // update the local verses @@ -223,8 +331,17 @@ return true; } } + else + { + $insert = ['book_nr' => $book, 'abbreviation' => $translation]; + array_walk($api->verses, function ($item, $key) use ($insert) { + foreach ($insert as $k => $v) { $item->$k = $v; } + }); - return false; + $inserted = $this->insert->items((array) $api->verses, 'verse'); + } + + return $inserted; } /** diff --git a/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.php b/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.php index e3c9ab8..1f81965 100644 --- a/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.php +++ b/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.php @@ -168,7 +168,7 @@ final class Translation extends Watcher { // check if the verse exist $match['value'] = $translation->abbreviation ?? null; - if (($object = $this->getTarget($match, $local_translations)) !== null) + if ($local_translations !== null && ($object = $this->getTarget($match, $local_translations)) !== null) { $translation->id = $object->id; $translation->created = $this->today; diff --git a/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.power b/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.power index 550ae0b..4113175 100644 --- a/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.power +++ b/src/7d592acd-f031-4d0f-b667-584c88ae0495/code.power @@ -140,7 +140,7 @@ { // check if the verse exist $match['value'] = $translation->abbreviation ?? null; - if (($object = $this->getTarget($match, $local_translations)) !== null) + if ($local_translations !== null && ($object = $this->getTarget($match, $local_translations)) !== null) { $translation->id = $object->id; $translation->created = $this->today;