为简化起见,我在pyrocms应用程序中附加了公司和工作人员。在公司中(由管理控制台中的表格生成器自动创建),每个公司的原始文件中都有按钮。 edit
按钮是开箱即用的按钮之一。所以我想添加另一个按钮,说“添加工作人员”,然后打开成功完成的工作人员创建页面。由于一个工人可以在多个公司工作,因此公司与工人之间存在multiple
关系。您可以将其视为任何帖子的类别。我想要的是,在工作人员创建表单页面中,我希望有一个自动单击其工作人员的“工作公司”字段中“添加工作人员”按钮的公司。实施这种想法的正确方法是什么?我可以使用attributes
将HTML属性传递给按钮,但不知道是否有帮助。
这是CompanyTableBuilder中的$ buttons数组。
/**
* The table buttons.
* @var array|string
*/
protected $buttons = [
'edit',
'add_worker' => [
'attributes' => [
'href' => '/admin/crm/workers/create',
],
'text' => 'Add worker',
'type' => 'success',
]
];
答案 0 :(得分:2)
假设您有两组客户:工人和付款人
class ClientTableBuilder extends \Anomaly\UsersModule\User\Table\UserTableBuilder
{
/**
* The table model
*
* @var string
*/
protected $model = UserModel::class;
/**
* The table views.
*
* @var array
*/
protected $views = [
'workers' => [
'query' => WorkersQuery::class,
'buttons' => [
'jobs' => [
'type' => 'info',
],
],
],
'payers' => [
'query' => PayersQuery::class,
'buttons' => [
'payments' => [
'type' => 'info',
],
],
],
];
}
当您按下工作按钮时,您将进入admin/{addon}/clients/jobs/{user_id}
路线。因此,您将需要一个下一个控制器:
class ClientsController extends AdminController
{
/**
* Shows the clients list.
*
* @param ClientTableBuilder $table The table
* @return Response
*/
public function clients(ClientTableBuilder $table)
{
return $table->render();
}
/**
* Shows the client's jobs list.
*
* @param UserRepositoryInterface $users The users
* @param ClientJobsTableBuilder $table The table
* @param $id The identifier
* @return Response
*/
public function assignedJobs(
UserRepositoryInterface $users,
ClientJobsTableBuilder $table,
$id
) {
/* @var UserInterface $user */
if (!$user = $users->find($id)) {
return $this->response->json([
'success' => false,
'message' => "Can't find user with id {$id}!",
]);
}
if ($this->request->ajax()) {
$table->setAjax(true);
}
return $table->setUser($user)->render();
}
/**
* Shows modal with unassigned jobs list
*
* @param UserRepositoryInterface $users The users
* @param ClientJobsLookupTableBuilder $table The table
* @param $id The identifier
* @return Response
*/
public function unassignedJobs(
UserRepositoryInterface $users,
ClientJobsLookupTableBuilder $table,
$id
) {
/* @var UserInterface $user */
if (!$user = $users->find($id)) {
return $this->response->json([
'success' => false,
'message' => "Can't find user with id {$id}!",
]);
}
return $table->setUser($user)->render();
}
/**
* Attach a job to a client
*
* @param int|str $user The user's id
* @param int|str $job The job's id
* @return JsonResponse
*/
public function attach($user, $job)
{
if ($error = $this->dispatch(new AttachJobToUser($user, $job))) {
return $this->response->json([
'success' => false,
'message' => $error,
]);
}
return $this->response->json([
'success' => true,
'user' => (int) $user,
'job' => (int) $job,
]);
}
/**
* Detach a job from a client
*
* @param int|str $user The user's id
* @param int|str $job The job's id
* @return JsonResponse
*/
public function detach($user, $job)
{
if ($error = $this->dispatch(new DetachJobFromUser($user, $job))) {
return $this->response->json([
'success' => false,
'message' => $error,
]);
}
return $this->response->json([
'success' => true,
'user' => (int) $user,
'job' => (int) $job,
]);
}
}
然后值TB如下:
class ClientJobsTableBuilder extends ValueTableBuilder
{
/**
* Table's user
*
* @var UserInterface|null
*/
protected $user = null;
/**
* Table's columns
*
* @var array
*/
protected $columns = [
'name' => [
'heading' => 'Name',
'value' => '<strong>{entry.name}</strong>',
],
'type' => [
'heading' => 'Type',
],
'categories' => [
'heading' => 'Categories',
'value' => 'entry.type.categories.pluck("name")|join("<br>")',
],
];
/**
* Table's buttons
*
* @var string
*/
protected $buttons = ClientJobsTableButtons::class;
/**
* Table's actions
*
* @var array
*/
protected $actions = [];
/**
* Table's options
*
* @var array
*/
protected $options = [
'sortable' => true,
];
/**
* Table's assets
*
* @var array
*/
protected $assets = [
'scripts.js' => [
'{YOUR_MODULE_FULL_NS}::js/detach.js',
],
];
/**
* Gets the user.
*
* @return UserInterface|null The user.
*/
public function getUser()
{
return $this->user;
}
/**
* Sets the user.
*
* @param UserInterface $user The user
* @return self
*/
public function setUser(UserInterface $user)
{
$this->user = $user;
return $this;
}
}
然后是valuetable按钮:
class ClientJobsTableButtons
{
/**
* Handle the table buttons
*
* @param ClientJobsTableBuilder $builder The builder
*/
public function handle(ClientJobsTableBuilder $builder)
{
/* @var UserInterface $user */
if (!$user = $builder->getUser()) {
return;
}
$builder->setButtons([
'detach' => [
'type' => 'danger',
'data-detach' => '{entry.id}',
'data-user' => $user->getId(),
'data-dismiss' => 'multiple',
],
]);
}
}
对于查找TB同样如此:
class ClientJobsLookupTableBuilder extends TableBuilder
{
/**
* AJAX mode flag
*
* @var bool
*/
protected $ajax = true;
/**
* Table's user
*
* @var UserInterface|null
*/
protected $user = null;
/**
* Table's columns
*
* @var array
*/
protected $columns = [
'name' => [
'heading' => 'Name',
'value' => '<strong>{entry.name}</strong>',
],
'type' => [
'heading' => 'Type',
],
'categories' => [
'heading' => 'Categories',
'value' => 'entry.type.categories.pluck("name")|join("<br>")',
],
];
/**
* Table's buttons
*
* @var string
*/
protected $buttons = ClientJobsLookupTableButtons::class;
/**
* Table's actions
*
* @var array
*/
protected $actions = [];
/**
* Table's options
*
* @var array
*/
protected $options = [
'sortable' => false,
];
/**
* Table's assets
*
* @var array
*/
protected $assets = [
'scripts.js' => [
'{YOUR_MODULE_FULL_NS}::js/attach.js',
],
];
/**
* Gets the user.
*
* @return UserInterface|null The user.
*/
public function getUser()
{
return $this->user;
}
/**
* Sets the user.
*
* @param UserInterface $user The user
* @return self
*/
public function setUser(UserInterface $user)
{
$this->user = $user;
return $this;
}
}
和查找TB按钮:
class ClientJobsLookupTableButtons
{
/**
* Handle the table buttons
*
* @param ClientJobsLookupTableBuilder $builder The builder
*/
public function handle(ClientJobsLookupTableBuilder $builder)
{
/* @var UserInterface $user */
if (!$user = $builder->getUser()) {
return;
}
$builder->setButtons([
'attach' => [
'data-attach' => '{entry.id}',
'data-user' => $user->getId(),
'data-dismiss' => 'multiple',
],
]);
}
}
之后,您只需要编写一些JS即可实现正确的行为。
UPD:这是* Query类的示例:
class WorkersQuery
{
/**
* Handle the query.
*
* @param Builder $query The query builder
* @param RoleRepositoryInterface $roles The roles repository
*/
public function handle(Builder $query, RoleRepositoryInterface $roles)
{
/* @var RoleInterface $role */
$role = $roles->findBySlug('worker');
$query
->leftJoin(
'users_users_roles',
'users_users_roles.entry_id',
'=',
'users_users.id'
)
->where('users_users_roles.related_id', $role->getId());
}
}
以及AttachJobToUser命令的示例:
class AttachJobToUser
{
/**
* User's identifier
*
* @var mixed
*/
protected $user;
/**
* Job's identifier
*
* @var mixed
*/
protected $job;
/**
* Create a new instance of AttachJobToUser class
*
* @param $user
* @param $job
*/
public function __construct($user, $job)
{
$this->user = $user;
$this->job = $job;
}
/**
* Handle the command
*
* @param UserRepositoryInterface $users The users
* @param JobRepositoryInterface $jobs The jobs
* @return boolean|string
*/
public function handle(
UserRepositoryInterface $users,
JobRepositoryInterface $jobs
) {
/* @var UserInterface $user */
if (!$user = $users->find($this->user)) {
return "Can't find user with id '{$this->user}'";
}
/* @var JobInterface $job */
if (!$job = $jobs->find($this->job)) {
return "Can't find job with id '{$this->job}'";
}
if (!$job->addWorker($user)) {
return "Can't attach a job with id '{$this->job}' to a worker with id '{$this->user}'";
}
return false;
}
}