Commit 0b57f9b0 authored by Jack Stupple's avatar Jack Stupple

import resource categories and resources

parent daee56fd
<?php
namespace App\Console\Commands;
use App\Language;
use App\Resource;
use App\ResourceCategory;
use Illuminate\Console\Command;
/**
* Class ImportResources
* @package App\Console\Commands
* @see https://laravel.com/docs/5.6/artisan
*/
class ImportResourceCategories extends Command
{
protected $signature = 'import:resource-categories {csv_file_name}';
protected $description = 'Import resource categories into the API via a CSV file.';
protected $csv_file_name = null;
protected $csv_file_pathinfo = [];
protected $csv_headers = null;
protected $csv_data = [];
protected static $required_headers = [
'resource category',
'resource category parent'
];
protected static $cached_categories = [];
public function handle()
{
ini_set('auto_detect_line_endings', true);
$this->csv_file_name = $this->argument('csv_file_name');
try {
// exception will be thrown if file is invalid
$this->csv_file_pathinfo = $this->checkFileProperties($this->csv_file_name);
} catch (\Exception $exception) {
$this->output->writeln("<error>Error encountered:</error> {$exception->getMessage()}");
return false;
}
list($this->csv_headers, $this->csv_data) = $this->getFileData($this->csv_file_name);
$this->checkHeaders($this->csv_headers); // throws exception if fails
// we now have the resources list
foreach ($this->csv_data as $csv_row_number => $_category) {
$resource_category = ResourceCategory::firstOrCreate([
'name' => $_category['resource category'],
'parent_category' => $_category['resource category parent']
]);
if (!$resource_category->exists()) {
$resource_category->save();
}
}
}
protected function checkHeaders($csv_headers)
{
foreach (static::$required_headers as $required_header) {
if (in_array($required_header, $csv_headers) === false) {
throw new \Exception ("Missing required header in CSV: {$required_header}");
}
}
}
protected function getFileData($csv_file_name)
{
$file_handle = fopen($csv_file_name, 'r');
$csv_headers = null;
$csv_data = [];
$row_number = 0;
$empty_rows = 0; // if 3 empty rows are encountered we just break the loop- assumed end of data
while (($csv_row = fgetcsv($file_handle)) !== false) {
$row_number++; // the header is also a row in the CSV
if ($csv_headers === null) {
$_csv_headers = [];
foreach ($csv_row as $header) {
$_csv_headers[] = trim(strtolower($header));
}
$csv_headers = $_csv_headers;
unset($_csv_headers);
continue;
}
// skip empty rows
if (array_filter(array_unique($csv_row)) === []) {
$empty_rows++;
if ($empty_rows >= 3) {
// assume we have reached the end of the data
$this->output->writeln("<comment>Assumed end of data:</comment> {$row_number}");
break;
} else {
$this->output->writeln("<comment>Skip empty row:</comment> {$row_number}");
continue;
}
}
$_csv_data = [];
foreach ($csv_headers as $header_index => $header) {
$_csv_data[$header] = trim($csv_row[$header_index]); // we trim to ensure no additional spaces
// ensure the data is expected - primarily for the URL's
$validation_method = camel_case('validate ' . $header);
if (method_exists($this, $validation_method) && !empty($_csv_data[$header])) {
if (!$this->$validation_method($_csv_data[$header])) {
throw new \Exception("Issue with {$header} on row: {$row_number}");
}
}
}
$csv_data[$row_number] = $_csv_data; // add the row to the data with same row number as that in the CSV
unset($_csv_data);
}
return [$csv_headers, $csv_data];
}
protected function checkFileProperties($csv_file_name)
{
if (file_exists($csv_file_name) === false) {
throw new \Exception('File does not exist');
}
$csv_file_pathinfo = pathinfo($csv_file_name);
// extension is only defined if one is provided, eg /etc/hosts will have no extension
if (!isset($csv_file_pathinfo['extension']) || $csv_file_pathinfo['extension'] !== 'csv') {
throw new \Exception('File is not CSV');
}
return $csv_file_pathinfo;
}
protected function validateUkAsset($uk_asset)
{
return $this->validateUrl($uk_asset);
}
protected function validateFrAsset($asset)
{
return $this->validateUrl($asset);
}
protected function validateDeAsset($asset)
{
return $this->validateUrl($asset);
}
protected function validateNoAsset($asset)
{
return $this->validateUrl($asset);
}
protected function validateUrl($asset)
{
return filter_var($asset, FILTER_VALIDATE_URL);
}
}
\ No newline at end of file
......@@ -56,29 +56,29 @@ class ImportResources extends Command
// we now have the resources list
foreach ($this->csv_data as $csv_row_number => $_resource) {
$language = null;
$resource_link = null;
// add here cases for the different kinds of resources we are importing
if (!empty($_resource['uk asset'])) {
$language = $this->getLanguageFor('uk');
$resource_link = $_resource['uk asset'];
} else if (!empty($_resource['nl asset'])) {
$language = $this->getLanguageFor('nl');
$resource_link = $_resource['nl asset'];
} else if (!empty($_resource['de asset'])) {
$language = $this->getLanguageFor('de');
$resource_link = $_resource['de asset'];
} else if (!empty($_resource['no asset'])) {
$language = $this->getLanguageFor('no');
$resource_link = $_resource['no asset'];
} else if (!empty($_resource['sl asset'])) {
$language = $this->getLanguageFor('sl');
$resource_link = $_resource['sl asset'];
} else {
$this->output->writeln("<comment>Could not determine language</comment> {$csv_row_number}");
continue;
}
$language = $this->getLanguageFor($_resource['language']);
$resource_link = $_resource['url'];
// // add here cases for the different kinds of resources we are importing
// if (!empty($_resource['uk asset'])) {
// $language = $this->getLanguageFor('uk');
// $resource_link = $_resource['uk asset'];
// } else if (!empty($_resource['nl asset'])) {
// $language = $this->getLanguageFor('nl');
// $resource_link = $_resource['nl asset'];
// } else if (!empty($_resource['de asset'])) {
// $language = $this->getLanguageFor('de');
// $resource_link = $_resource['de asset'];
// } else if (!empty($_resource['no asset'])) {
// $language = $this->getLanguageFor('no');
// $resource_link = $_resource['no asset'];
// } else if (!empty($_resource['sl asset'])) {
// $language = $this->getLanguageFor('sl');
// $resource_link = $_resource['sl asset'];
// } else {
// $this->output->writeln("<comment>Could not determine language</comment> {$csv_row_number}");
// continue;
// }
// check for duplicates
if ($this->isDuplicateResource($language->id, $resource_link)) {
......@@ -100,15 +100,14 @@ class ImportResources extends Command
}
}
protected function getResourceCategory($category, $parent_category)
protected function getResourceCategory($category)
{
if (isset(static::$cached_categories[$category])) {
return static::$cached_categories[$category];
}
$resource_category = ResourceCategory::firstOrCreate([
'name' => $category,
'parent_category' => $parent_category
'name' => $category
]);
if (!$resource_category->exists()) {
......
......@@ -3,6 +3,7 @@
namespace App\Console;
use App\Console\Commands\AddCurrency;
use App\Console\Commands\ImportResourceCategories;
use App\Console\Commands\ImportResources;
use App\Console\Commands\Make\MakeAdminUser;
use Illuminate\Console\Scheduling\Schedule;
......@@ -17,6 +18,7 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
ImportResources::class,
ImportResourceCategories::class,
AddCurrency::class,
MakeAdminUser::class
];
......
......@@ -22,6 +22,10 @@ class ImportEmilyQuestions extends CharacterMigration
*/
public function up()
{
if (!\App\ResourceCategory::count()) {
throw new \Exception('Import resources first.');
}
$this->created_at = date('Y-m-d H:i:s');
$this->character = new \App\Character();
......
......@@ -187,9 +187,11 @@ class CharacterMigration extends Migration
foreach ($_answer['resource_categories'] as $_resource_category) {
$category = \App\ResourceCategory::where('name', 'LIKE', $_resource_category)->first();
if ($category) {
$answer->resourceCategories()->syncWithoutDetaching($category);
if (!$category) {
throw new \Exception('Resource category not found: ' . $_resource_category);
}
$answer->resourceCategories()->syncWithoutDetaching($category);
}
}
}
......
RESOURCE CATEGORY, Resource category parent
Online Banking, banking
Overdraft, loans
Direct Debits, banking
Car Depreciation, investments
Pensions, banking
Overdrafts, loans
Accounts, banking
Bonds, investments
Borrowing Money, loans
Budgeting, budgeting
Car Repayments, loans
Cash, banking
Credit Cards, loans
Credit Options, loans
Debit Cards, banking
Interest, investments
Interest Rates, investments
Loans, loans
Payday Loans, loans
Phone Deals, budgeting
Phone Depreciation, investments
Saving For A Car, budgeting
Savings, banking
Startups, investments
Store Credit, budgeting
Prepaid Cards, banking
Packaged Accounts, banking
Retirement, investments
Mortgages, loans
Comparison Websites, budgeting
Investments, investments
\ No newline at end of file
RESOURCE GROUPS,CATEGORY,TITLE,UK ASSET,NO ASSET,NL ASSET,DE ASSET,SL ASSET
banking,Accounts,Types of bank accounts,http://readanddigest.com/different-types-of-bank-accounts/,https://www.wikifin.be/nl/themas/uw-dagelijks-geld/bankrekeningen/bankrekening,,,
budgeting,Savings,Learn about saving,https://en.wikipedia.org/wiki/Saving,https://no.wikipedia.org/wiki/Sparing,,,
budgeting,Savings,Learn about saving accounts,https://www.investopedia.com/terms/s/savingsaccount.asp?ad=dirN&qo=investopediaSiteSearch&qsrc=0&o=40186,,,,
loans,Interest rates,Learning about interest rates,https://www.investopedia.com/terms/i/interestrate.asp,,,,
banking,Debit cards,Store credit vs credit cards,https://www.investopedia.com/articles/pf/10/credit-card-debit-card.asp,,,,
loans,Interest,What is interest?,https://www.thebalance.com/what-is-interest-315436,,,,
budgeting,Phone deals,Comparing phone contracts,https://mobile-phones.confused.com,,,,
budgeting,Saving for a car,How to save for a car,https://www.nerdwallet.com/blog/finance/how-to-save-for-a-car/,,,,
budgeting,Saving for a car,Finance advice on buying a car,https://personal.natwest.com/personal/life-moments/buying-a-car.html,,,,
loans,Car repayments,Car repayment tips,https://www.thebalance.com/just-made-your-last-car-payment-start-paying-yourself-453779,,,,
budgeting,Budgeting,Learn about budgeting,https://www.practicalmoneyskills.com/learn/budgeting,,,,
budgeting,Phone deals,View mobile phone deals,https://www.mobilephonechecker.co.uk,https://www.phonearena.com/phones#/phones/sort/popular,,,
loans,Borrowing money,Borrowing and credit basics,https://www.moneyadviceservice.org.uk/en/articles/borrowing-and-credit-basics,,,,
loans,Borrowing money,Borrowing money and paying it back,https://www.youtube.com/watch?time_continue=1&v=IsZu0fjxR2U ,,,,
loans,Borrowing money,Calculating the cost of borrowing,https://www.moneyadviceservice.org.uk/en/articles/how-to-work-out-the-true-cost-of-borrowing,,,,
investments,Phone Depreciation,Learn about phone depreciation,https://atotaxrates.info/tax-deductions/ato-depreciation/depreciation-mobile-phone/,,,,
loans,Borrowing money,Personal loans and overdraft,https://www.moneyadviceservice.org.uk/en/articles/borrowing-and-credit-basics,,,,
loans,Borrowing money,The pros and cons of borrowing from friends / family ,https://www.moneyadviceservice.org.uk/en/articles/should-you-borrow-from-family-or-friends#pros-and-cons-of-borrowing-from-family-and-friends,,,,
banking,Cash,What is 'Cash'?,https://www.investopedia.com/terms/c/cash.asp?ad=dirN&qo=serpSearchTopBox&qsrc=1&o=40186,https://www.investopedia.com/articles/pf/08/pay-in-cash.asp?ad=dirN&qo=serpSearchTopBox&qsrc=1&o=40186,,,
investments,Phone Depreciation,Phone depreciation,https://priceonomics.com/phones/,,,,
investments,Startups,The pros and cons of crowdfunding,https://www.cnbc.com/2017/07/05/can-you-get-rich-by-investing-in-a-startup-how-to-do-it--and-the-pros-and-cons-of-crowdfunding.html,https://www.wikifin.be/nl/themas/sparen-en-beleggen/aandelen/wat-een-aandeel,,,
investments,Bonds,What is a Bond?,https://en.wikipedia.org/wiki/Bond_(finance),https://www.wikifin.be/nl/themas/sparen-en-beleggen/obligaties/wat-een-obligatie,,,
loans,Credit options,Credit options ,http://www.experian.co.uk/downloads/consumer/gettingCredit/gettingCreditDownload.pdf,,,,
loans,Borrowing money,Do you need to borrow money?,https://www.moneyadviceservice.org.uk/en/articles/do-you-need-to-borrow-money,,,,
loans,Credit cards,Learn about credit cards,https://en.wikipedia.org/wiki/Credit_card,,,,
loans,Loans,Learn about loans,https://en.wikipedia.org/wiki/Loan,https://www.laanekalkulator.no/,,,
loans,Payday loans,Learn about payday loans,https://www.moneyadviceservice.org.uk/en/payday-loans,,,,
banking,Store credit,Store credit vs credit cards,,,,,
loans,Loans,The cost of your loan,https://www.moneyadviceservice.org.uk/en/tools/loan-calculator,,,,
loans,Mortgages,Mortgages - beginners guide,https://www.moneyadviceservice.org.uk/en/articles/mortgages-a-beginners-guide,,,,
investments,Retirement,Retirement advice,https://www.saga.co.uk/magazine/money/retirement,,,,
loans,loans,Debt consolidation,https://www.stepchange.org/debt-info/free-debt-consolidation.aspx,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,https://www.practicalmoneyskills.com/learn/budgeting,,,,
,,,,,,,
,,,,,,,
,,,https://www.investopedia.com/terms/l/loan.asp,,,,
\ No newline at end of file
RESOURCE GROUPS,Language,CATEGORY,TITLE,URL,NO ASSET,NL ASSET,DE ASSET,SL ASSET
#N/A,en,Accounts,Types of bank accounts,http://readanddigest.com/different-types-of-bank-accounts/,,,,
investments,en,Savings,Learn about saving,https://en.wikipedia.org/wiki/Saving,,,,
investments,en,Savings,Learn about saving accounts,https://www.investopedia.com/terms/s/savingsaccount.asp?ad=dirN&qo=investopediaSiteSearch&qsrc=0&o=40186,,,,
investments,en,Interest rates,Learning about interest rates,https://www.investopedia.com/terms/i/interestrate.asp,,,,
banking,en,Debit cards,Store credit vs credit cards,https://www.investopedia.com/articles/pf/10/credit-card-debit-card.asp,,,,
investments,en,Interest,What is interest?,https://www.thebalance.com/what-is-interest-315436,,,,
budgeting,en,Phone deals,Comparing phone contracts,https://mobile-phones.confused.com,,,,
investments,en,Saving for a car,How to save for a car,https://www.nerdwallet.com/blog/finance/how-to-save-for-a-car/,,,,
investments,en,Saving for a car,Finance advice on buying a car,https://personal.natwest.com/personal/life-moments/buying-a-car.html,,,,
loans,en,Car repayments,Car repayment tips,https://www.thebalance.com/just-made-your-last-car-payment-start-paying-yourself-453779,,,,
#N/A,en,Budgeting,Learn about budgeting,https://www.practicalmoneyskills.com/learn/budgeting,,,,
budgeting,en,Phone deals,View mobile phone deals,https://www.mobilephonechecker.co.uk,,,,
#N/A,en,Borrowing money,Borrowing and credit basics,https://www.moneyadviceservice.org.uk/en/articles/borrowing-and-credit-basics,,,,
#N/A,en,Borrowing money,Borrowing money and paying it back,https://www.youtube.com/watch?time_continue=1&v=IsZu0fjxR2U ,,,,
#N/A,en,Borrowing money,Calculating the cost of borrowing,https://www.moneyadviceservice.org.uk/en/articles/how-to-work-out-the-true-cost-of-borrowing,,,,
investments,en,Phone Depreciation,Learn about phone depreciation,https://atotaxrates.info/tax-deductions/ato-depreciation/depreciation-mobile-phone/,,,,
#N/A,en,Borrowing money,Personal loans and overdraft,https://www.moneyadviceservice.org.uk/en/articles/borrowing-and-credit-basics,,,,
#N/A,en,Borrowing money,The pros and cons of borrowing from friends / family ,https://www.moneyadviceservice.org.uk/en/articles/should-you-borrow-from-family-or-friends#pros-and-cons-of-borrowing-from-family-and-friends,,,,
banking,en,Cash,What is 'Cash'?,https://www.investopedia.com/terms/c/cash.asp?ad=dirN&qo=serpSearchTopBox&qsrc=1&o=40186,,,,
investments,en,Phone Depreciation,Phone depreciation,https://priceonomics.com/phones/,,,,
investments,en,Startups,The pros and cons of crowdfunding,https://www.cnbc.com/2017/07/05/can-you-get-rich-by-investing-in-a-startup-how-to-do-it--and-the-pros-and-cons-of-crowdfunding.html,,,,
#N/A,en,Bonds,What is a Bond?,https://en.wikipedia.org/wiki/Bond_(finance),,,,
loans,en,Credit options,Credit options ,http://www.experian.co.uk/downloads/consumer/gettingCredit/gettingCreditDownload.pdf,,,,
#N/A,en,Borrowing money,Do you need to borrow money?,https://www.moneyadviceservice.org.uk/en/articles/do-you-need-to-borrow-money,,,,
loans,en,Credit cards,Learn about credit cards,https://en.wikipedia.org/wiki/Credit_card,,,,
loans,en,Loans,Learn about loans,https://en.wikipedia.org/wiki/Loan,,,,
loans,en,Payday loans,Learn about payday loans,https://www.moneyadviceservice.org.uk/en/payday-loans,,,,
investments,en,Store credit,Store credit vs credit cards,,,,,
loans,en,Loans,The cost of your loan,https://www.moneyadviceservice.org.uk/en/tools/loan-calculator,,,,
loans,en,Mortgages,Mortgages - beginners guide,https://www.moneyadviceservice.org.uk/en/articles/mortgages-a-beginners-guide,,,,
investments,en,Retirement,Retirement advice,https://www.saga.co.uk/magazine/money/retirement,,,,
loans,en,loans,Debt consolidation,https://www.stepchange.org/debt-info/free-debt-consolidation.aspx,,,,
,en,Online Banking,Online Banking,https://en.wikipedia.org/wiki/Online_banking,,,,
,no,Online Banking,Nettbank,https://no.wikipedia.org/wiki/Nettbank,,,,
,en,Overdraft,Overdraft,https://en.wikipedia.org/wiki/Overdraft,,,,
,en,Direct Debits,Direct Debits,https://en.wikipedia.org/wiki/Direct_debit,,,,
,en,Car Depreciation,Car depreciation,https://en.wikipedia.org/wiki/Car_costs,,,,
,en,Pensions,Pensions,https://en.wikipedia.org/wiki/Pension,,,,
,en,Accounts,Accounts,https://en.wikipedia.org/wiki/Bank_account,,,,
,en,Bonds,Bonds,https://en.wikipedia.org/wiki/Bond_(finance),,,,
,en,Borrowing Money,Borrowing money,https://en.wikipedia.org/wiki/Loan,,,,
,en,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,no,Accounts,Types of bank accounts,https://www.wikifin.be/nl/themas/uw-dagelijks-geld/bankrekeningen/bankrekening,,,,
,no,Savings,Learn about saving,https://no.wikipedia.org/wiki/Sparing,,,,
,no,Phone deals,View mobile phone deals,https://www.phonearena.com/phones#/phones/sort/popular,,,,
,no,Cash,What is 'Cash'?,https://www.investopedia.com/articles/pf/08/pay-in-cash.asp?ad=dirN&qo=serpSearchTopBox&qsrc=1&o=40186,,,,
,no,Cash,What is 'Cash'?,https://www.investopedia.com/articles/pf/08/pay-in-cash.asp?ad=dirN&qo=serpSearchTopBox&qsrc=1&o=40186,,,,
,no,Startups,The pros and cons of crowdfunding,https://www.wikifin.be/nl/themas/sparen-en-beleggen/aandelen/wat-een-aandeel,,,,
,no,Bonds,What is a Bond?,https://www.wikifin.be/nl/themas/sparen-en-beleggen/obligaties/wat-een-obligatie,,,,
,no,Loans,Learn about loans,https://www.laanekalkulator.no/,,,,
\ No newline at end of file
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