Skip to content

Commit

Permalink
Merge branch 'bugfix/db-connection' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrossi committed Jan 19, 2017
2 parents 8acd570 + f47fbfb commit bde13ea
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 152 deletions.
1 change: 0 additions & 1 deletion src/AdvancedCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class AdvancedCustomFields
public function __construct(Post $post)
{
$this->post = $post;
FieldFactory::$connection = $post->getConnectionName();
}

/**
Expand Down
34 changes: 9 additions & 25 deletions src/Field/BasicField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,26 @@ abstract class BasicField

/**
* Constructor method.
*
* @param Post $post
*/
public function __construct()
public function __construct(Post $post)
{
$this->post = $post;
$this->postMeta = new PostMeta();
$this->postMeta->setConnection($post->getConnectionName());
}

/**
* Get the value of a field according it's post ID.
*
* @param string $field
* @param Post $post
*
* @return array|string
*/
public function fetchValue($field, Post $post)
public function fetchValue($field)
{
$postMeta = $this->postMeta->where('post_id', $post->ID)
$postMeta = $this->postMeta->where('post_id', $this->post->ID)
->where('meta_key', $field)
->first();

Expand All @@ -85,16 +88,14 @@ public function fetchValue($field, Post $post)

/**
* @param string $fieldName
* @param Post $post
*
* @return string
*/
public function fetchFieldKey($fieldName, Post $post)
public function fetchFieldKey($fieldName)
{
$this->post = $post;
$this->name = $fieldName;

$postMeta = $this->postMeta->where('post_id', $post->ID)
$postMeta = $this->postMeta->where('post_id', $this->post->ID)
->where('meta_key', '_' . $fieldName)
->first();

Expand Down Expand Up @@ -133,21 +134,4 @@ public function __toString()
{
return $this->get();
}

/**
* @return string
*/
public function getConnection()
{
return $this->connection;
}

/**
* @param string $connection
*/
public function setConnection($connection)
{
$this->connection = $connection;
$this->postMeta->setConnection($connection);
}
}
5 changes: 2 additions & 3 deletions src/Field/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ class DateTime extends BasicField implements FieldInterface

/**
* @param string $fieldName
* @param Post $post
*/
public function process($fieldName, Post $post)
public function process($fieldName)
{
$dateString = $this->fetchValue($fieldName, $post);
$dateString = $this->fetchValue($fieldName);
$format = $this->getDateFormatFromString($dateString);
$this->date = Carbon::createFromFormat($format, $dateString);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ class File extends BasicField implements FieldInterface

/**
* @param string $field
* @param Post $post
*/
public function process($field, Post $post)
public function process($field)
{
$value = $this->fetchValue($field, $post);
$file = $post->find($value);
$value = $this->fetchValue($field);
$file = $this->post->find($value);
$this->fillFields($file);
}

Expand Down
9 changes: 4 additions & 5 deletions src/Field/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ class Gallery extends Image implements FieldInterface

/**
* @param $field
* @param Post $post
*/
public function process($field, Post $post)
public function process($field)
{
$ids = $this->fetchValue($field, $post);
$attachments = $post->whereIn('ID', $ids)->get();
$ids = $this->fetchValue($field);
$attachments = $this->post->whereIn('ID', $ids)->get();
$metaDataValues = $this->fetchMultipleMetadataValues($attachments);

foreach ($attachments as $attachment) {
$image = new Image();
$image = new Image($this->post);
$image->fillFields($attachment);
$image->fillMetadataFields($metaDataValues[$attachment->ID]);
$this->images[] = $image;
Expand Down
11 changes: 5 additions & 6 deletions src/Field/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ class Image extends BasicField implements FieldInterface

/**
* @param string $field
* @param Post $post
*/
public function process($field, Post $post)
public function process($field)
{
$attachmentId = $this->fetchValue($field, $post);
$attachmentId = $this->fetchValue($field);

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

$imageData = $this->fetchMetadataValue($attachment);
Expand Down Expand Up @@ -107,7 +106,7 @@ public function size($size)
*/
protected function fillThumbnailFields(array $data)
{
$size = new static();
$size = new static($this->post);
$size->filename = $data['file'];
$size->width = $data['width'];
$size->height = $data['height'];
Expand All @@ -133,7 +132,7 @@ protected function fetchMetadataValue(Post $attachment)
/**
* @param Collection $attachments
*
* @return Collection
* @return Collection|array
*/
protected function fetchMultipleMetadataValues(Collection $attachments)
{
Expand Down
9 changes: 4 additions & 5 deletions src/Field/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ class PostObject extends BasicField implements FieldInterface

/**
* @param string $fieldName
* @param Post $post
*/
public function process($fieldName, Post $post)
public function process($fieldName)
{
$postId = $this->fetchValue($fieldName, $post);
$postId = $this->fetchValue($fieldName);

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

Expand Down
6 changes: 2 additions & 4 deletions src/Field/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ class Repeater extends BasicField implements FieldInterface

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

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

$this->fields = new Collection($fields);
Expand Down
13 changes: 8 additions & 5 deletions src/Field/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ class Term extends BasicField implements FieldInterface
*/
protected $term;

public function __construct()
/**
* @param Post $post
*/
public function __construct(Post $post)
{
parent::__construct();
parent::__construct($post);
$this->term = new \Corcel\Term();
$this->term->setConnection($post->getConnectionName());
}

/**
* @param string $fieldName
* @param Post $post
*/
public function process($fieldName, Post $post)
public function process($fieldName)
{
$value = $this->fetchValue($fieldName, $post);
$value = $this->fetchValue($fieldName);
if (is_array($value)) {
$this->items = $this->term->whereIn('term_id', $value)->get(); // ids
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/Field/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class Text extends BasicField implements FieldInterface
*/
protected $value;

public function process($field, Post $post)
/**
* @param string $field
*/
public function process($field)
{
$this->value = $this->fetchValue($field, $post);
$this->value = $this->fetchValue($field);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Field/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ class User extends BasicField implements FieldInterface
*/
protected $value;

public function __construct()
/**
* @param Post $post
*/
public function __construct(Post $post)
{
parent::__construct();
parent::__construct($post);
$this->user = new \Corcel\User();
$this->user->setConnection($post->getConnectionName());
}

/**
* @param string $fieldName
* @param Post $post
*/
public function process($fieldName, Post $post)
public function process($fieldName)
{
$userId = $this->fetchValue($fieldName, $post);
$userId = $this->fetchValue($fieldName);
$this->value = $this->user->find($userId);
}

Expand Down
46 changes: 16 additions & 30 deletions src/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,22 @@
*/
class FieldFactory
{
/**
* @var null|string
*/
public static $connection = null;

private function __construct()
{
}

/**
* @param string $name
* @param Post $post
* @param string $name
* @param Post $post
* @param null|string $type
*
* @return FieldInterface|Collection|string
*/
public static function make($name, Post $post, $type = null)
{
if (null === $type) {
$fakeText = new Text();

if (static::$connection) {
$fakeText->setConnection(static::$connection);
}

$key = $fakeText->fetchFieldKey($name, $post);
$fakeText = new Text($post);
$key = $fakeText->fetchFieldKey($name);

if ($key === null) { // Field does not exist
return null;
Expand All @@ -73,52 +63,48 @@ public static function make($name, Post $post, $type = null)
case 'select':
case 'checkbox':
case 'radio':
$field = new Text();
$field = new Text($post);
break;
case 'image':
case 'img':
$field = new Image();
$field = new Image($post);
break;
case 'file':
$field = new File();
$field = new File($post);
break;
case 'gallery':
$field = new Gallery();
$field = new Gallery($post);
break;
case 'true_false':
case 'boolean':
$field = new Boolean();
$field = new Boolean($post);
break;
case 'post_object':
case 'post':
case 'relationship':
$field = new PostObject();
$field = new PostObject($post);
break;
case 'page_link':
$field = new PageLink();
$field = new PageLink($post);
break;
case 'taxonomy':
case 'term':
$field = new Term();
$field = new Term($post);
break;
case 'user':
$field = new User();
$field = new User($post);
break;
case 'date_picker':
case 'date_time_picker':
case 'time_picker':
$field = new DateTime();
$field = new DateTime($post);
break;
case 'repeater':
$field = new Repeater();
$field = new Repeater($post);
break;
}

if (static::$connection) {
$field->setConnection(static::$connection);
}

$field->process($name, $post);
$field->process($name);

return $field;
}
Expand Down
3 changes: 1 addition & 2 deletions src/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ interface FieldInterface
{
/**
* @param string $fieldName
* @param Post $post
*/
public function process($fieldName, Post $post);
public function process($fieldName);

/**
* @return mixed
Expand Down
Loading

0 comments on commit bde13ea

Please sign in to comment.