Application->isAdmin) { if ($event->Name == 'OnCreate') { // anybody can submit forms on front return true; } } $section = $event->getSection(); $form_id = $this->Application->GetVar('form_id'); $event->setEventParam('PermSection', $section . ':' . $form_id); return parent::CheckPermission($event); } /** * Always allow to view feedback form * */ function mapPermissions() { parent::mapPermissions(); $permissions = Array( 'OnItemBuild' => Array ('self' => true), 'OnEdit' => Array ('self' => 'view', 'subitem' => 'view'), ); $this->permMapping = array_merge($this->permMapping, $permissions); } /** * Returns filter block based on field element type * * @param string $element_type * @return string */ function _getFilterBlock($element_type) { $mapping = Array ( 'text' => 'grid_like_filter', 'select' => 'grid_options_filter', 'radio' => 'grid_options_filter', 'checkbox' => 'grid_options_filter', 'password' => 'grid_like_filter', 'textarea' => 'grid_like_filter', 'label' => 'grid_like_filter', ); return $mapping[$element_type]; } function OnBuildFormFields(&$event) { $form_id = $this->Application->GetVar('form_id'); if (!$form_id) return ; $conf_fields = $this->Application->getUnitOption($event->Prefix, 'Fields'); $conf_grids = $this->Application->getUnitOption($event->Prefix, 'Grids'); $helper =& $this->Application->recallObject('InpCustomFieldsHelper'); /* @var $helper InpCustomFieldsHelper */ $sql = 'SELECT * FROM ' . TABLE_PREFIX . 'FormFields WHERE FormId = ' . (int)$form_id . ' ORDER BY Priority DESC'; $fields = $this->Conn->Query($sql, 'FormFieldId'); $use_options = Array ('radio', 'select', 'checkbox'); $check_visibility = $this->Application->LoggedIn() && !$this->Application->isAdminUser; foreach ($fields as $field_id => $options) { $field_visible = $check_visibility ? $options['Visibility'] == FORM_FIELD_EVERYONE : true; $field_options = Array('type' => 'string', 'default' => $options['DefaultValue']); if ($options['Required'] && $field_visible) { $field_options['required'] = 1; } if ($options['Validation'] == 1) { $field_options['formatter'] = 'kFormatter'; $field_options['regexp'] = '/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i'; } if ($options['DisplayInGrid']) { $title = $options['Prompt']; if (substr($title, 0, 1) == '+') { $this->Application->Phrases->AddCachedPhrase('form_col_title' . $field_id, substr($title, 1)); $title = 'form_col_title' . $field_id; } $conf_grids['Default']['Fields']['fld_' . $field_id] = Array ( 'title' => $title, 'no_special' => 1, 'nl2br' => 1, 'first_chars' => 200, 'filter_block' => $this->_getFilterBlock($options['ElementType']) ); if ($options['Validation'] == 1) { $conf_grids['Default']['Fields']['fld_' . $field_id]['data_block'] = 'grid_email_td'; } } if ($options['ElementType'] == 'checkbox' && !$options['ValueList']) { // fix case, when user haven't defined any options for checkbox $options['ValueList'] = '1=la_Yes||0=la_No'; } if (in_array($options['ElementType'], $use_options) && $options['ValueList']) { // field type can have options and user have defined them too $field_options['options'] = $helper->GetValuesHash( $options['ValueList'] ); $field_options['formatter'] = 'kOptionsFormatter'; } if ($options['ElementType'] == 'password') { $field_options['formatter'] = 'kPasswordFormatter'; $field_options['encryption_method'] = 'plain'; $field_options['verify_field'] = 'fld_' . $field_id . '_verify'; } $conf_fields['fld_' . $field_id] = $field_options; } $this->Application->setUnitOption($event->Prefix, 'Fields', $conf_fields); $this->Application->setUnitOption($event->Prefix, 'Grids', $conf_grids); } function SetCustomQuery(&$event) { $object =& $event->getObject(); $form_id = $this->Application->GetVar('form_id'); $object->addFilter('form_filter','%1$s.FormId = '.$form_id); } /** * Allows user to see it's last feedback form data * * @param kEvent $event * @return int */ function getPassedID(&$event) { if ($event->Special == 'last') { // allow user to see his last submitted form return $this->Application->RecallVar('last_submission_id'); } if ($this->Application->isAdminUser) { // don't check ids in admin return parent::getPassedID($event); } // no way to see other user's form submission by giving it's ID directly in url return 0; } /** * Creates new form submission from Front-End * * @param kEvent $event */ function OnCreate(&$event) { parent::OnCreate($event); if ($event->status != erSUCCESS) { return ; } $object =& $event->getObject(); /* @var $object kDBItem */ // allows user to view only it's last submission $this->Application->StoreVar('last_submission_id', $object->GetID()); $this->Application->EmailEventAdmin('FORM.SUBMITTED'); // $this->Application->EmailEventUser('FORM.SUBMITTED', null, 'to_email' => ''); $event->SetRedirectParam('opener', 's'); $event->SetRedirectParam('m_cat_id', 0); $theme =& $this->Application->recallObject('theme.current'); /* @var $theme kDBItem */ $template = $this->Application->GetVar('success_template'); $alias_template = $theme->GetField('TemplateAliases', $template); $event->redirect = $alias_template ? $alias_template : $template; } /** * Processes Captcha code * * @param kEvent $event */ function OnBeforeItemCreate(&$event) { parent::OnBeforeItemCreate($event); $object =& $event->getObject(); /* @var $object kDBItem */ $object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']); $object->SetDBField('ReferrerURL', $_SERVER['HTTP_REFERER']); $form_submission_helper =& $this->Application->recallObject('FormSubmissionHelper'); /* @var $form_submission_helper FormSubmissionHelper */ $form =& $form_submission_helper->getForm($object); // validate captcha code if ($form->GetDBField('UseSecurityImage') && !$this->Application->LoggedIn()) { $captcha_helper =& $this->Application->recallObject('CaptchaHelper'); /* @var $captcha_helper kCaptchaHelper */ $captcha_helper->validateCode($event, false); } } /** * Passes form_id, when using "Prev"/"Next" toolbar buttons * * @param kEvent $event */ function OnPreSaveAndGo(&$event) { parent::OnPreSaveAndGo($event); if ($event->status == erSUCCESS) { $event->SetRedirectParam('pass', 'm,form,formsubs'); } } /** * Saves edited item in temp table and goes * to passed tabs, by redirecting to it with OnPreSave event * * @param kEvent $event */ function OnPreSaveAndGoToTab(&$event) { parent::OnPreSaveAndGoToTab($event); if ($event->status == erSUCCESS) { $event->SetRedirectParam('pass', 'm,form,formsubs'); } } }