Skip to content

Commit

Permalink
Merge branch 'release/0.1.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrossi committed Feb 15, 2017
2 parents 71178b6 + 2346c8c commit 45a29bd
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 23 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ First we should create the fields classes and the test cases. After we have to s
| Date Time Picker | ok | [@jgrossi](http://github.com/jgrossi) | `Carbon\Carbon` |
| Time Picker | ok | [@jgrossi](http://github.com/jgrossi) | `Carbon\Carbon` |
| Color Picker | ok | [@jgrossi](http://github.com/jgrossi) | `string` |
| Repeater | partially | [@jgrossi](http://github.com/jgrossi) | `Collection` of fields |
| Flexible Content | missing | | |
| Repeater | ok | [@jgrossi](http://github.com/jgrossi) | `Collection` of fields |
| Flexible Content | ok | [@marcoboom](http://github.com/marcoboom) | `Collection` |

# Contributing

Expand Down
2 changes: 1 addition & 1 deletion src/AdvancedCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __get($name)
{
$field = FieldFactory::make($name, $this->post);

return $field->get();
return $field ? $field->get() : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Field/BasicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function fetchValue($field)
->where('meta_key', $field)
->first();

if (isset($postMeta->meta_value) and $postMeta->meta_value) {
if (isset($postMeta->meta_value) and ! is_null($postMeta->meta_value)) {
$value = $postMeta->meta_value;
if ($array = @unserialize($value) and is_array($array)) {
$this->value = $array;
Expand Down
117 changes: 117 additions & 0 deletions src/Field/FlexibleContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Corcel\Acf\Field;

use Corcel\Acf\FieldFactory;
use Corcel\Acf\FieldInterface;
use Corcel\Post;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

/**
* Class Flexible Content.
*
* @author Marco Boom <[email protected]>
*/
class FlexibleContent extends BasicField implements FieldInterface
{
/**
* @var Collection
*/
protected $fields;

/**
* @param string $fieldName
*/
public function process($fieldName)
{
$this->name = $fieldName;

$builder = $this->fetchPostsMeta($fieldName, $this->post);
$fields = $this->fetchFields($fieldName, $builder);

$this->fields = new Collection($fields);
}

/**
* @return Collection
*/
public function get()
{
return $this->fields;
}

/**
* @param string $metaKey
* @param string $fieldName
*
* @return int
*/
protected function retrieveIdFromFieldName($metaKey, $fieldName)
{
return (int) str_replace("{$fieldName}_", '', $metaKey);
}

/**
* @param string $metaKey
* @param string $fieldName
* @param int $id
*
* @return string
*/
protected function retrieveFieldName($metaKey, $fieldName, $id)
{
$pattern = "{$fieldName}_{$id}_";

return str_replace($pattern, '', $metaKey);
}

/**
* @param $fieldName
* @param Post $post
*
* @return mixed
*/
protected function fetchPostsMeta($fieldName, Post $post)
{
$builder = $this->postMeta->where('post_id', $post->ID);
$builder->where(function ($query) use ($fieldName) {
$query->orWhere('meta_key', 'like', "{$fieldName}_%");
});

return $builder;
}

/**
* @param $fieldName
* @param $builder
*
* @return mixed
*/
protected function fetchFields($fieldName, Builder $builder)
{
$fields = [];
$blocks = $this->fetchValue($fieldName, $this->post);

foreach ($builder->get() as $meta) {
$id = $this->retrieveIdFromFieldName($meta->meta_key, $fieldName);

$name = $this->retrieveFieldName($meta->meta_key, $fieldName, $id);
$field = FieldFactory::make($meta->meta_key, $this->post->find($meta->post_id));

if (!array_key_exists($id, $blocks)) {
continue;
}

if (empty($fields[$id])) {
$fields[$id] = new \stdClass;
$fields[$id]->type = $blocks[$id];
$fields[$id]->fields = new \stdClass;
}

$fields[$id]->fields->$name = $field->get();
}

return $fields;
}
}
21 changes: 12 additions & 9 deletions src/Field/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ class Gallery extends Image implements FieldInterface
*/
public function process($field)
{
$ids = $this->fetchValue($field);
$attachments = $this->post->whereIn('ID', $ids)->get();
$metaDataValues = $this->fetchMultipleMetadataValues($attachments);

foreach ($attachments as $attachment) {
$image = new Image($this->post);
$image->fillFields($attachment);
$image->fillMetadataFields($metaDataValues[$attachment->ID]);
$this->images[] = $image;
if ($ids = $this->fetchValue($field)) {
$connection = $this->post->getConnectionName();
$attachments = Post::on($connection)->whereIn('ID', $ids)->get();

$metaDataValues = $this->fetchMultipleMetadataValues($attachments);

foreach ($attachments as $attachment) {
$image = new Image($this->post);
$image->fillFields($attachment);
$image->fillMetadataFields($metaDataValues[$attachment->ID]);
$this->images[] = $image;
}
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/Field/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Corcel\Acf\Field;

use Corcel\Acf\FieldInterface;
use Corcel\Post;
use Corcel\Acf\FieldInterface;
use Illuminate\Database\Eloquent\Collection;

/**
Expand Down Expand Up @@ -59,13 +59,16 @@ class Image extends BasicField implements FieldInterface
public function process($field)
{
$attachmentId = $this->fetchValue($field);

$attachment = $this->post->attachment()->find(intval($attachmentId));
$connection = $this->post->getConnectionName();

$this->fillFields($attachment);
if ($attachment = Post::on($connection)->find(intval($attachmentId))) {
$this->fillFields($attachment);

$imageData = $this->fetchMetadataValue($attachment);
$this->fillMetadataFields($imageData);
$imageData = $this->fetchMetadataValue($attachment);

$this->fillMetadataFields($imageData);
}
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Field/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class PostObject extends BasicField implements FieldInterface
public function process($fieldName)
{
$postId = $this->fetchValue($fieldName);

$connection = $this->post->getConnectionName();

if (is_array($postId)) {
$this->object = $this->post->whereIn('ID', $postId)->get();
$this->object = Post::on($connection)->whereIn('ID', $postId)->get();
} else {
$this->object = $this->post->find($postId);
$this->object = Post::on($connection)->find($postId);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Corcel\Acf\Field\PageLink;
use Corcel\Acf\Field\PostObject;
use Corcel\Acf\Field\Repeater;
use Corcel\Acf\Field\FlexibleContent;
use Corcel\Acf\Field\Select;
use Corcel\Acf\Field\Term;
use Corcel\Acf\Field\Text;
Expand Down Expand Up @@ -48,6 +49,7 @@ public static function make($name, Post $post, $type = null)
$type = $fakeText->fetchFieldType($key);
}


switch ($type) {
case 'text':
case 'textarea':
Expand Down Expand Up @@ -102,6 +104,10 @@ public static function make($name, Post $post, $type = null)
case 'repeater':
$field = new Repeater($post);
break;
case 'flexible_content':
$field = new FlexibleContent($post);
break;
default: return null;
}

$field->process($name);
Expand Down
74 changes: 74 additions & 0 deletions tests/EmptyBasicFieldsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use Corcel\Acf\Field\Text;
use Corcel\Acf\Field\BasicField;
use Corcel\Post;

/**
* Class BasicFieldTest.
*
* @author Junior Grossi <[email protected]>
*/
class EmptyBasicFieldsTest extends PHPUnit_Framework_TestCase
{
/**
* @var Post
*/
protected $post;

/**
* Setup a base $this->post object to represent the page with the basic fields.
*/
protected function setUp()
{
$this->post = Post::find(91); // it' a page with empty custom fields
}

public function testTextFieldValue()
{
$text = new Text($this->post);
$text->process('fake_text');

$this->assertSame('', $text->get());
}

public function testTextareaFieldValue()
{
$textarea = new Text($this->post);
$textarea->process('fake_textarea');

$this->assertSame('', $textarea->get());
}

public function testNumberFieldValue()
{
$number = new Text($this->post);
$number->process('fake_number');

$this->assertSame('', $number->get());
}

public function testEmailFieldValue()
{
$email = new Text($this->post);
$email->process('fake_email');

$this->assertSame('', $email->get());
}

public function testUrlFieldValue()
{
$url = new Text($this->post);
$url->process('fake_url');

$this->assertSame('', $url->get());
}

public function testPasswordFieldValue()
{
$password = new Text($this->post);
$password->process('fake_password');

$this->assertSame('', $password->get());
}
}
25 changes: 24 additions & 1 deletion tests/LayoutFieldsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Corcel\Acf\Field\Repeater;
use Corcel\Acf\Field\FlexibleContent;

/**
* Class LayoutFieldsTest.
Expand Down Expand Up @@ -29,10 +30,32 @@ public function testComplexRepeaterField()
$repeater->process('fake_repeater_2');
$fields = $repeater->get()->toArray();


$this->assertEquals('admin', $fields[0]['fake_user']->nickname);
$this->assertEquals('admin', $fields[1]['fake_user']->nickname);
$this->assertEquals(2, $fields[0]['fake_relationship']->count());
$this->assertEquals(1, $fields[1]['fake_relationship']->count());
$this->assertInstanceOf(Post::class, $fields[0]['fake_relationship']->first());
$this->assertInstanceOf('Corcel\Post', $fields[0]['fake_relationship']->first());
}


public function testFlexibleContentField()
{
$page = Post::find(86);
$flex = new FlexibleContent($page);
$flex->process('fake_flexible_content');
$layout = $flex->get();

$this->assertEquals(3, $layout->count());

$this->assertEquals('normal_text', $layout[0]->type);
$this->assertEquals('Lorem ipsum', $layout[0]->fields->text);

$this->assertEquals('related_post', $layout[1]->type);
$this->assertInstanceOf('Corcel\Post', $layout[1]->fields->post);

$this->assertEquals('multiple_posts', $layout[2]->type);
$this->assertEquals(2, $layout[2]->fields->post->count());
$this->assertInstanceOf('Corcel\Post', $layout[2]->fields->post->first());
}
}
Binary file modified tests/config/database.sql.zip
Binary file not shown.

0 comments on commit 45a29bd

Please sign in to comment.