Skip to content

Commit

Permalink
Better AdminGrid code style
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz committed Oct 15, 2014
1 parent b7c1f8b commit d5b1275
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 94 deletions.
20 changes: 11 additions & 9 deletions src/Notifications/AdminModule/SettingsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,23 @@ protected function createComponentTable()
->getCellPrototype()->width = '20%';

// actions
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';
$event = $table->addActionEvent('edit', 'Edit');
$event->getElementPrototype()->class[] = 'ajax';

$form = $admin->addForm('notification', 'Notification setting', $this->notificationSettingFormFactory);
$form = $admin->addForm('notification', 'Notification setting', function (NotificationSetting $notificationSetting = null) {
return $this->notificationSettingFormService->getFormFactory($notificationSetting !== null ? $notificationSetting->getId() : null);
});

$admin->connectFormWithAction($form, $table->getAction('edit'));
$admin->connectFormWithAction($form, $event);

// Toolbar
$toolbar = $admin->getNavbar();
$toolbar->addSection('new', 'Create', 'file');
$admin->connectFormWithNavbar($form, $toolbar->getSection('new'));
$section = $toolbar->addSection('new', 'Create', 'file');
$admin->connectFormWithNavbar($form, $section);

$table->addActionEvent('delete', 'Delete')
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));
$deleteEvent = $table->addActionEvent('delete', 'Delete');
$deleteEvent->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($deleteEvent);

return $admin;
}
Expand Down
33 changes: 15 additions & 18 deletions src/Security/AdminModule/AccountPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ protected function createComponentLoginTable()
$securityManager = $this->securityManager;
$providerFormFactory = $this->providerFormFactory;

// actions
$table->addActionEvent('connect', 'Connect')
$connectAction = $table->addActionEvent('connect', 'Connect')
->setCustomRender(function ($entity, $element) use ($securityManager, $user) {
if ($user->hasLoginProvider($entity['name'])) {
$element->class[] = 'disabled';
Expand All @@ -174,15 +173,14 @@ protected function createComponentLoginTable()

return $element;
});
$table->getAction('connect')->onClick[] = function ($button, $name) use ($_this, $securityManager, $providerFormFactory, $user) {
$connectAction->onClick[] = function ($button, $name) use ($_this, $securityManager, $providerFormFactory, $user) {
if (!$securityManager->getLoginProviderByName(str_replace('_', ' ', $name))->getFormContainer()) {
$_this->redirect('connect!', str_replace('_', ' ', $name));
} else {
$_this->provider = str_replace('_', ' ', $name);
$providerFormFactory->setProvider($_this->provider);
}
};
$table->getAction('connect');

$this->providerFormFactory->setUser($user);
if ($this->provider) {
Expand All @@ -195,7 +193,6 @@ protected function createComponentLoginTable()
$_this->redirect('this');
};
$form = $admin->addForm('provider', 'Provider', $this->providerFormFactory);
$admin->connectFormWithAction($form, $table->getAction('connect'));

$table->addActionEvent('disconnect', 'Disconnect')
->setCustomRender(function ($entity, $element) use ($securityManager, $user) {
Expand All @@ -213,6 +210,8 @@ protected function createComponentLoginTable()
};
$table->getAction('disconnect')->getElementPrototype()->class[] = 'ajax';

$admin->connectFormWithAction($form, $connectAction);

return $admin;
}

Expand All @@ -224,7 +223,6 @@ protected function createComponentTable()
$session = $this->session;
$admin = $this->adminGridFactory->create($this->loginRepository);

// columns
$table = $admin->getTable();
if ($this->user->identity instanceof User) {
$table->setModel(new Doctrine($this->loginRepository->createQueryBuilder('a')->andWhere('a.user = :user')->setParameter('user', $this->user->identity)));
Expand All @@ -246,10 +244,10 @@ protected function createComponentTable()
$table->addColumnText('sessionId', 'Session ID')
->getCellPrototype()->width = '50%';

// actions
$table->addActionEvent('delete', 'Delete')
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));
$deleteAction = $table->addActionEvent('delete', 'Delete');
$deleteAction->getElementPrototype()->class[] = 'ajax';

$admin->connectActionAsDelete($deleteAction);

return $admin;
}
Expand All @@ -266,18 +264,17 @@ protected function createComponentAccountForm()
->getFrontFormService();

$form = $formService
->createFormFactory($user->getExtendedUser())
->getFormFactory($user->getId())
->create();

$form->onSuccess[] = $this->accountFormSuccess;
$form->onSuccess[] = function () {
$this->flashMessage($this->translator->translate('Account settings has been updated.'), 'success');
$this->redirect('this');

return $form;
}
$this->redrawControl('content');
};

