Book Image

concrete5 Cookbook

Book Image

concrete5 Cookbook

Overview of this book

concrete5 is an increasingly popular open source content management system because of its incredible and easy-to-use interface. But, it also comes complete with a robust PHP framework, allowing web developers to create custom websites and applications with ease. "concrete5 Cookbook" is a practical collection of solutions to tasks that both novice and experienced concrete5 developers face on a regular basis. Readers will learn multiple subjects, including full blueprints for developing an event calendar add-on and an image gallery block. Developers new to concrete5 will quickly learn how to customize concrete5 to their needs, and seasoned pros will find it an excellent quick reference for performing specific tasks. "concrete5 Cookbook" will transform ordinary PHP developers into concrete5 experts capable of bending concrete5 to their will and unleashing the true power of this up-and-coming content management system. Throughout the course of over 140 recipes and 3 bonus project blueprint chapters, PHP developers will learn how to create custom blocks and dashboard interfaces as well as programmatically work with pages, files, users, permissions, and more. Discover the built-in Active Record support that makes working with databases simple and maintainable. Readers will also learn how to take advantage of the numerous helper classes included in concrete5, and will dive deep into the concrete5 MVC framework to create powerful custom websites and applications. Tie together all of the concepts learned in the recipes with 3 bonus chapters featuring complete blueprints to create a calendar add-on, an image gallery block type, and tips on how to sell your themes and add-ons for money! "concrete5 Cookbook" is a complete collection of recipes to solve the most common (and some not-so-common) tasks that concrete5 developers will face on a regular basis.
Table of Contents (19 chapters)
concrete5 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the add form single page


Open /packages/cookbook_events/controllers/dashboard/cookbook_events/add.php in your code editor. Let's add a few functions to this class:

public function edit($id = null) {
   if ($id) {
      // in edit mode, load the event to be edited
       Loader::model('cookbook_event', 'cookbook_events');
      $event = new CookbookEvent();
      $event->load('id = ?', $id);
      // pass the event object to the view as an array
      $this->set('data', (array) $event);
   }
}

public function save() {
   $data = $_POST;
   // verify that all required fields have been filled out
   $val = Loader::helper('validation/form');
   $val->setData($data);
   $val->addRequired('title', t('Please enter a title.'));
   $passed = $val->test();
   if (!$passed) {
      $this->set('errors', $val->getError()->getList());
   }
   else {
      $dth = Loader::helper('form/date_time');
      Loader::model('cookbook_event', 'cookbook_events');
      $event = new CookbookEvent();
      if ($data['id']) {
         $event->load('id = ?', $data['id']);
      }
      $event->title = $data['title'];
      $event->event_date = $dth->translate('event_date');
      if ($data['location']) {
         $event->location = $data['location'];
      }
      if ($data['description']) {
         $event->description = $data['description'];
      }
      if (!$data['id']) {
         $user = new User();
         $event->created_by = $user->getUserID();
         $event->created_at = date('Y-m-d H:i:s');
      }
      $event->save();
      
      $this->redirect('/dashboard/cookbook_events/list?success');
   }
}

First, we added a function to provide editing capabilities to this form. Essentially, if an ID is provided in the URL, we will use the event model to load the corresponding event and send it to the view as an array. We use an array because if the array is empty, the view will not produce errors as it would with an object.

Next, we define the function to actually save the new event. We use the Validation helper to verify that the required fields have been filled out. After that, we begin populating the event object to be saved to the database. One important thing to notice here is that we check for the presence of an event ID in the POST data. This is to allow us to edit an existing event, rather than create a duplicate one. We also use this area to set the metadata for the event, such as the ID of the user that added it, and the timestamp of when it was created.

Finally, we save the event object to the database and redirect the user back to the event list, displaying a success message.

The form view file

Of course, we still need to write the other half of this single page, the view. Let's open /packages/cookbook_events/single_pages/dashboard/cookbook_events/add.php and add the following HTML and PHP code to it:

<?php 
   defined('C5_EXECUTE') or die(_("Access Denied."));
   $form = Loader::helper('form');
   $dth = Loader::helper('form/date_time');
   
   Loader::element('editor_init');
   Loader::element('editor_config');
?>
<div class="ccm-ui">
   <?php if (!empty($errors)): ?>
      <div class="alert-message block-message error">
         <strong><?php echo t('There were some problems saving the event.') ?></strong>
         <ul style="margin-top: 5px;">
            <?php foreach ($errors as $e): ?>
               <li><?php echo $e ?></li>
            <?php endforeach ?>
         </ul>
      </div>
   <?php endif; ?>
   <div class="ccm-pane">
      <?php
         $dashboard = Loader::helper('concrete/dashboard');
         echo $dashboard->getDashboardPaneHeader(t('Add Event'));
      ?>
      <form action=<?php echo $this->action('save') ?> method="POST">
         <div class="ccm-pane-body">
            <table class="table table-striped table-bordered">
               <tr>
                  <td class="form-label">
                     <?php echo t('Event Title') ?>
                     <span class="req">*</span>
                  </td>
                  <td>
                     <?php echo $form->text('title', $data['title']) ?>
                  </td>
               </tr>
               <tr>
                  <td class="form-label">
                     <?php echo t('Event Date') ?>
                     <span class="req">*</span>
                  </td>
                  <td>
                     <?php echo $dth->datetime('event_date', $data['event_date']) ?>
                  </td>
               </tr>
               <tr>
                  <td class="form-label">
                     <?php echo t('Location') ?>
                  </td>
                  <td>
                     <?php echo $form->text('location', $data['location']) ?>
                  </td>
               </tr>
               <tr>
                  <td class="form-label">
                     <?php echo t('Description') ?>
                  </td>
                  <td>
                     <?php
                        Loader::element('editor_controls');
                        echo $form->textarea('description', $data['description'], array('style' => 'width:100%;', 'class' => 'ccm-advanced-editor'));
                     ?>
                  </td>
               </tr>
            </table>
         </div>
         <div class="ccm-pane-footer">
            <div class="pull-right">
               <input type="submit" class="btn primary" value="<?php echo t('Save Event') ?>">
               <a href="<?php echo $this->url('/dashboard/cookbook_events/list') ?>" class="btn"><?php echo t('Cancel') ?></a>
            </div>
         </div>
         <?php if (!empty($data)): ?>
            <input type="hidden" name="id" value="<?php echo $data['id'] ?>">
         <?php endif; ?>
      </form>
   </div>
</div>

Whoa, that's a lot of code! Don't worry, it's not as complex as it looks. At the top of the file, we of course add our defined or die statement, and then load two helpers, the Form helper and the Date field helper.

As our event's description field can contain HTML formatting, we will need to use a WYSIWYG editor. That editor needs some special JavaScript on the page before it can load, so we output that right away using the editor_init and editor_config elements.

Next, we begin our output by displaying any errors that occurred in the save procedure (for example, if the title field was left empty). After that, we use concrete5's Dashboard helper to output the header of the page, including the native page controls.

After that, we will define the form, setting the action of the form to use the save function in this page's controller and setting the HTTP method to POST.

Inside the form, we use a table to give our form a simple layout. We output fields for title, location, date, and description. At the bottom of the page, we display some buttons to save the data or to cancel and return to the events listing. Let's take a look at this form in our browser now!

That looks great! Let's add a bunch of events to the site now, using this form.