/**
  * The event which runs before storing (saving) data to the database
  *
  * @param   boolean  $updateNulls  Should nulls be saved as nulls (true) or just skipped over (false)?
  *
  * @return  boolean  True to allow saving
  */
 protected function onBeforeStore($updateNulls)
 {
     // Do we have a "Created" set of fields?
     $created_on = $this->getColumnAlias('created_on');
     $created_by = $this->getColumnAlias('created_by');
     $modified_on = $this->getColumnAlias('modified_on');
     $modified_by = $this->getColumnAlias('modified_by');
     $locked_on = $this->getColumnAlias('locked_on');
     $locked_by = $this->getColumnAlias('locked_by');
     $title = $this->getColumnAlias('title');
     $slug = $this->getColumnAlias('slug');
     $hasCreatedOn = in_array($created_on, $this->getKnownFields());
     $hasCreatedBy = in_array($created_by, $this->getKnownFields());
     if ($hasCreatedOn && $hasCreatedBy) {
         $hasModifiedOn = in_array($modified_on, $this->getKnownFields());
         $hasModifiedBy = in_array($modified_by, $this->getKnownFields());
         $nullDate = $this->_db->getNullDate();
         if (empty($this->{$created_by}) || $this->{$created_on} == $nullDate || empty($this->{$created_on})) {
             $uid = FOFPlatform::getInstance()->getUser()->id;
             if ($uid) {
                 $this->{$created_by} = FOFPlatform::getInstance()->getUser()->id;
             }
             $date = FOFPlatform::getInstance()->getDate('now', null, false);
             $this->{$created_on} = method_exists($date, 'toSql') ? $date->toSql() : $date->toMySQL();
         } elseif ($hasModifiedOn && $hasModifiedBy) {
             $uid = FOFPlatform::getInstance()->getUser()->id;
             if ($uid) {
                 $this->{$modified_by} = FOFPlatform::getInstance()->getUser()->id;
             }
             $date = FOFPlatform::getInstance()->getDate('now', null, false);
             $this->{$modified_on} = method_exists($date, 'toSql') ? $date->toSql() : $date->toMySQL();
         }
     }
     // Do we have a set of title and slug fields?
     $hasTitle = in_array($title, $this->getKnownFields());
     $hasSlug = in_array($slug, $this->getKnownFields());
     if ($hasTitle && $hasSlug) {
         if (empty($this->{$slug})) {
             // Create a slug from the title
             $this->{$slug} = FOFStringUtils::toSlug($this->{$title});
         } else {
             // Filter the slug for invalid characters
             $this->{$slug} = FOFStringUtils::toSlug($this->{$slug});
         }
         // Make sure we don't have a duplicate slug on this table
         $db = $this->getDbo();
         $query = $db->getQuery(true)->select($db->qn($slug))->from($this->_tbl)->where($db->qn($slug) . ' = ' . $db->q($this->{$slug}))->where('NOT ' . $db->qn($this->_tbl_key) . ' = ' . $db->q($this->{$this->_tbl_key}));
         $db->setQuery($query);
         $existingItems = $db->loadAssocList();
         $count = 0;
         $newSlug = $this->{$slug};
         while (!empty($existingItems)) {
             $count++;
             $newSlug = $this->{$slug} . '-' . $count;
             $query = $db->getQuery(true)->select($db->qn($slug))->from($this->_tbl)->where($db->qn($slug) . ' = ' . $db->q($newSlug))->where('NOT ' . $db->qn($this->_tbl_key) . ' = ' . $db->q($this->{$this->_tbl_key}));
             $db->setQuery($query);
             $existingItems = $db->loadAssocList();
         }
         $this->{$slug} = $newSlug;
     }
     // Call the behaviors
     $result = $this->tableDispatcher->trigger('onBeforeStore', array(&$this, $updateNulls));
     if (in_array(false, $result, true)) {
         // Behavior failed, return false
         return false;
     }
     // Execute onBeforeStore<tablename> events in loaded plugins
     if ($this->_trigger_events) {
         $name = FOFInflector::pluralize($this->getKeyName());
         $result = FOFPlatform::getInstance()->runPlugins('onBeforeStore' . ucfirst($name), array(&$this, $updateNulls));
         if (in_array(false, $result, true)) {
             return false;
         } else {
             return true;
         }
     }
     return true;
 }