Jump to content

Extending the PW-backend: e.g. CRUD system


dragan
 Share

Recommended Posts

Hello everyone,

I'm a PW newbie. Overwhelmed from all I've seen and tried out the last few days.

My CMS of choice the last couple of years has been MODX. Other than that, I am working as a frontend and backend developer since over 10 years. Having so much freedom to work directly with PHP inside a CMS / CMF is a relief. No new pseudo scripting language one has to learn (as in Typo3), no unnecessary restrictions or bloat.

So, my first general question:

If I have an existing dataset in mySQL, that I would like to manage with PW (i.e. inside the admin area of PW) - how would I most likely do that?

I know I could export the database tables to PW pages. But what if I wanted a database CRUD system inside the PW area? In MODX "Evo" you could build a custom module for such things. You know - phpMyAdmin is over the top for most clients, and an admin section with a different login and URL is clutter.

How can I extend the PW backend? Is it possible to include an off-the shelf CRUD script somewhere? Is that not possible with PW? Or frowned upon? I'm just trying to see what the "best practises" are for these kinds of scenarios.

Thanks for explanations, tips and pointers.

Link to comment
Share on other sites

Dragan,

Welcome to PW!

PW is very flexible....your decisions will not be frowned upon but of course advice will be offered, especially when it comes to security :). So, as for best practices, my experience is that PW is very flexible and in most cases there is not one way of doing anything. I know, it sounds like a cliche...but seriously, you'll notice this very quickly once you start getting your hands dirty. One thing you will notice is that with PW, you will end up writing less code :) It's simply that good. 

Yes; extending PW is in most cases quite easy....

Have a look at this module (WIP). You will get ideas how you can create your CRUD script....It's using DataTables to display PW pages. You can create your own to do something different, e.g. your CRUD stuff using either native SQL or PW's db class. Having worked in the industry that long I don't need to tell you care needs to be exercised if handing a client that much fire power ;). There is also this new kid on the block...a new take at making custom admin pages...

Oh, btw, am ex-MODx as well. There's more in here :)

Enjoy!

  • Like 1
Link to comment
Share on other sites

  • 7 months later...

Hmm ... I was wondering the same, but rather about how to create a CRUD system accessible from the front end (after login) ... much like how you can create custom content types and views in Drupal, and let various user roles (even non-logged in users) add content through the ".../node/add" interface.

Should I start a new thread for this, or could someone elaborate a little on what it would take to get something like this working?

Link to comment
Share on other sites

Since ProcessWire doesn't generate markup for you, you'd have to do this from the API side. Here's a simple example where you have an email subscription form and you accept an email address and store it in the 'title' field of a new page. 

<?php

$showForm = true; 
$email = $sanitizer->email($input->post->email); 

