Skip to content

Commit

Permalink
Formulario + CRUD Admin
Browse files Browse the repository at this point in the history
Creamos formulario en el panel Admin de Magento , para registrar contactos nuevos

1.- Creaos Controlador NewAction
	app/code/Pfay/Contacts/Controller/Adminhtml/Test/NewAction.php

2.- Creamos el Layout
	app/code/Pfay/Contacts/view/adminhtml/layout/contacts_test_newaction.xml
	indicando que nuestro diseño se encuentra en contacts_test_edit
3.- cremos nuestro archivo test_edit
	app/code/Pfay/Contacts/view/adminhtml/layout/contacts_test_edit.xml
	Aquí indicamos que cargue un uiComponent llamado pfay_contacts_form

4.- creamos nuestro componente que contendrá nuestro FORMULARIO

5.- creamos el archivo que sera la fuente de nuestros datos
	app/code/Pfay/Contacts/Model/Contact/DataProvider.php
6.- Creamos nuestra clase para nuestros botones
	app/code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/GenericButton.php
	esta clase permite que existan otros botones. Esta es la base que extiende los otros botones.
7.- Creamos nuestro bloque para botón que guarda formulario
	app/code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/BackButton.php
8.- Creamos bloque para botón borrar
	app/code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/DeleteButton.php
9.- Creamos botón resetear formulario
	app/code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/ResetButton.php
10.- Creamos bloque para botón guardar formulario
	app/code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/SaveButton.php
11.- Creamos nuestra accion eliminar
	app/code/Pfay/Contacts/Controller/Adminhtml/Test/Delete.php

11.- actualízamos , compilamos y asignamos permisos
	php bin/magento setup:upgrade
	php bin/magento setup:di:compile
	chmod -R 777 var/ pub/
  • Loading branch information
PerezContrerasLuis committed Jan 21, 2022
1 parent 8a4b633 commit 2d443be
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 1 deletion.
33 changes: 33 additions & 0 deletions code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/BackButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Pfay\Contacts\Block\Adminhtml\Contact\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class BackButton
*/
class BackButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Back'),
'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
'class' => 'back',
'sort_order' => 10
];
}

/**
* Get URL for back (reset) button
*
* @return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/');
}
}
37 changes: 37 additions & 0 deletions code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/DeleteButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Pfay\Contacts\Block\Adminhtml\Contact\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class DeleteButton
*/
class DeleteButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
$data = [];
if ($this->getId()) {
$data = [
'label' => __('Delete Contact'),
'class' => 'delete',
'on_click' => 'deleteConfirm(\''
. __('Are you sure you want to delete this contact ?')
. '\', \'' . $this->getDeleteUrl() . '\')',
'sort_order' => 20,
];
}
return $data;
}

/**
* @return string
*/
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', ['pfay_contacts_id' => $this->getId()]);
}
}
61 changes: 61 additions & 0 deletions code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/GenericButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Pfay\Contacts\Block\Adminhtml\Contact\Edit;

use Magento\Search\Controller\RegistryConstants;

/**
* Class GenericButton
*/
class GenericButton
{
/**
* Url Builder
*
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;

/**
* Registry
*
* @var \Magento\Framework\Registry
*/
protected $registry;

/**
* Constructor
*
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry
) {
$this->urlBuilder = $context->getUrlBuilder();
$this->registry = $registry;
}

/**
* Return the synonyms group Id.
*
* @return int|null
*/
public function getId()
{
$contact = $this->registry->registry('contact');
return $contact ? $contact->getId() : null;
}

/**
* Generate url by route and parameters
*
* @param string $route
* @param array $params
* @return string
*/
public function getUrl($route = '', $params = [])
{
return $this->urlBuilder->getUrl($route, $params);
}
}
23 changes: 23 additions & 0 deletions code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/ResetButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Pfay\Contacts\Block\Adminhtml\Contact\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class ResetButton
*/
class ResetButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Reset'),
'class' => 'reset',
'on_click' => 'location.reload();',
'sort_order' => 30
];
}
}
26 changes: 26 additions & 0 deletions code/Pfay/Contacts/Block/Adminhtml/Contact/Edit/SaveButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Pfay\Contacts\Block\Adminhtml\Contact\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class SaveButton
*/
class SaveButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save Contact'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 90,
];
}
}
29 changes: 29 additions & 0 deletions code/Pfay/Contacts/Controller/Adminhtml/Test/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Pfay\Contacts\Controller\Adminhtml\Test;
use Pfay\Contacts\Model\Contact as Contact;
use Magento\Backend\App\Action;

class Delete extends \Magento\Backend\App\Action
{
public function execute()
{
$id = $this->getRequest()->getParam('id');

if (!($contact = $this->_objectManager->create(Contact::class)->load($id))) {
$this->messageManager->addError(__('Unable to proceed. Please, try again.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/index', array('_current' => true));
}
try{
$contact->delete();
$this->messageManager->addSuccess(__('Your contact has been deleted !'));
} catch (Exception $e) {
$this->messageManager->addError(__('Error while trying to delete contact: '));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/index', array('_current' => true));
}

$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/index', array('_current' => true));
}
}
27 changes: 27 additions & 0 deletions code/Pfay/Contacts/Controller/Adminhtml/Test/NewAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Pfay\Contacts\Controller\Adminhtml\Test;
use Magento\Backend\App\Action;
use Pfay\Contacts\Model\Contact as Contact;

class NewAction extends \Magento\Backend\App\Action
{
/**
* Edit A Contact Page
*
* @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$this->_view->loadLayout();
$this->_view->renderLayout();

$contactDatas = $this->getRequest()->getParam('contact');
if(is_array($contactDatas)) {
$contact = $this->_objectManager->create(Contact::class);
$contact->setData($contactDatas)->save();
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/index');
}
}
}
44 changes: 44 additions & 0 deletions code/Pfay/Contacts/Model/Contact/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Pfay\Contacts\Model\Contact;
use Pfay\Contacts\Model\ResourceModel\Contact\CollectionFactory;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $contactCollectionFactory
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $contactCollectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $contactCollectionFactory->create();
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}

$items = $this->collection->getItems();
$this->loadedData = array();
/** @var Customer $customer */
foreach ($items as $contact) {
// notre fieldset s'apelle "contact" d'ou ce tableau pour que magento puisse retrouver ses datas :
$this->loadedData[$contact->getId()]['contact'] = $contact->getData();
}


return $this->loadedData;

}
}
10 changes: 10 additions & 0 deletions code/Pfay/Contacts/view/adminhtml/layout/contacts_test_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="styles"/>
<update handle="editor"/>
<body>
<referenceContainer name="content">
<uiComponent name="pfay_contacts_form"/>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="contacts_test_edit"/>
<body/>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<item name="name" xsi:type="string">add</item>
<item name="label" xsi:type="string" translate="true">Add a new contact</item>
<item name="class" xsi:type="string">primary</item>
<item name="url" xsi:type="string">*/*/add</item>
<item name="url" xsi:type="string">*/*/newAction</item>
</item>
</item>

Expand Down

0 comments on commit 2d443be

Please sign in to comment.