Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Declarative Expression Builder in PHP

Folks,

I'm looking to build a piece of PHP5 UI that I'm pretty sure is common to a bunch of applications. Basically, it's an expression builder that allows users to specify expressions combined through logical operators (AND/OR), like this:

  • FieldX > 3 AND FieldY = 5
  • FieldY = "bob" and FieldZ is not null
  • FieldX > '5/23/2007' OR (FieldY = 5 AND FieldY is not null)

Ideally, a system like that would allow me as a programmer to specify the list of parameters that the user can pick (columns) and the data type for each. It would also provide a nice, user-friendly interface for defining expressions like that - I'm imagining something like a table, where each row has several pickers:

[Column] [Condition] [Value] [AND/OR] [Column] [Condition] [Value] [AND/OR] ...

Do you know of an open-source component that does something like that? Or maybe a part of this kind of functionality?

like image 978
Alex Weinstein Avatar asked Jun 09 '09 01:06

Alex Weinstein


2 Answers

A word of caution. These types of generic expression builders are commonly thought of as excellent solutions to a variety of user problems. The thinking tends to go along these lines:

  • The user wants all these different reports
  • There's no way we're giving them SQL access (even if they could understand it)
  • We don't have time to write all these custom reports
  • Therefore, I'll make an easy way for them to write queries without having to know specific syntax, tables and fields.

This is not necessarily a bad idea, but what we have found at my company is that non-techie users have surprising difficulty understanding and building expressions more complex than a group of ANDS or a group of ORS. This is hard for us programmers to grasp, as most of us probably had an intuitive understanding of boolean logic even before we learned to program.

I would recommend that you not try to give them complete flexibility to create whatever expression they want, and instead you put together a UI that lets you the programmer define something more complicated on the back-end but gives the user simple choices on the front-end. This is easier said than done, obviously.

Remember - sometimes the difficulty for end users isn't the fact that they don't know the right syntax to express their idea in. More often it's because they don't even know how to formulate their thoughts in an unambiguous way, even if they were provided an easy way to do so.

NOTE: I'm not saying the end users are always morons - just that their minds may work differently from us crazy developers.

like image 188
Brian Ramsay Avatar answered Nov 02 '22 09:11

Brian Ramsay


This isn't exactly a component, but you might take a look at the Conditions tab of the Shopping Cart rule builder in Magento for a solid PHP implementation of this type of functionality. It is customized towards e-commerce, so it is not a generic database overlay, but it does have a great condition builder interface.

like image 1
Steve Goodman Avatar answered Nov 02 '22 08:11

Steve Goodman