/**
  * Process rule data to be added or edited
  *
  * @param object $entity The rule entity object
  * @param array $data The form data to be processed
  * @return null
  * @access protected
  */
 protected function add_edit_rule_data($entity, $data)
 {
     // Get form's POST actions (submit or preview)
     $submit = $this->request->is_set_post('submit');
     $preview = $this->request->is_set_post('preview');
     // Load posting language file for the BBCode editor
     $this->user->add_lang('posting');
     // Create an array to collect errors that will be output to the user
     $errors = array();
     // Grab the form data's message parsing options (possible values: 1 or 0)
     $message_parse_options = array('bbcode' => $submit || $preview ? $data['bbcode'] : $entity->message_bbcode_enabled(), 'magic_url' => $submit || $preview ? $data['magic_url'] : $entity->message_magic_url_enabled(), 'smilies' => $submit || $preview ? $data['smilies'] : $entity->message_smilies_enabled());
     // Set the message parse options in the entity
     foreach ($message_parse_options as $function => $enabled) {
         call_user_func(array($entity, ($enabled ? 'message_enable_' : 'message_disable_') . $function));
     }
     unset($message_parse_options);
     // Grab the form's rule data fields
     $rule_fields = array('title' => $data['rule_title'], 'anchor' => $data['rule_anchor'], 'message' => $data['rule_message']);
     // Set the rule's data in the entity
     foreach ($rule_fields as $entity_function => $rule_data) {
         try {
             // Calling the set_$entity_function on the entity and passing it $rule_data
             call_user_func_array(array($entity, 'set_' . $entity_function), array($rule_data));
         } catch (\phpbb\boardrules\exception\base $e) {
             // Catch exceptions and add them to errors array
             $errors[] = $e->get_message($this->user);
         }
     }
     unset($rule_fields);
     // If the form has been submitted or previewed
     if ($submit || $preview) {
         // Test if the form is valid
         if (!check_form_key('add_edit_rule')) {
             $errors[] = $this->user->lang('FORM_INVALID');
         }
         // Do not allow an empty rule title
         if ($entity->get_title() == '') {
             $errors[] = $this->user->lang('ACP_RULE_TITLE_EMPTY');
         }
     }
     // Preview
     if ($preview && empty($errors)) {
         // Set output vars for display in the template
         $this->template->assign_vars(array('S_PREVIEW' => $preview, 'RULE_TITLE_PREVIEW' => $entity->get_title(), 'RULE_MESSAGE_PREVIEW' => $entity->get_message_for_display()));
     }
     // Insert or update rule
     if ($submit && empty($errors) && !$preview) {
         if ($entity->get_id()) {
             // Save the edited rule entity to the database
             $entity->save();
             // Change rule parent
             if (isset($data['rule_parent_id']) && $data['rule_parent_id'] != $entity->get_parent_id()) {
                 $this->rule_operator->change_parent($entity->get_id(), $data['rule_parent_id']);
             }
             // Show user confirmation of the saved rule and provide link back to the previous page
             trigger_error($this->user->lang('ACP_RULE_EDITED') . adm_back_link("{$this->u_action}&language={$entity->get_language()}&parent_id={$entity->get_parent_id()}"));
         } else {
             // Add a new rule entity to the database
             $this->rule_operator->add_rule($entity, $data['rule_language'], $data['rule_parent_id']);
             // Show user confirmation of the added rule and provide link back to the previous page
             trigger_error($this->user->lang('ACP_RULE_ADDED') . adm_back_link("{$this->u_action}&language={$data['rule_language']}&parent_id={$data['rule_parent_id']}"));
         }
     }
     // Set output vars for display in the template
     $this->template->assign_vars(array('S_ERROR' => sizeof($errors) ? true : false, 'ERROR_MSG' => sizeof($errors) ? implode('<br />', $errors) : '', 'RULE_TITLE' => $entity->get_title(), 'RULE_ANCHOR' => $entity->get_anchor(), 'RULE_MESSAGE' => $entity->get_message_for_edit(), 'S_BBCODE_DISABLE_CHECKED' => !$entity->message_bbcode_enabled(), 'S_SMILIES_DISABLE_CHECKED' => !$entity->message_smilies_enabled(), 'S_MAGIC_URL_DISABLE_CHECKED' => !$entity->message_magic_url_enabled(), 'BBCODE_STATUS' => $this->user->lang('BBCODE_IS_ON', '<a href="' . append_sid("{$this->root_path}faq.{$this->php_ext}", 'mode=bbcode') . '">', '</a>'), 'SMILIES_STATUS' => $this->user->lang('SMILIES_ARE_ON'), 'IMG_STATUS' => $this->user->lang('IMAGES_ARE_ON'), 'FLASH_STATUS' => $this->user->lang('FLASH_IS_ON'), 'URL_STATUS' => $this->user->lang('URL_IS_ON'), 'S_BBCODE_ALLOWED' => true, 'S_SMILIES_ALLOWED' => true, 'S_BBCODE_IMG' => true, 'S_BBCODE_FLASH' => true, 'S_LINKS_ALLOWED' => true));
     // Build custom bbcodes array
     include_once $this->root_path . 'includes/functions_display.' . $this->php_ext;
     display_custom_bbcodes();
 }