if($email) {

  // parent where we will store our new page
  $parent = $pages->get("/subscriptions/");

  if($parent->child("title=" . $sanitizer->selectorValue($email)) {
    // this checks if we already have this email 
    echo "<p>You are already subscribed!</p>";

  } else {
    // create a new 'subscription' page
    $subscription = new Page();
    $subscription->parent = $parent;
    $subscription->template = 'subscription';
    $subscription->title = $email; 
    $subscription->save();
    $showForm = false;
    echo "<p>Thank you! You have been subscribed.</p>"; 
  }
} 

if($showForm) echo "
  <form action='./' method='post'>
    <label for='email'>Enter your email address</label>
    <input type='email' name='email' id='email' />
    <input type='submit' name='subscribe' value='Subscribe' />
  </form>
  ";
 


There are also some tools that can automate this process to some extend, like FormBuilder, and the FormTemplateProcessor (proof of concept), among others. But the best way to get exactly what you want is to use the API. 

  • Like 2
Link to comment
Share on other sites

  • 3 months later...

Since ProcessWire doesn't generate markup for you, you'd have to do this from the API side. Here's a simple example where you have an email subscription form and you accept an email address and store it in the 'title' field of a new page. 

<?php

$showForm = true; 
$email = $sanitizer->email($input->post->email); 

if($email) {

  // parent where we will store our new page
  $parent = $pages->get("/subscriptions/");

  if($parent->child("title=" . $sanitizer->selectorValue($email)) {
    // this checks if we already have this email 
    echo "<p>You are already subscribed!</p>";

  } else {
    // create a new 'subscription' page
    $subscription = new Page();
    $subscription->parent = $parent;
    $subscription->template = 'subscription';
    $subscription->title = $email; 
    $subscription->save();
    $showForm = false;
    echo "<p>Thank you! You have been subscribed.</p>"; 
  }
} 

if($showForm) echo "
  <form action='./' method='post'>
    <label for='email'>Enter your email address</label>
    <input type='email' name='email' id='email' />
    <input type='submit' name='subscribe' value='Subscribe' />
  </form>
  ";
 

There are also some tools that can automate this process to some extend, like FormBuilder, and the FormTemplateProcessor (proof of concept), among others. But the best way to get exactly what you want is to use the API. 

Thanks for the snippet. Just tried it out for learning purposes but I can´t get it working.

First in the if($parent->child line is a closing bracket missing.

As I´m not really into php it wasn´t to easy to find^^

So the form is showing up but whatever email I try it´s saying "You are already subscribed!"

and I have no idea what to do.

Ah, important to mention that I included head.inc and foot.inc and created the /subscriptions/ page. Is there anything else to do?

Would nice to a have a little hint :-)

Cheers

Can

Link to comment
Share on other sites

I'm a little surprised as this example comes from Ryan. :)

The closing ) is something that happens all the time even to those into programming, but usually an error indicates that there's something missing. 

Well there seems to be a problem with the if($parent->child(....)) { ... }

It will always resolve to true, even if no page with that title found. This is because such a search will return a NullPage object if none found. In a if() this will resolve to true.

You'd have to add a little more to check if a page was found, like test for the id.

if( $parent->child("title=" . $sanitizer->selectorValue($email))->id ) {
   ...
}

Now this works.

  • Like 2
Link to comment
Share on other sites

wow that was fast and sounds logic

will try it in a couple of minutes have to make some shopping first :D

thank you soma!

UPDATE:

took a bit longer, because we just met a guy who will probably sail us to latin america  ^-^

It works like a charm..thank you Soma!!

Edited by Can
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By stanoliver
      Hello Community!
      I found quite a  few ways or tools to style the admin or build a dashboard:
      - the new way with the .less compilation (reno style, rock style)
      - dashboard modules or tutorials how to build your own
      - admin on stereoids
      Vocabulary: With "admin like user" I do not mean a full admin but an user with quite a lot of priviledges ...
      My problem is that I do want to change the admin template for every (admin like) user in a different way. Or if I would build a bigger dashboard certain functionlity should only be available for a few (admin like) users but not for everyone.
      I would need different versions of the admin theme or the built dashboards for different admin users
      To start with a very simple example:
      Let's say I wanna give one (admin like) user a different background color (with css). 
      Let's say I wanna have an extra button or tab for some (admin like) user which another (admin like) user should not have
      How can I take advantage of processwire roles in all these examples I mentioned in the beginning?
       
       
    • By Robin S
      This module lets you add some custom menu items to the main admin menu, and you can set the dropdown links dynamically in a hook if needed.
      Sidenote: the module config uses some repeatable/sortable rows for the child link settings, similar to the ProFields Table interface. The data gets saved as JSON in a hidden textarea field. Might be interesting to other module developers?
      Custom Admin Menus
      Adds up to three custom menu items with optional dropdowns to the main admin menu.
      The menu items can link to admin pages, front-end pages, or pages on external websites.
      The links can be set to open in a new browser tab, and child links in the dropdown can be given an icon.
      Requires ProcessWire v3.0.178 or newer.
      Screenshots
      Example of menu items

      Module config for the menus

      Link list shown when parent menu item is not given a URL

      Advanced
      Setting child menu items dynamically
      If needed you can set the child menu items dynamically using a hook.
      Example:
      $wire->addHookAfter('CustomAdminMenus::getMenuChildren', function(HookEvent $event) { // The menu number is the first argument $menu_number = $event->arguments(0); if($menu_number === 1) { $colours = $event->wire()->pages->findRaw('template=colour', ['title', 'url', 'page_icon']); $children = []; foreach($colours as $colour) { // Each child item should be an array with the following keys $children[] = [ 'icon' => $colour['page_icon'], 'label' => $colour['title'], 'url' => $colour['url'], 'newtab' => false, ]; } $event->return = $children; } }); Create multiple levels of flyout menus
      It's also possible to create multiple levels of flyout submenus using a hook.

      For each level a submenu can be defined in a "children" item. Example:
      $wire->addHookAfter('CustomAdminMenus::getMenuChildren', function(HookEvent $event) { // The menu number is the first argument $menu_number = $event->arguments(0); if($menu_number === 1) { $children = [ [ 'icon' => 'adjust', 'label' => 'One', 'url' => '/one/', 'newtab' => false, ], [ 'icon' => 'anchor', 'label' => 'Two', 'url' => '/two/', 'newtab' => false, 'children' => [ [ 'icon' => 'child', 'label' => 'Red', 'url' => '/red/', 'newtab' => false, ], [ 'icon' => 'bullhorn', 'label' => 'Green', 'url' => '/green/', 'newtab' => false, 'children' => [ [ 'icon' => 'wifi', 'label' => 'Small', 'url' => '/small/', 'newtab' => true, ], [ 'icon' => 'codepen', 'label' => 'Medium', 'url' => '/medium/', 'newtab' => false, ], [ 'icon' => 'cogs', 'label' => 'Large', 'url' => '/large/', 'newtab' => false, ], ] ], [ 'icon' => 'futbol-o', 'label' => 'Blue', 'url' => '/blue/', 'newtab' => true, ], ] ], [ 'icon' => 'hand-o-left', 'label' => 'Three', 'url' => '/three/', 'newtab' => false, ], ]; $event->return = $children; } }); Showing/hiding menus according to user role
      You can determine which menu items can be seen by a role by checking the user's role in the hook.
      For example, if a user has or lacks a role you could include different child menu items in the hook return value. Or if you want to conditionally hide a custom menu altogether you can set the return value to false. Example:
      $wire->addHookAfter('CustomAdminMenus::getMenuChildren', function(HookEvent $event) { // The menu number is the first argument $menu_number = $event->arguments(0); $user = $event->wire()->user; // For custom menu number 1... if($menu_number === 1) { // ...if user does not have some particular role... if(!$user->hasRole('foo')) { // ...do not show the menu $event->return = false; } } });  
      https://github.com/Toutouwai/CustomAdminMenus
      https://processwire.com/modules/custom-admin-menus/
    • By Aswincs
      Here are the step by step to install and setup ProcessWire with the help of a server management tool - https://cloudstick.io/
      1. Create your Vultr compute.

      2. Select the Operating system Ubuntu 16.04/18.04/20.04 LTS >> Enter your server root password then click on Deploy now!

      3. Create an account in CloudStick and connect your server:
       
      Click on connect server >> Enter your server login details >> Add this server.

      4. Your server setup will be done in couple of minutes - The setup will finish less than 8 minutes. Then select your server:

      5. Create an account to host/upload ProcessWire: Click on Accounts >> Create an account >> Create Custom Web application:

      6. Enter the web application details, such as the email address which you would like to receive the SFTP login details >> web application name >> Domain name >> username then >> select the web application stack >> nginx + apache >> then create web application. 

      7. Now select the web application then install SSL: 
      8. Open your email account, and find the login details to connect the server over SFTP >> then upload the source code of ProcessWire:

      9. Let us open the domain in browser once the upload finish.

      10. Select the profile and click next:

      11. Click next to proceed further: Now, you will see an incompatibility issue with PDO-Mysql which can be install in 2 clicks.

       
      12. Go back to the summary page >> Click on easy PHP >> Select the PHP version of your web account:

      13. Then it is time to enable PDO_Mysql, scroll down and enable it:

      14. Go back to to the ProcessWire installation URL and click on check again >> You can see no incompatibility issue after enabling PDO_Mysql:

      15. Click on Next and now it is time to enter the database credentials:

      16. let us create the database, db user and grant privilege's to the db user - it is just matter of few clicks and very easy! 
      Click on the menu Accounts >> Select your web account of ProcessWire >> Click on App Database then create the a database:

      17: Click on create database and enter the database name:

      18. Create the database user:

      19: Go back to the database page and click on Grant user then grant permission:

      20. Go back to the ProcessWire installation URL and enter the database credentials you have created in CloudStick dashboard.
      Now, it is time to setup your admin user credentials and setup admin area URL:

      21. Then you are done:


    • By sambadave
      Hi there
      Short version of question
      Let's say I have a page in the admin that contains a field... Is it possible to output the content from that field on another page in the admin? Almost like a reference.
      Longer version of question (with example)
      A house builder with multiple (60+) developments. They want to be able to create notices/messages that can be added to one or many developments. Handy for things like regional covid lockdowns or temporary office closures due to bad weather.
      My approach for the admin editing options:
      Add each message to each development
      Pros: You edit the message on the development page in context
      Cons: Very time consuming and repetitive if the same message needs to be applied to 60+ developments
        Control all the messages from one admin page and say which development each message should be applied to
      Pros: Easier to add/remove messages to more than one development at a time. Control all messages from one place.
      Cons: Content is not added on development page, which is where typical admin users may expect to find it I went for option 2 due to flexibility, and created a page within the admin for global development notices. This contains a repeater with:
      Field for message to display Checkbox list of all developments. The user can select which ones to apply each message to It's working really well but the only thing is that if the user goes to a specific development in the admin, the relevant messages aren't displayed in context (as they aren't edited on that page and instead on the global development notices page)... which may cause confusion when a new staff member / content admin tries to edit the text but there is no field when they go to the development admin page where they expect to see it...
      Solution???
      I don't require the message(s) to also be editable on the development page, but I wondered if there was a nice way to show it/them somehow. I feel like I am missing something really simple as I'm sure ProcessWire will have a nice way of achieving this, or maybe there are field settings that allow this kind of thing to happen?
      Any ideas on approaches or similar experiences would be much appreciated, even if it is just a much simpler example with the content from one field being shown on another admin page to get the ball rolling.
      Thanks in advance for any advice :)
    • By humanafterall
      Hi,
      I would like to set an admin template to 'https only' as recommended in the Processwire security docs.
      However if I do this it forces this setting locally too, resulting in https://localhost requests which result in an error page.
      Is there a simple way round this? Setting https for templates in the config?
      Thanks!
×
×
  • Create New...