public function accountFormSuccess()
{
$this->flashMessage($this->translator->translate('Account settings has been updated.'), 'success');
$this->redirect('this');
return $form;
}

}
30 changes: 12 additions & 18 deletions src/Security/AdminModule/DefaultPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,12 @@ protected function createComponentTable()
$table->setTranslator($this->translator);
$table->setPrimaryKey('user.id');

// columns
$table->addColumnText('user.email', 'E-mail')
->setSortable()
->getCellPrototype()->width = '100%';
$table->getColumn('user.email')
->setFilterText()->setSuggestion();

// actions
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';

$table->addActionEvent('loginProviders', 'Login providers')
->getElementPrototype()->class[] = 'ajax';

$type = $this->type;

$form = $admin->addForm('user', 'User', function (ExtendedUser $extendedUser = null) {
return $this->getUserType()->getFormService()->getFormFactory($extendedUser ? $extendedUser->getUser()->getId() : null);
}, Form::TYPE_LARGE);
Expand All @@ -129,13 +119,21 @@ protected function createComponentTable()

$providerForm = $admin->addForm('loginProviders', 'Login providers', $this->providersForm, null, Form::TYPE_LARGE);

$admin->connectFormWithAction($form, $table->getAction('edit'), $admin::MODE_PLACE);
$admin->connectFormWithAction($providerForm, $table->getAction('loginProviders'));

// Toolbar
$toolbar = $admin->getNavbar();
$section = $toolbar->addSection('new', 'Create', 'user');

$editAction = $table->addActionEvent('edit', 'Edit');
$editAction->getElementPrototype()->class[] = 'ajax';

$loginProviders = $table->addActionEvent('loginProviders', 'Login providers');
$loginProviders->getElementPrototype()->class[] = 'ajax';

$deleteAction = $table->addActionEvent('delete', 'Delete');
$deleteAction->getElementPrototype()->class[] = 'ajax';

$admin->connectFormWithAction($form, $editAction, $admin::MODE_PLACE);
$admin->connectFormWithAction($providerForm, $loginProviders);
$admin->connectActionAsDelete($deleteAction);
foreach ($this->securityManager->getUserTypes() as $type => $value) {
$admin->connectFormWithNavbar(
$form,
Expand All @@ -144,10 +142,6 @@ protected function createComponentTable()
);
}

$table->addActionEvent('delete', 'Delete')
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));

return $admin;
}

Expand Down
23 changes: 10 additions & 13 deletions src/Security/AdminModule/InvitationsTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function create()
->createQueryBuilder('a')
->andWhere('a.author = :author')->setParameter('author', $this->netteUser->identity->getId());

// columns
$table = $admin->getTable();
$table->setModel(new Doctrine($qb));
$table->setTranslator($this->translator);
Expand All @@ -85,24 +84,22 @@ public function create()
})
->getCellPrototype()->width = '40%';

// actions
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';

$form = $admin->addForm('invitation', 'Invitation', function (Invitation $invitation = null) {
return $this->invitationFormService->getFormFactory($invitation !== null ? $invitation->getId() : null);
});

$admin->connectFormWithAction($form, $table->getAction('edit'));

// Toolbar
$toolbar = $admin->getNavbar();
$toolbar->addSection('new', 'Create', 'file');
$admin->connectFormWithNavbar($form, $toolbar->getSection('new'));
$newSection = $toolbar->addSection('new', 'Create', 'file');

$editAction = $table->addActionEvent('edit', 'Edit');
$editAction->getElementPrototype()->class[] = 'ajax';

$deleteAction = $table->addActionEvent('delete', 'Delete');
$deleteAction->getElementPrototype()->class[] = 'ajax';

$table->addActionEvent('delete', 'Delete')
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));
$admin->connectFormWithNavbar($form, $newSection);
$admin->connectFormWithAction($form, $editAction);
$admin->connectActionAsDelete($deleteAction);

return $admin;
}
Expand Down
39 changes: 18 additions & 21 deletions src/Security/AdminModule/RolesTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ public function create()
{
$admin = $this->adminGridFactory->create($this->roleRepository);

// columns
$table = $admin->getTable();
$table->setTranslator($this->translator);
$table->addColumnText('name', 'Name')
$name = $table->addColumnText('name', 'Name');
$name
->setSortable()
->getCellPrototype()->width = '40%';
$table->getColumn('name')
$name
->setFilterText()->setSuggestion();

$table->addColumnText('parent', 'Parent')
->setSortable()
->getCellPrototype()->width = '60%';
$table->getColumn('parent')
->setCustomRender(function (Role $entity) {
$entities = array();
$en = $entity;
Expand All @@ -74,29 +72,28 @@ public function create()
}

return implode(', ', $entities);
});

