Skip to content

Commit

Permalink
Finish custom field
Browse files Browse the repository at this point in the history
  • Loading branch information
ymhuang0808 committed Apr 22, 2016
1 parent cf33fc0 commit 37f769c
Show file tree
Hide file tree
Showing 37 changed files with 2,031 additions and 14 deletions.
40 changes: 40 additions & 0 deletions app/CustomField/Metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\CustomField;

use Illuminate\Contracts\Support\Arrayable;

abstract class Metadata implements \Serializable, Arrayable
{
protected $data;

public function __construct(array $data)
{
$this->set($data);
}

public function serialize()
{
return serialize($this->data);
}

public function unserialize($data)
{
$this->data = unserialize($data);
}

public function get()
{
return $this->data;
}

public function set(array $data)
{
$this->data = $data;
}

public function toArray()
{
return $this->data;
}
}
8 changes: 8 additions & 0 deletions app/CustomField/MetadataValidationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\CustomField;

interface MetadataValidationInterface
{
public function validate();
}
42 changes: 42 additions & 0 deletions app/CustomField/Payload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\CustomField;

use Illuminate\Contracts\Support\Arrayable;

class Payload implements PayloadContract, \Serializable, Arrayable
{
protected $data;

public function __construct(array $data)
{
$this->set($data);
}

public function get()
{
return $this->data;
}

public function set(array $data)
{
$this->data = $data;

return $this;
}

public function serialize()
{
return serialize($this->data);
}

public function unserialize($data)
{
$this->data = unserialize($data);
}

public function toArray()
{
return $this->data;
}
}
9 changes: 9 additions & 0 deletions app/CustomField/PayloadContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\CustomField;

interface PayloadContract
{
public function get();
public function set(array $payload);
}
16 changes: 16 additions & 0 deletions app/CustomField/RadioButtonMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\CustomField;

use App\CustomField\Metadata;

class RadioButtonMetadata extends Metadata implements MetadataValidationInterface
{
public function validate()
{
/**
* TODO: to be implemented
*/
return true;
}
}
16 changes: 16 additions & 0 deletions app/CustomField/TextMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\CustomField;

use App\CustomField\Metadata;

class TextMetadata extends Metadata implements MetadataValidationInterface
{
public function validate()
{
/**
* TODO: to be implemented
*/
return true;
}
}
89 changes: 89 additions & 0 deletions app/CustomField/TypeMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\CustomField;

