Permalink
Browse files

init

  • Loading branch information...
0 parents commit 032f243b31eab6c81c44791531bc6df96c3cc6c2 @Rhincodon committed Nov 20, 2016
@@ -0,0 +1,15 @@
+; This file is for unifying the coding style for different editors and IDEs.
+; More information at http://editorconfig.org
+
+root = true
+
+[*]
+charset = utf-8
+indent_size = 4
+indent_style = space
+end_of_line = lf
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+[*.md]
+trim_trailing_whitespace = false
@@ -0,0 +1,10 @@
+# Path-based git attributes
+# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
+
+# Ignore all test and documentation with "export-ignore".
+/.gitattributes export-ignore
+/.gitignore export-ignore
+/.travis.yml export-ignore
+/phpunit.xml.dist export-ignore
+/.scrutinizer.yml export-ignore
+/tests export-ignore
@@ -0,0 +1,4 @@
+build
+composer.lock
+docs
+vendor
@@ -0,0 +1,21 @@
+# The MIT License (MIT)
+
+Copyright (c) 2016 Rhinodontypicus <rhinodontypicus@gmail.com>
+
+> Permission is hereby granted, free of charge, to any person obtaining a copy
+> of this software and associated documentation files (the "Software"), to deal
+> in the Software without restriction, including without limitation the rights
+> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+> copies of the Software, and to permit persons to whom the Software is
+> furnished to do so, subject to the following conditions:
+>
+> The above copyright notice and this permission notice shall be included in
+> all copies or substantial portions of the Software.
+>
+> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+> THE SOFTWARE.
@@ -0,0 +1,22 @@
+# Laravel Eloquent sync with Google Spreadsheets
+
+[![Latest Version on Packagist](https://img.shields.io/packagist/v/rhincodon/laravel-eloquent-spreadsheets.svg?style=flat-square)](https://packagist.org/packages/rhincodon/laravel-eloquent-spreadsheets)
+[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
+
+Under development
+
+## Installation
+
+Coming Soon
+
+## Usage
+
+Coming Soon
+
+## Credits
+
+- [rhinodontypicus](https://github.com/Rhincodon)
+
+## License
+
+The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
@@ -0,0 +1,40 @@
+{
+ "name": "rhincodon/laravel-eloquent-spreadsheets",
+ "description": "Sync Eloquent models with google spreadsheets",
+ "keywords": [
+ "laravel",
+ "eloquent",
+ "spreadsheets"
+ ],
+ "homepage": "https://github.com/Rhincodon/laravel-eloquent-spreadsheets",
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "rhinodontypicus",
+ "email": "rhinodontypicus@gmail.com",
+ "homepage": "https://laravelinfo.com",
+ "role": "Developer"
+ }
+ ],
+ "require": {
+ "php" : "^7.0",
+ "illuminate/support": "~5.3.0",
+ "illuminate/bus": "~5.3.0",
+ "illuminate/console": "~5.3.0",
+ "illuminate/contracts": "~5.3.0",
+ "illuminate/queue": "~5.3.0",
+ "google/apiclient": "^2.0",
+ "google/apiclient-services": "^0.8.0"
+ },
+ "autoload": {
+ "psr-4": {
+ "Rhinodontypicus\\EloquentSpreadsheets\\": "src"
+ },
+ "files": [
+ "src/helpers.php"
+ ]
+ },
+ "config": {
+ "sort-packages": true
+ }
+}
No changes.
@@ -0,0 +1,17 @@
+<?php
+
+return [
+ 'credentials_path' => storage_path('credentials.json'),
+ 'sync_models' => [
+ \App\User::class => [
+ 'spreadsheet_id' => '17GOsXgEe6WdPJ-aCO46bNb9T6me96LTEasv-E61-AxI',
+ 'list_name' => 'Main',
+ 'sync_attributes' => [
+ 'id' => 'A',
+ 'name' => 'B',
+ 'email' => 'C',
+ ],
+ 'queue_name' => 'default',
+ ],
+ ],
+];
@@ -0,0 +1,55 @@
+<?php
+
+namespace Rhinodontypicus\EloquentSpreadsheets\Commands;
+
+use Illuminate\Console\Command;
+use Rhinodontypicus\EloquentSpreadsheets\ModelObserver;
+
+class FillSheet extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'eloquent-spreadsheets:fill {modelType}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Fill sheet with models';
+
+ /**
+ * Create a new command instance.
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $modelType = $this->argument('modelType');
+
+ if (empty(config('laravel-eloquent-spreadsheets')["sync_models"][$modelType])) {
+ $this->warn('Model not registered in config');
+ return;
+ }
+
+ $model = new $modelType;
+
+ foreach ($model->all() as $modelItem) {
+ (new ModelObserver())->updated($modelItem);
+ $this->info("Dispatched model with ID: {$modelItem->id}");
+ }
+
+ $this->info("Dispatching completed");
+ }
+}
@@ -0,0 +1,117 @@
+<?php
+
+namespace Rhinodontypicus\EloquentSpreadsheets\Commands;
+
+use Illuminate\Console\Command;
+use Rhinodontypicus\EloquentSpreadsheets\SpreadsheetService;
+
+class Sync extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'eloquent-spreadsheets:sync {modelType}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Sync sheet with site';
+
+ private $config;
+ private $startColumn;
+ private $endColumn;
+ private $idColumn;
+
+ /**
+ * Create a new command instance.
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $modelType = $this->argument('modelType');
+
+ if (empty(config('laravel-eloquent-spreadsheets')["sync_models"][$modelType])) {
+ $this->warn('Model not registered in config');
+ return;
+ }
+
+ $this->prepareConfig($modelType);
+ $model = new $modelType;
+
+ $range = "{$this->config['list_name']}!{$this->startColumn}2:{$this->endColumn}";
+ $response = app(SpreadsheetService::class)->service()->spreadsheets_values->get(
+ $this->config['spreadsheet_id'],
+ $range
+ );
+ $values = $response->getValues();
+
+ if (empty($values)) {
+ $this->warn('Sheet is empty');
+ return;
+ }
+
+ foreach ($values as $value) {
+ if (
+ !is_array($value) ||
+ empty($value[getLettersToIdsTable($this->startColumn, $this->endColumn)[$this->idColumn]])
+ ) {
+ continue;
+ }
+
+ $id = $value[getLettersToIdsTable($this->startColumn, $this->endColumn)[$this->idColumn]];
+ $modelItem = $model->whereId($id)->first();
+
+ if (!$modelItem) {
+ continue;
+ }
+
+ $this->updateModel($modelItem, $value);
+ }
+ }
+
+ private function updateModel($modelItem, $value)
+ {
+ foreach ($this->config['sync_attributes'] as $attributeKey => $attributeColumn) {
+ $id = getLettersToIdsTable($this->startColumn, $this->endColumn)[$attributeColumn];
+
+ if (empty($value[$id])) {
+ continue;
+ }
+
+ $modelItem->{$attributeKey} = $value[$id];
+ }
+
+ if (empty($modelItem->getDirty())) {
+ return;
+ }
+
+ $modelItem->removeObservableEvents(['updated']);
+ $modelItem->save();
+ $modelItem->addObservableEvents(['updated']);
+
+ $this->info("Model updated: {$modelItem->id}");
+ }
+
+ private function prepareConfig($modelType)
+ {
+ $this->config = config('laravel-eloquent-spreadsheets')["sync_models"][$modelType];
+
+ asort($this->config['sync_attributes']);
+ $this->startColumn = array_first($this->config['sync_attributes']);
+ $this->endColumn = array_last($this->config['sync_attributes']);
+ $this->idColumn = $this->config['sync_attributes']['id'];
+ }
+}
@@ -0,0 +1,63 @@
+<?php
+
+namespace Rhinodontypicus\EloquentSpreadsheets;
+
+use Illuminate\Support\ServiceProvider;
+use Rhinodontypicus\EloquentSpreadsheets\Commands\FillSheet;
+use Rhinodontypicus\EloquentSpreadsheets\Commands\Sync;
+
+class EloquentSpreadsheetsServiceProvider extends ServiceProvider
+{
+ /**
+ * Bootstrap the application services.
+ */
+ public function boot()
+ {
+ $this->publishes([
+ __DIR__ . '/../config/laravel-eloquent-spreadsheets.php' => config_path('laravel-eloquent-spreadsheets.php'),
+ ], 'config');
+
+ $this->bootObservers();
+ }
+
+ /**
+ * Register the application services.
+ */
+ public function register()
+ {
+ $this->mergeConfigFrom(
+ __DIR__ . '/../config/laravel-eloquent-spreadsheets.php', 'laravel-eloquent-spreadsheets'
+ );
+
+ $this->app->singleton(SpreadsheetService::class, function() {
+ $client = (new SpreadsheetClient(config('laravel-eloquent-spreadsheets.credentials_path')))->client();
+
+ return new SpreadsheetService($client);
+ });
+
+ $this->app->bind('command.eloquent-spreadsheets:fill', FillSheet::class);
+ $this->app->bind('command.eloquent-spreadsheets:sync', Sync::class);
+ $this->commands([
+ 'command.eloquent-spreadsheets:fill',
+ 'command.eloquent-spreadsheets:sync',
+ ]);
+ }
+
+ /**
+ * @return bool
+ */
+ private function bootObservers()
+ {
+ $models = config('laravel-eloquent-spreadsheets.sync_models');
+
+ if (empty($models)) {
+ return false;
+ }
+
+ foreach ($models as $modelClass => $modelConfig) {
+ $modelClass::observe(new ModelObserver());
+ }
+
+ return true;
+ }
+}
Oops, something went wrong.

0 comments on commit 032f243

Please sign in to comment.