// actions
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';

$table->addActionEvent('permissions', 'Permissions')
->getElementPrototype()->class[] = 'ajax';
})
->getCellPrototype()->width = '60%';

$form = $admin->addForm('role', 'Role', function (Role $role = null) {
return $this->roleFormService->getFormFactory($role ? $role->getId() : null);
});

$admin->connectFormWithAction($form, $table->getAction('edit'));

// Toolbar
$toolbar = $admin->getNavbar();
$toolbar->addSection('new', 'Create', 'file');
$admin->connectFormWithNavbar($form, $toolbar->getSection('new'));
$newSection = $toolbar->addSection('new', 'Create', 'file');

$editAction = $table->addActionEvent('edit', 'Edit');
$editAction->getElementPrototype()->class[] = 'ajax';

$permissionAction = $table->addActionEvent('permissions', 'Permissions');
$permissionAction->getElementPrototype()->class[] = 'ajax';

$deleteAction = $table->addActionEvent('delete', 'Delete');
$deleteAction->getElementPrototype()->class[] = 'ajax';

$table->addActionEvent('delete', 'Delete')
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));
$admin->connectFormWithNavbar($form, $newSection);
$admin->connectFormWithAction($form, $editAction);
$admin->connectActionAsDelete($deleteAction);

return $admin;
}
Expand Down
17 changes: 15 additions & 2 deletions src/System/AdminModule/LogsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ protected function createComponentTable()
->setSortable()
->getCellPrototype()->width = '30%';

$table->addActionEvent('show', 'Show')->onClick[] = $this->handleShow;
$table->addActionEvent('delete', 'Delete')->onClick[] = $this->handleDelete;
$event = $table->addActionEvent('show', 'Show');
$event->onClick[] = $this->handleShow;

$event = $table->addActionEvent('delete', 'Delete');
$event->getElementPrototype()->class[] = 'ajax';
$event->onClick[] = $this->handleDelete;
$event->setConfirm(function () {
return 'Really delete?';
});

return $table;
}
Expand Down Expand Up @@ -117,6 +124,9 @@ public function handleDelete($id)
unlink($this->logDir . '/' . $id);
$this->flashMessage($this->translator->translate('Log has been removed.'), 'success');
$this->redirect('this');

unset($this['table']);
$this->redrawControl('content');
}

/**
Expand All @@ -130,6 +140,9 @@ public function handleDeleteAll()

$this->flashMessage($this->translator->translate('Logs were removed.'), 'success');
$this->redirect('this');

unset($this['table']);
$this->redrawControl('content');
}

/**
Expand Down
23 changes: 10 additions & 13 deletions src/System/AdminModule/RegistrationTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public function create()
{
$admin = $this->adminGridFactory->create($this->registrationRepository);

// columns
$table = $admin->getTable();
$table->setTranslator($this->translator);
$table->addColumnText('name', 'Name')
Expand All @@ -62,24 +61,22 @@ public function create()
$table->getColumn('name')
->setFilterText()->setSuggestion();

// actions
$table->addActionEvent('edit', 'Edit')
->getElementPrototype()->class[] = 'ajax';

$form = $admin->addForm('registration', 'Registration', function (Registration $registration = null) {
return $this->registrationFormService->getFormFactory($registration !== null ? $registration->getId() : null);
});

$admin->connectFormWithAction($form, $table->getAction('edit'));

// Toolbar
$toolbar = $admin->getNavbar();
$toolbar->addSection('new', 'Create', 'file');
$admin->connectFormWithNavbar($form, $toolbar->getSection('new'));
$newSection = $toolbar->addSection('new', 'Create', 'file');

$editAction = $table->addActionEvent('edit', 'Edit');
$editAction->getElementPrototype()->class[] = 'ajax';

$deleteAction = $table->addActionEvent('delete', 'Delete');
$deleteAction->getElementPrototype()->class[] = 'ajax';

$table->addActionEvent('delete', 'Delete')
->getElementPrototype()->class[] = 'ajax';
$admin->connectActionAsDelete($table->getAction('delete'));
$admin->connectFormWithNavbar($form, $newSection);
$admin->connectFormWithAction($form, $editAction);
$admin->connectActionAsDelete($deleteAction);

return $admin;
}
Expand Down

0 comments on commit d5b1275

Please sign in to comment.