class TypeMapping
{
private static $typeStrToIntMapping;
private static $typeIntToStrMapping;

public static function strToInt($key)
{
self::mappingType();

if (isset(self::$typeStrToIntMapping[$key]['number'])) {
return self::$typeStrToIntMapping[$key]['number'];
}

return null;
}

public static function strToMetadataClass($key)
{
self::mappingType();

if (isset(self::$typeStrToIntMapping[$key]['metadata'])) {
return self::$typeStrToIntMapping[$key]['metadata'];
}

return null;
}

public static function intToStr($key)
{
self::mappingType();

if (isset(self::$typeIntToStrMapping[$key]['type'])) {
return self::$typeIntToStrMapping[$key]['type'];
}

return null;
}

public static function intToClass($key)
{
self::mappingType();

if (isset(self::$typeIntToStrMapping[$key]['class'])) {
return self::$typeIntToStrMapping[$key]['class'];
}

return null;
}

protected static function mappingType()
{
if (isset(static::$typeStrToIntMapping) && isset(static::$typeIntToStrMapping)) {
return;
}

/**
* TODO: Check the configuration string exists
*/
$customFieldTypeConfig = config('constants.custom_field_type');
static::$typeStrToIntMapping = $customFieldTypeConfig;
static::$typeIntToStrMapping = self::reverseMapping($customFieldTypeConfig, 'type', 'number');
}

private static function reverseMapping($value, $originKeyName, $needleReversedKeyName)
{
$collection = collect($value);
$reversed = [];
$collection->each(function ($item, $key) use (&$reversed, $originKeyName, $needleReversedKeyName) {
$mappingValue = [];
$mappingKeyName = $item[$needleReversedKeyName];

unset($item[$needleReversedKeyName]);

$mappingValue[$originKeyName] = $key;

foreach ($item as $itemKey => $itemValue) {
$mappingValue[$itemKey] = $itemValue;
}

$reversed[$mappingKeyName] = $mappingValue;
});

return $reversed;
}
}
98 changes: 96 additions & 2 deletions app/Http/Controllers/Api/V1_0/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function show($id)
{
// Get the project model by $id
$project = Project::findOrFail($id);
$user = $this->jwtService->getUser();

if (!$this->isPublic($project)) {
$user = $this->jwtService->getUser();

// Check the permission
if (Gate::denies('show', $project)) {
Expand All @@ -78,6 +78,12 @@ public function show($id)
$resource = TransformerService::getResourceItem($project,
'App\Transformers\JsonApiProjectTransformer', 'projects');

// set meta for the resource
$meta = [
'role' => $this->getRoleInProject($user, $project)
];
$resource->setMeta($meta);

$manager->parseIncludes(['managers', 'hyperlinks']);

$result = $manager->createData($resource)->toArray();
Expand Down Expand Up @@ -122,6 +128,40 @@ public function update(UpdateProjectRequest $request, $id)
return response()->json($result, $status);
}

public function showManagedProjects()
{
$user = $this->jwtService->getUser();
$projects = $user->manageProjects()->get();

$manager = TransformerService::getJsonApiManager();
$resource = TransformerService::getResourceCollection($projects,
'App\Transformers\JsonApiProjectTransformer', 'projects');

$manager->parseIncludes(['managers', 'hyperlinks']);

$result = $manager->createData($resource)->toArray();
$status = 200;

return response()->json($result, $status);
}

public function showSelfAttendingProjects()
{
$volunteer = $this->jwtService->getUser();
$projects = $volunteer->attendingProjects()->get();

$manager = TransformerService::getJsonApiManager();
$resource = TransformerService::getResourceCollection($projects,
'App\Transformers\JsonApiProjectTransformer', 'projects');

$manager->parseIncludes(['managers', 'hyperlinks']);

$result = $manager->createData($resource)->toArray();
$status = 200;

return response()->json($result, $status);
}

public function showAll(ProjectDbQueryRepository $repository)
{
$user = $this->jwtService->getUser();
Expand Down Expand Up @@ -163,6 +203,33 @@ public function attachVolunteer(AttachVolunteerInProjectRequest $request, $id)
return response()->json([], 204);
}

public function attend($projectId)
{
$volunteer = $this->jwtService->getUser();
$project = Project::findOrFail($projectId);

if (!$volunteer->inProject($project)) {
// The volunteer is not in the project
$volunteer->attachProject(
$project,
config('constants.member_project_permission.PRIVATE_FOR_ALL_ATTENDING_MANAGER')
);

$result = [];
$status = 204;
} else {
// The volunteer is already in the project
// Throw a exception
throw new s\GeneralException(
'Exists in the project',
'exists_in_the_project',
409
);
}

return response()->json($result, $status);
}

/**
* Detach a volunteer from a project
* @param DetachVolunteerInProjectRequest $request
Expand All @@ -188,7 +255,7 @@ public function showMembers(ShowProjectRequest $request, $projectId)
$members = $project->members()->get();
} else {
// Not a project manager
$members = $project->viewableMembers($user);
$members = $project->viewableMembers($user, $project);
}

$manager = TransformerService::getJsonApiManager();
Expand All @@ -209,4 +276,31 @@ protected function isPublic(Project $project)

return false;
}

protected function getRoleInProject(Volunteer $user, Project $project)
{
$role = [];

if ($user->isCreatorOfProject($project)) {
$role = [
'name' => 'creator'
];
} elseif ($user->inProject($project)) {
$role = [
'name' => 'member'
];

if ($user->isAttendingProject($project)) {
$role['status'] = 'attending';
} elseif ($user->isPendingProject($project)) {
$role['status'] = 'pending';
}
} else {
$role = [
'name' => 'guest'
];
}

return $role;
}
}
Loading

0 comments on commit 37f769c

Please sign in to comment.