Created
April 19, 2022 13:47
-
-
Save jsakhil/883e034d324db49af9d43713474e545e to your computer and use it in GitHub Desktop.
FormsModulePlugin.php | File Path /core/anomaly/forms-module/src | getField for Mail Notification | {{ forms_getField(input,'field_slug') | raw }}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Anomaly\FormsModule; | |
use Anomaly\FormsModule\Form\Contract\FormRepositoryInterface; | |
use Anomaly\Streams\Platform\Addon\Plugin\Plugin; | |
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface; | |
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; | |
use Anomaly\Streams\Platform\Support\Decorator; | |
use Anomaly\Streams\Platform\Support\Presenter; | |
/** | |
* Class FormsModulePlugin | |
* | |
* @link http://anomaly.is/streams-platform | |
* @author AnomalyLabs, Inc. <[email protected]> | |
* @author Ryan Thompson <[email protected]> | |
* @package Anomaly\FormsModule | |
*/ | |
class FormsModulePlugin extends Plugin | |
{ | |
/** | |
* The form repository. | |
* | |
* @var FormRepositoryInterface | |
*/ | |
protected $forms; | |
/** | |
* The presenter decorator. | |
* | |
* @var Decorator | |
*/ | |
protected $decorator; | |
/** | |
* Create a new FormsModulePlugin instance. | |
* | |
* @param FormRepositoryInterface $forms | |
* @param Decorator $decorator | |
*/ | |
public function __construct(FormRepositoryInterface $forms, Decorator $decorator) | |
{ | |
$this->forms = $forms; | |
$this->decorator = $decorator; | |
} | |
/** | |
* Get the functions. | |
* | |
* @return array | |
*/ | |
public function getFunctions() | |
{ | |
return [ | |
new \Twig_SimpleFunction('forms_get', [$this, 'get'], ['is_safe' => ['html']]), | |
new \Twig_SimpleFunction('forms_input', [$this, 'input'], ['is_safe' => ['html']]), | |
new \Twig_SimpleFunction('forms_display', [$this, 'get'], ['is_safe' => ['html']]), | |
new \Twig_SimpleFunction('forms_getField', [$this, 'getField'], ['is_safe' => ['html']]), | |
new \Twig_SimpleFunction('forms_getDate', [$this, 'getDate'], ['is_safe' => ['html']]), | |
new \Twig_SimpleFunction('forms_getLocation', [$this, 'getLocation'], ['is_safe' => ['html']]), | |
]; | |
} | |
/** | |
* Return the form presenter. | |
* | |
* @param $slug | |
* @return array|\ArrayAccess|\IteratorAggregate|null|\Robbo\Presenter\Presenter | |
*/ | |
public function get($slug) | |
{ | |
if (!$form = $this->forms->findBySlug($slug)) { | |
return null; | |
} | |
$handler = $form->getFormHandler(); | |
$builder = $handler->builder($form); | |
return $this->decorator->decorate( | |
$builder->make()->getForm() | |
); | |
} | |
/** | |
* Return the form input. | |
* | |
* @param $input | |
*/ | |
public function input($input) | |
{ | |
$output = ''; | |
if ($input instanceof Presenter) { | |
$input = $input->getObject(); | |
} | |
/* @var EntryInterface $input */ | |
/* @var AssignmentInterface $assignment */ | |
foreach ($input->getAssignments() as $assignment) { | |
$value = $input->getFieldValue($assignment->getFieldSlug()); | |
if (is_array($value)) { | |
$value = implode(', ', $value); | |
} | |
$label = $assignment->getFieldName(); | |
$output .= "<strong>{$label}: </strong> {$value}<br>"; | |
} | |
return $output; | |
} | |
public function getField($input,$fieldslug) | |
{ | |
$output = ''; | |
if ($input instanceof Presenter) { | |
$input = $input->getObject(); | |
} | |
/* @var EntryInterface $input */ | |
/* @var AssignmentInterface $assignment */ | |
foreach ($input->getAssignments() as $assignment) { | |
if($assignment->getFieldSlug() == $fieldslug){ | |
$value = $input->getFieldValue($assignment->getFieldSlug()); | |
if (is_array($value)) { | |
$value = implode(', ', $value); | |
} | |
$label = $assignment->getFieldName(); | |
$output .= "{$value}"; | |
} | |
} | |
return $output; | |
} | |
public function getDate($input,$fieldslug) | |
{ | |
if ($input instanceof Presenter) { | |
$input = $input->getObject(); | |
} | |
foreach ($input->getAssignments() as $assignment) { | |
if($assignment->getFieldSlug() == $fieldslug){ | |
$eventId = $input->getFieldValue($assignment->getFieldSlug()); | |
} | |
} | |
$event = DB::table('posts_posts') | |
->join('posts_posts_translations','posts_posts_translations.entry_id','=','posts_posts.id') | |
->join('posts_events_posts','posts_events_posts.id','=','posts_posts.entry_id') | |
->select('event_date_and_time','to_date_and_time') | |
->where('posts_posts.id',$eventId) | |
->get(); | |
$event_date_and_time = new DateTime($event[0]->event_date_and_time,new DateTimeZone('UTC')); | |
$event_date_and_time->setTimezone(new DateTimeZone('America/Chicago')); | |
$to_date_and_time = $event[0]->to_date_and_time; | |
if($event[0]->to_date_and_time){ | |
$to_date_and_time = new DateTime($event[0]->to_date_and_time,new DateTimeZone('UTC')); | |
$to_date_and_time->setTimezone(new DateTimeZone('America/Chicago')); | |
} | |
$output = $event_date_and_time->format('l, F d Y, h:i A').($event[0]->to_date_and_time ? ' - '.$to_date_and_time->format('l, F d Y, h:i A') : ''); | |
return $output; | |
} | |
public function getLocation($input,$fieldslug) | |
{ | |
if ($input instanceof Presenter) { | |
$input = $input->getObject(); | |
} | |
foreach ($input->getAssignments() as $assignment) { | |
if($assignment->getFieldSlug() == $fieldslug){ | |
$eventId = $input->getFieldValue($assignment->getFieldSlug()); | |
} | |
} | |
$event = DB::table('posts_posts') | |
->join('posts_posts_translations','posts_posts_translations.entry_id','=','posts_posts.id') | |
->join('posts_events_posts','posts_events_posts.id','=','posts_posts.entry_id') | |
->select('address','state','postal_zip') | |
->where('posts_posts.id',$eventId) | |
->get(); | |
$output = $event[0]->address.($event[0]->state ? ', '.$event[0]->state : '').' '.$event[0]->postal_zip; | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment