Building a basic reserved area with ProcessWire

Introduction In this tutorial we will learn how to add a simple reserved area to an existing ProcessWire website, using two handy modules, an external package and a few tricks. In short we will build a protected page on our site, where users of a specific role can log in using their credentials, see a list of documents that the administrator made available to them, and download each one using a secure link. Notice: If you don’t where to start with ProcessWire, I suggest you to look at my 4-part series “How to build a basic webiste with ProcessWire”. The ingredients for this tutorial are the following: An existing ProcessWire 3.0.62 (or higher) installation on PHP >= 5.6.0 The LoginRegister module The FieldTypeSecureFile module The UrlSigner package by Spatie Starting up The FieldTypeSecureFile module module extends ProcessWire by adding a new File field which allows the user to upload files...

Read More
Practical use case for React.js High Order components

Introduction Since the React core team dropped the support for mixins, which were the standard way of sharing code between components, the use of composition over inheritance has become the right way for building user interfaces. A High Order Component (HOC from now on) is a pattern for reusing component logic that has evolved to maturity. It’s used throughout many famous libraries like Redux or React-Router, and it’s a direct consequence of React’s compositional nature. In practical terms a HOC is nothing more that a function that takes an input component and returns an enhanced/modified version of that component. Sometimes they are also called decorators. The problem At work we have a CMS that is also an ecommerce platform. We needed to implement a few statistic widgets for the dashboard, showing for example the number of orders or the number of abandoned carts. The idea was to build isolated standalone...

Read More
Split char-separated values in MySQL

Introduction Recently I needed to extract a list of URLs from a MySQL table. All those URLs were inserted in a single TEXT column, however they were all separated by semicolons. The situation was more or less like the following: SELECT * FROM links --------------------------------------------------------------- | id | urls | --------------------------------------------------------------- | 1 | http://first.link;http://second.link | | 2 | https://third.link | | 3 | | | 4 | http://fourth.link;http://fifth.link;https://sixth.link| | ...| ... | I wanted to get those URLs with a single query, without using any programming language. Luckily for me I could make a few assumptions to make my work a lot easier: The column contained only urls or empty strings There were a maximum of 5 urls for each row Diggin in MySQL does not include a function to split a delimited string. You can define a function yourself for this purpose. However I didn’t have the required...

Read More
Feature Flags in ProcessWire

Introduction While building a website or an application, it’s often useful to control which parts are visibile (and which are not) to the actual users. Maybe you just want to hide a particular functionality on which you are still working, or maybe you just want to reveal things gradually without deploying new code in production. This concept is called Feature flags or Feature toggles. Feature Flag is a technique to turn some functionality of your application off, via configuration, without deploying new code. The concept of feature flags is explained really well in this article by Martin Fowler, which also explains why you shouldn’t do feature flags in the first place: Your first choice should be to break the feature down so you can safely introduce parts of the feature into the product. With this concept in mind let’s switch to my actual use case. Recently I had the need...

Read More
Using ProcessWire InputFieldDateTime via API

Introduction Recently I had the need to create a custom form in the backend, using the API and InputField modules that ProcessWire provides. Think InputFields as the basic building blocks to create forms in ProcessWire. They are used all over the administration panel, and provide styling (via jQuery UI), validation ad error rendering (through the FormBuilder) out of the box. Here is an example of how you can use the API to create a basic form. The code is taken from this PW forum thread written by Soma. It’s pretty straightforward, just follow the comments. <?php // the variable that holds the form output $out = ''; // create a new form field (also field wrapper) $form = $modules->get("InputfieldForm"); $form->action = "./"; $form->method = "POST"; $form->attr("id+name",'subscribe-form'); // the previous function ("id+name") is the same as: // $field->attr('id', 'subscribe-form'); // $field->attr('name', 'subscribe-form'); // create a text input $field = $modules->get("InputfieldText"); $field->label...

Read More
Conditional validation on Laravel's Form Request

Introduction In my opinion, Laravel Form Requests are one of the most useful components of Laravel. They can perform authorization and validation on requests, without even touching the controller. Form Requests are also very flexible, because they are completely customizable by method overriding. When creating a Form Request, the first method to implement is the authorization() method, which can be used to check if the authenticated user can perform the action. For example the following code checks through gates if the user is allowed to update a post. public function authorize() { $post = Post::find($this->route('id')); return $post && $this->user()->can('update', $post); } The code $this->route('id') retrieves the URI parameter named id, defined in our route file as Route::put('post/{id}'). If you don’t need authorization just return true. The second method to define, is of course the one that adds the validation rules and it’s called (as you may probably guess) rules(). public...

Read More