Advertisement
Guest User

listener.php

a guest
Dec 11th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.96 KB | None | 0 0
  1. <?php
  2. /*Fix Versuch*/
  3. namespace dmzx\formcreator\event;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class listener implements EventSubscriberInterface
  6.     {
  7.         protected $user;
  8.         protected $template;
  9.         protected $db;
  10.         protected $config;
  11.         protected $auth;
  12.         protected $helper;
  13.         protected $cache;
  14.         protected $request;
  15.         protected $root_path;
  16.         protected $php_ext;
  17.         protected $table_formcreator;
  18.         protected $files_factory;
  19.  
  20.         public function __construct(\phpbb\user $user, \phpbb\template\template $template, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\auth\auth $auth, \phpbb\controller\helper $helper, \phpbb\cache\service $cache, \phpbb\request\request $request, $root_path, $php_ext, $table_formcreator, \phpbb\files\factory $files_factory = null)
  21.         {
  22.             $this->user = $user;
  23.             $this->template = $template;
  24.             $this->db = $db;
  25.             $this->config = $config;
  26.             $this->auth = $auth;
  27.             $this->helper = $helper;
  28.             $this->cache = $cache;
  29.             $this->request = $request;
  30.             $this->root_path = $root_path;
  31.             $this->php_ext = $php_ext;
  32.             $this->table_formcreator = $table_formcreator;
  33.             $this->files_factory = $files_factory;
  34.         }
  35.  
  36.         static public function getSubscribedEvents()
  37.         {
  38.             return array(
  39.                 'core.user_setup' => 'load_language_on_setup',
  40.                 'core.permissions' => 'add_permission',
  41.                 'core.posting_modify_message_text' => 'modify_message_text',
  42.                 'core.posting_modify_template_vars' => 'posting_modify_template_vars',
  43.             );
  44.         }
  45.  
  46.         public function load_language_on_setup($event)
  47.         {
  48.             $lang_set_ext = $event['lang_set_ext'];
  49.             $lang_set_ext[] = array(
  50.                 'ext_name' => 'dmzx/formcreator',
  51.                 'lang_set' => 'common',
  52.             );
  53.             $event['lang_set_ext'] = $lang_set_ext;
  54.         }
  55.  
  56.         public function add_permission($event)
  57.         {
  58.             $permissions = $event['permissions'];
  59.             $permissions['a_form_maker'] = array(
  60.                 'lang' => 'ACL_A_FORM_MAKER',
  61.                 'cat' => 'misc'
  62.             );
  63.             $event['permissions'] = $permissions;
  64.         }
  65.  
  66.         public function modify_message_text($event)
  67.         {
  68.             $post_data = $event['post_data'];
  69.             $mode = $event['mode'];
  70.             $forum_id = $event['forum_id'];
  71.             $submit = $event['submit'];
  72.             $preview = $event['preview'];
  73.             $refresh = $event['refresh'];
  74.             $message_parser = $event['message_parser'];
  75.             $files = $this
  76.                 ->request
  77.                 ->get_super_global(\phpbb\request\request::FILES);
  78.             $message = $this
  79.                 ->request
  80.                 ->variable('message', '', true);
  81.  
  82.             if ($message === '')
  83.             {
  84.                 $message = $this->grab_form_data($forum_id);
  85.                 foreach ($files as $key => $name)
  86.                 {
  87.                     $message_parser->parse_attachments($key, $mode, $forum_id, $submit, $preview, $refresh);
  88.                 }
  89.             }
  90.  
  91.             $message_parser->message = $message;
  92.  
  93.             $post_data = $message_parser->message;
  94.         }
  95.  
  96.         public function posting_modify_template_vars($event)
  97.         {
  98.             $forum_id = $event['forum_id'];
  99.  
  100.             $this->build_form($forum_id);
  101.         }
  102.  
  103.         private function build_form($forum_id)
  104.         {
  105.             global $mode;
  106.  
  107.             $style = 'style="border-radius: 5px;"';
  108.             $style_ta = 'style="border-radius: 5px; max-width: 400px;"';
  109.             $entry = "";
  110.  
  111.             $sql = 'SELECT id, form_id, ndx_order, name, hint, type, mandatory, options, forum_name, forum_id
  112.                 FROM ' . $this->table_formcreator . ' m, ' . FORUMS_TABLE . ' f
  113.                 WHERE m.form_id = ' . (int)$forum_id . '
  114.                     AND m.form_id = f.forum_id
  115.                 ORDER BY ndx_order ASC';
  116.             $result = $this
  117.                 ->db
  118.                 ->sql_query($sql);
  119.  
  120.             while ($row = $this
  121.                 ->db
  122.                 ->sql_fetchrow($result))
  123.             {
  124.                 if ($row['mandatory'])
  125.                 {
  126.                     $mandatory = " required ";
  127.                 }
  128.                 else
  129.                 {
  130.                     $mandatory = "";
  131.                 }
  132.  
  133.                 $temp_name = $row['name'];
  134.  
  135.                 $row['name'] = str_replace(' ', ' ', $row['name']);
  136.  
  137.                 // make things even easier to read //
  138.                 $name = "name='templatefield{$row['name']}'";
  139.                 $id = "id='templatefield{$row['name']}'";
  140.                 $placeholder = "placeholder='{$row['hint']}' ";
  141.                 $tabindex = "tabindex='{$row['ndx_order']}' ";
  142.  
  143.                 $type = "type='{$row['type']}' ";
  144.  
  145.                 $size = 'size="40" ';
  146.                 $maxlength = 'maxlength="255" ';
  147.                 $cols = 'rows="3"';
  148.                 $rows = 'cols="76"';
  149.  
  150.                 switch (strtolower($row['type']))
  151.                 {
  152.                     case 'email':
  153.                     case 'password':
  154.                     case 'url':
  155.                     case 'text':
  156.                     case 'file':
  157.                         $entry = '<input ' . $type . $name . $id . $placeholder . $mandatory . $size . $maxlength . $tabindex . $style . ' />';
  158.                     break;
  159.  
  160.                     case 'textbox':
  161.                         $entry = '<textarea ' . $type . $name . $id . $rows . $cols . $tabindex . $placeholder . $mandatory . $style_ta . '" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);"></textarea>';
  162.                     break;
  163.  
  164.                     case 'selectbox':
  165.                         $entry = '<select ' . $type . $name . $id . $tabindex . $mandatory . $style . '>';
  166.                         $entry .= '<option value="">----------------</option>';
  167.                         $select_option = explode(',', $row['options']);
  168.                         foreach ($select_option as $value)
  169.                         {
  170.                             $entry .= '<option value="' . $value . '">' . $value . '</option>';
  171.                         }
  172.                         $entry .= '</select>';
  173.                     break;
  174.  
  175.                     case 'radiobuttons':
  176.                         $radio_option = explode(',', $row['options']);
  177.                         $entry = '';
  178.                         foreach ($radio_option as $value)
  179.                         {
  180.                             $entry .= '<input type="radio" ' . $tabindex . $mandatory . $name . $id . '" value="' . $value . '"/>&nbsp;' . $value . '&nbsp;&nbsp;';
  181.                         }
  182.                     break;
  183.  
  184.                     case 'checkbox':
  185.                         $check_option = explode(',', $row['options']);
  186.                         $entry = '';
  187.                         foreach ($check_option as $value)
  188.                         {
  189.                             $entry .= '<input ' . $type . $tabindex . $mandatory . ' name="templatefield' . $row['name'] . '[]"' . $id . '" value="' . $value . '" />&nbsp;' . $value . '&nbsp;&nbsp;';
  190.                         }
  191.                     break;
  192.  
  193.                     default:
  194.                     break;
  195.                 }
  196.  
  197.                 $mandatory = '';
  198.  
  199.                 if ($row['mandatory'])
  200.                 {
  201.                     $mandatory = '<span class="mandatory">*</span>';
  202.                 }
  203.  
  204.                 $this
  205.                     ->template
  206.                     ->assign_block_vars('form_apptemplate', array(
  207.                     'NDX_ORDER' => $row['ndx_order'],
  208.                     'NAME' => $row['name'],
  209.                     'LABEL' => $row['name'],
  210.                     'HINT' => $row['hint'],
  211.                     'OPTIONS' => $row['options'],
  212.                     'TYPE' => $entry,
  213.                     'MANDATORY' => $mandatory,
  214.                 ));
  215.  
  216.                 $this
  217.                     ->template
  218.                     ->assign_vars(array(
  219.                     'MODE' => $mode,
  220.                     'S_FORM_MAKER' => true,
  221.                 ));
  222.             }
  223.             $this
  224.                 ->db
  225.                 ->sql_freeresult($result);
  226.         }
  227.  
  228.         private function grab_form_data($forum_id)
  229.         {
  230.             $ret = $appform_post = $last_checked = $temp = '';
  231.             $name_length_max = $file_count = 0;
  232.             $form_data = $names = array();
  233.  
  234.             $this->config['title_colour'] = (isset($this->config['title_colour'])) ? $this->config['title_colour'] : '#FF0000';
  235.  
  236.             $sql = 'SELECT *
  237.                 FROM ' . $this->table_formcreator . '
  238.                 WHERE form_id = ' . (int)$forum_id . '
  239.                 ORDER BY ndx_order ASC';
  240.             $result = $this
  241.                 ->db
  242.                 ->sql_query($sql);
  243.  
  244.             while ($row = $this
  245.                 ->db
  246.                 ->sql_fetchrow($result))
  247.             {
  248.                 $form_data[] = array(
  249.                     'id' => $row['id'],
  250.                     'form_id' => $row['form_id'],
  251.                     'hint' => $row['hint'],
  252.                     'options' => $row['options'],
  253.                     'mandatory' => $row['mandatory'],
  254.                     'name' => $row['name'],
  255.                     'type' => $row['type'],
  256.                     'ndx_order' => $row['ndx_order'],
  257.                 );
  258.  
  259.                 if ($row['type'] == 'file')
  260.                 {
  261.                     $files[] = $row['name'];
  262.                 }
  263.             }
  264.             $this
  265.                 ->db
  266.                 ->sql_freeresult($result);
  267.  
  268.             $fileupload = $this
  269.                 ->files_factory
  270.                 ->get('upload');
  271.  
  272.             if (isset($files))
  273.             {
  274.                 foreach ($files as $name)
  275.                 {
  276.                     $temp = $fileupload->handle_upload('files.types.form', 'upload');
  277.  
  278.                     if ($temp->get('realname'))
  279.                     {
  280.                         $names[] = $temp->get('realname');
  281.                     }
  282.                 }
  283.             }
  284.  
  285.             foreach ($form_data as $row)
  286.             {
  287.                 $name = "";
  288.                 $row['name'] = str_replace(' ', ' ', $row['name']);
  289.                 $name = "templatefield" . $row['name'];
  290.  
  291.                 if (isset($row['type']))
  292.                 {
  293.                     switch ($row['type'])
  294.                     {
  295.                         case 'checkbox':
  296.  
  297.                             $check_box_items = count($this
  298.                                 ->request
  299.                                 ->variable('templatefield' . $row['name'], array(
  300.                                 0 => 0
  301.                             )));
  302.                             $check_box_count = 0;
  303.                             $appform_post .= '[form][b]' . $row['name'] . ':[/b][/form][fbox]';
  304.  
  305.                             $checkbox = $this
  306.                                 ->request
  307.                                 ->variable($name, array(
  308.                                 0 => ''
  309.                             ) , true);
  310.  
  311.                             foreach ($checkbox as $value)
  312.                             {
  313.                                 $appform_post .= $value;
  314.  
  315.                                 if ($check_box_count < $check_box_items - 1)
  316.                                 {
  317.                                     $appform_post .= ', ';
  318.                                 }
  319.                                 $check_box_count++;
  320.                             }
  321.                             $appform_post .= '[/fbox]';
  322.  
  323.                         break;
  324.  
  325.                         case 'email':
  326.                         case 'password':
  327.                         case 'url':
  328.                         case 'text':
  329.                         case 'selectbox':
  330.                         case 'radiobuttons':
  331.                             $fieldcontents = $this
  332.                                 ->request
  333.                                 ->variable($name, ' ', true);
  334.                             // only process if element has valid data //
  335.                             if ($fieldcontents)
  336.                             {
  337.                                 $appform_post .= '[form][b]' . $row['name'] . ':[/b][/form][fbox]';
  338.                                 $appform_post .= $fieldcontents .= '[/fbox]';
  339.                             }
  340.                         break;
  341.  
  342.                         case 'file':
  343.                             $fieldcontents = $this
  344.                                 ->request
  345.                                 ->variable($name, ' ', true);
  346.                             // only process if element has valid data //
  347.                             if ($fieldcontents)
  348.                             {
  349.                                 $appform_post .= '[b]' . $row['name'] . ':[/b][att]';
  350.                                 $fieldcontents = '[attachment=' . $file_count . ']' . $names[$file_count] . '[/attachment]';
  351.                                 $file_count++;
  352.                                 $appform_post .= $fieldcontents .= '[/att]';
  353.                             }
  354.                         break;
  355.  
  356.                         case 'textbox':
  357.                             $fieldcontents = $this
  358.                                 ->request
  359.                                 ->variable($name, ' ', true);
  360.                             // only process if element has valid data //
  361.                             if ($fieldcontents)
  362.                             {
  363.                                 $appform_post .= '[form][b]' . $row['name'] . ':[/b][/form][fbox]';
  364.                                 $appform_post .= $fieldcontents .= '[/fbox]';
  365.                             }
  366.                         break;
  367.  
  368.                         default:
  369.                         break;
  370.                     }
  371.                 }
  372.             }
  373.             if ($fieldcontents == '')
  374.             {
  375.                 return;
  376.             }
  377.             else
  378.             {
  379.                 return $appform_post;
  380.             }
  381.         }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement