Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #278 #386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions app/controllers/ModController.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

use Illuminate\Support\MessageBag;
use League\Csv\Reader;

class ModController extends BaseController {

public function __construct()
{
parent::__construct();
$this->beforeFilter('perm', array('solder_mods'));
$this->beforeFilter('perm', array('mods_manage'), array('only' => array('view','versions')));
$this->beforeFilter('perm', array('mods_create'), array('only' => array('create')));
$this->beforeFilter('perm', array('mods_create'), array('only' => array('create', 'bulk')));
$this->beforeFilter('perm', array('mods_delete'), array('only' => array('delete')));
}

Expand Down Expand Up @@ -68,6 +70,74 @@ public function postCreate()
return Redirect::to('mod/view/'.$mod->id);
}

public function getBulk()
{
return View::make('mod.bulk');
}

public function postBulk()
{
$rules = array(
'csv' => 'required|csv',
);
$messages = array(
'csv.required' => 'You must select a file to upload.',
'csv.csv' => 'The file either did not have the csv extension or did not read like a csv.'
);

$file_validation = Validator::make(Input::all(), $rules, $messages);
if ($file_validation->fails())
return Redirect::to('mod/bulk')->withErrors($file_validation->messages());

$file = Input::file('csv');
$csv = Reader::createFromPath($file->getRealPath());
$data = $csv->fetchAssoc(0, function ($row){
return array_map('trim', $row);
});
$errors = array();
$notices = array();
foreach ($data as $line => $mod_data)
{
$rules = array(
'name' => 'required|unique:mods',
'pretty_name' => 'required',
'link' => 'url',
'donatelink' => 'url',
);
$messages = array(
'name.required' => 'On line: ' . (intval($line) + 1) . ' you are missing a mod slug name.',
'name.unique' => 'The slug on line: ' . (intval($line) + 1) . ' for mod: ' . $mod_data['name'] . ' is already taken',
'pretty_name.required' => 'On line: ' . (intval($line) + 1) . ' for mod: ' . $mod_data['name'] . ' is missing a mod name',
'link.url' => 'On line: ' . (intval($line) + 1) . ' for mod: ' . $mod_data['pretty_name'] . ' you must enter a properly formatted Website',
'donatelink.url' => 'On line: ' . (intval($line) + 1) . ' for mod: ' . $mod_data['pretty_name'] . ' you must enter a proper formatted Donation Link',
);

$validation = Validator::make($mod_data, $rules, $messages);
if ($validation->fails())
{
foreach ($validation->messages()->all() as $error)
{
$errors[] = $error;
}
continue;
}

$mod = new Mod();
$mod->name = Str::slug($mod_data['name']);
$mod->pretty_name = $mod_data['pretty_name'];
$mod->author = $mod_data['author'];
$mod->description = $mod_data['description'];
$mod->link = $mod_data['link'];
$mod->donatelink = $mod_data['donatelink'];
$mod->save();
$notices[] = 'Added ' . $mod_data['pretty_name'] . ' to Mod List.';
}
if (!empty($errors))
return Redirect::to('mod/bulk')->withErrors($errors)->with('notices', $notices);

return Redirect::to('mod/list')->with('notices', $notices);
}

public function getDelete($mod_id = null)
{
$mod = Mod::find($mod_id);
Expand Down Expand Up @@ -269,4 +339,9 @@ private function remote_mod_md5($mod, $version, $attempts = 0)
return $this->remote_mod_md5($mod, $version, $attempts + 1);
}
}
}
}

Validator::extend('csv', function($attribute, $value, $parameters)
{
return (false !== strpos($value->getClientOriginalName(), '.csv') && 'application/octet-stream' === $value->getClientMimeType());
});
56 changes: 56 additions & 0 deletions app/views/mod/bulk.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@extends('layouts/master')
@section('title')
<title>Bulk Upload Mods - TechnicSolder</title>
@stop
@section('content')
<div class="page-header">
<h1>Mod Library</h1>
</div>
<div class="panel panel-default">
<div class="panel-heading">
Bulk Upload Mods
</div>
<div class="panel-body">
@if ($errors->all())
<div class="alert alert-danger">
@foreach ($errors->all() as $error)
{{ $error }}<br />
@endforeach
</div>
@endif
@if (Session::get('notices'))
<div class="alert alert-info">
@foreach (Session::get('notices') as $notice)
{{ $notice }}<br />
@endforeach
</div>
@endif
{{ Form::open(array('url'=>URL::to('mod/bulk'),'files'=>true)) }}
<div class="row">
<div class="col-md-3">
<div class="form-group">
{{ Form::label('csv', 'Bulk Upload CSV') }}
{{ Form::file('csv', '', array('class'=>'form-controll')) }}
</div>
</div>
<div class="col-md-9">
<p>In order to have your file processed correctly it must end in .csv and follow the below example:</p>
<code>pretty_name,name,author,description,link,donatelink<br/>Cool Mod,cool-mod,Bob,A really cool starter mod.,"http://example.com","http://example.com"</code><br/><br/>
<p>Not having the first line with the column names will cause the file to break and there is currently no validation to make sure you put them in.</p>
</div>
</div>
{{ Form::submit('Bulk Upload Mods', array('class' => 'btn btn-success')) }}
{{ HTML::link('mod/list/', 'Go Back', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
@endsection
@section('bottom')
<script type="text/javascript">
$("#name").slugify('#pretty_name');
$(".modslug").slugify("#pretty_name");
$("#name").keyup(function() {
$(".modslug").html($(this).val());
});
</script>
@endsection
60 changes: 30 additions & 30 deletions app/views/mod/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@
</div>
<div class="panel-body">
@if ($errors->all())
<div class="alert alert-danger">
@foreach ($errors->all() as $error)
{{ $error }}<br />
@endforeach
</div>
@endif
<div class="alert alert-danger">
@foreach ($errors->all() as $error)
{{ $error }}<br />
@endforeach
</div>
@endif
<form method="post" action="{{ URL::to('mod/create') }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="pretty_name">Mod Name</label>
<input type="text" class="form-control" name="pretty_name" id="pretty_name">
</div>
<div class="form-group">
<label for="name">Mod Slug</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" class="form-control" name="author" id="author">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" class="form-control" rows="5"></textarea>
</div>
<div class="form-group">
<label for="link">Mod Website</label>
<input type="text" class="form-control" name="link" id="link">
</div>
<div class="form-group">
<label for="donatelink">Author Donation Link</label>
<input type="text" class="form-control" name="donatelink" id="donatelink">
<span class="help-block">This is only in use by the official Technic Solder</span>
</div>
<label for="pretty_name">Mod Name</label>
<input type="text" class="form-control" name="pretty_name" id="pretty_name">
</div>
<div class="form-group">
<label for="name">Mod Slug</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" class="form-control" name="author" id="author">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" class="form-control" rows="5"></textarea>
</div>
<div class="form-group">
<label for="link">Mod Website</label>
<input type="text" class="form-control" name="link" id="link">
</div>
<div class="form-group">
<label for="donatelink">Author Donation Link</label>
<input type="text" class="form-control" name="donatelink" id="donatelink">
<span class="help-block">This is only in use by the official Technic Solder</span>
</div>
</div>
<div class="col-md-6">
<p>Because Solder doesn't do any file handling yet you will need to manually manage your set of mods in your repository. The mod repository structure is very strict and must match your Solder data exact. An example of your mod directory structure will be listed below:</p>
Expand Down
1 change: 1 addition & 0 deletions app/views/mod/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<div class="panel panel-default">
<div class="panel-heading">
<div class="pull-right">
<a href="{{ URL::to('mod/bulk') }}" class="btn btn-xs btn-success">Bulk Upload Mods</a>
<a href="{{ URL::to('mod/create') }}" class="btn btn-xs btn-success">Add Mod</a>
</div>
Mod List
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
"composer/composer": "1.0.0-alpha9",
"knplabs/github-api": "1.4.*",
"guzzlehttp/guzzle": "5.2.*",
"aws/aws-sdk-php": "2.7.*"
"aws/aws-sdk-php": "2.7.*",
"phpunit/phpunit": "~4.8@dev",
"league/csv": "~7.1@dev"
},
"require-dev": {
"barryvdh/laravel-debugbar": "~1.8@dev",
"mockery/mockery": "master@dev",
"phpunit/phpunit": "~4.0"
"mockery/mockery": "master@dev"
},
"autoload": {
"classmap": [
Expand Down