/**
  * onBeforeSave method. Hook for chidlren model to prepare the data.
  *
  * @param   array  $data     The data to be saved.
  * @param   JTable  $table   The table object.
  *
  * @return boolean
  */
 protected function onBeforeSave(&$data, $table)
 {
     // User
     $user = JFactory::getUser();
     // Create action
     if (!$table->id) {
         // Detect the context
         $context = isset($data['parent_id']) && $data['parent_id'] ? 'com_k2.category.' . $data['parent_id'] : 'com_k2';
         // If the user has not the permission to create category stop the processs. Otherwise handle the category state
         if (!$user->authorise('k2.category.create', $context)) {
             $this->setError(JText::_('K2_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_THIS_OPERATION'));
             return false;
         } else {
             // User can create the category but cannot edit it's state so we set the category state to 0
             if (!$user->authorise('k2.category.edit.state', $context)) {
                 $data['state'] = 0;
             }
         }
     }
     // Edit action
     if ($table->id) {
         // Detect the context
         $context = 'com_k2.category.' . $table->id;
         // Actions
         $canEdit = $user->authorise('k2.category.edit', $context) || $user->authorise('k2.item.edit.own', $context) && $user->id == $table->created_by;
         $canEditState = $user->authorise('k2.item.edit.state', $context);
         // User cannot edit the category neither it's state. Stop the process
         if (!$canEdit && !$canEditState) {
             $this->setError(JText::_('K2_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_THIS_OPERATION'));
             return false;
         } else {
             // Store the input states values in case we need them after
             $state = isset($data['state']) ? $data['state'] : $table->state;
             // User cannot edit the item. Reset the input
             if (!$canEdit) {
                 $data = array();
                 $data['id'] = $table->id;
             }
             // Set the states values depending on permissions
             $data['state'] = $canEditState ? $state : $table->state;
         }
     }
     // Get timezone
     $configuration = JFactory::getConfig();
     $userTimeZone = $user->getParam('timezone', $configuration->get('offset'));
     // Handle date data
     if ($data['id'] && isset($data['createdDate'])) {
         // Convert date to UTC
         $createdDateTime = $data['createdDate'] . ' ' . $data['createdTime'];
         $data['created'] = JFactory::getDate($createdDateTime, $userTimeZone)->toSql();
     }
     // Update category location
     if (isset($data['parent_id']) && $table->parent_id != $data['parent_id'] || !$data['id']) {
         $table->setLocation($data['parent_id'], 'last-child');
     }
     if ($this->getState('patch') && isset($data['parent_id'])) {
         $table->setLocation($data['parent_id'], 'first-child');
     }
     // Image
     if (isset($data['image'])) {
         // Detect if category has an image
         $data['image']['flag'] = (int) (!$data['image']['remove'] && ($data['image']['id'] || $data['image']['temp']));
         // Store the input of the image to state
         $this->setState('image', $data['image']);
         // Unset values we do not want to get stored to our database
         unset($data['image']['path']);
         unset($data['image']['id']);
         unset($data['image']['temp']);
         unset($data['image']['remove']);
         // Encode the value to JSON
         $data['image'] = json_encode($data['image']);
     }
     // Extra fields
     if (isset($data['extra_fields'])) {
         $data['extra_fields'] = json_encode($data['extra_fields']);
     }
     // Add flag for moving a category to trash
     if (isset($data['state']) && $data['state'] == -1 && $table->state != -1) {
         $this->setState('trash', true);
     }
     return true;
 }