Skip to content

Instantly share code, notes, and snippets.

@chihiro-adachi
Created June 21, 2016 11:15
Show Gist options
  • Save chihiro-adachi/bbb897fe06d4d329d8b20b42874562b8 to your computer and use it in GitHub Desktop.
Save chihiro-adachi/bbb897fe06d4d329d8b20b42874562b8 to your computer and use it in GitHub Desktop.
Functions.php
<?php
namespace Plugin\Sample;
use Eccube\Application;
use Eccube\Event\EccubeEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Functions implements EventSubscriberInterface
{
private $app;
/**
* Constructor function.
*
* @param Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* 会員編集画面にフォーム項目を追加する.
*
* @param \Eccube\Event\EventArgs $args
*/
public function onAdminCustomerEditInitialize(\Eccube\Event\EventArgs $args)
{
// フォーム項目の追加
$builder = $args->getArgument('builder');
$builder->add(
'plg_memo',
'text',
array(
'mapped' => false,
'constraints' => array(
new \Symfony\Component\Validator\Constraints\NotBlank(),
),
)
);
// オプションテーブルからデータを取得
$Customer = $args->getArgument('Customer');
if ($Customer->getId()) {
$Option = $this->app['eccube.repository.options']->findBy(
array(
'plugin_code' => 'customer_memo',
'key' => $Customer->getId(),
)
);
$builder->get('plg_memo')->setData($Option->getValue());
}
}
/**
* 追加項目をデータベースに保存する.
*
* @param \Eccube\Event\EventArgs $args
*/
public function onAdminCustomerIndexComplete(\Eccube\Event\EventArgs $args)
{
$Customer = $args->getArgument('Customer');
$Option = $this->app['eccube.repository.options']->findBy(
array(
'plugin_code' => 'customer_memo',
'key' => $Customer->getId(),
)
);
if (is_null($Option)) {
$Option = new Option();
$Option->setPluginCode('customer_memo');
$Option->setKey($Customer->getId());
}
$form = $args->getArgument('form');
$memo = $form['plg_memo']->getData();
$Option->setValue($memo);
$this->app['eccube.repository.options']->save($Option);
}
/**
* Return the events to subscribe to.
*
* @return array
*/
public static function getSubscribedEvents()
{
return array(
EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE => 'onAdminCustomerIndexComplete',
EccubeEvents::ADMIN_CUSTOMER_EDIT_INITIALIZE => 'onAdminCustomerEditInitialize',
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment