Page MenuHomeIn-Portal Phabricator

form_submission_helper.php
No OneTemporary

File Metadata

Created
Sun, Feb 1, 5:43 AM

form_submission_helper.php

<?php
/**
* @version $Id: form_submission_helper.php 15698 2013-02-19 11:33:17Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class FormSubmissionHelper extends kHelper {
/**
* Role names for easy usage via FormField tag
*
* @var Array
*/
var $roleNames = Array (
'name' => SubmissionFormField::COMMUNICATION_ROLE_NAME,
'email' => SubmissionFormField::COMMUNICATION_ROLE_EMAIL,
'subject' => SubmissionFormField::COMMUNICATION_ROLE_SUBJECT,
'body' => SubmissionFormField::COMMUNICATION_ROLE_BODY,
);
/**
* Returns submission field based on given role
*
* @param kDBItem $form_submission
* @param string $role
* @param bool $formatted
* @param string $format
* @return string
* @access public
*/
public function getFieldByRole(&$form_submission, $role, $formatted = false, $format = null)
{
$form_id = $form_submission->GetDBField('FormId');
$field_name = $this->getFieldNameByRole($form_id, $role);
if ( $field_name ) {
return $formatted ? $form_submission->GetField($field_name, $format) : $form_submission->GetDBField($field_name);
}
return false;
}
/**
* Returns submission field name based on given role
*
* @param int $form_id
* @param string $role
* @return string
* @access public
*/
public function getFieldNameByRole($form_id, $role)
{
static $cache = Array ();
if ( !array_key_exists($form_id, $cache) ) {
$config = $this->Application->getUnitConfig('formflds');
$sql = 'SELECT ' . $config->getIDField() . ', EmailCommunicationRole
FROM ' . $config->getTableName() . '
WHERE FormId = ' . $form_id . ' AND EmailCommunicationRole <> 0';
$cache[$form_id] = $this->Conn->GetCol($sql, 'EmailCommunicationRole');
}
// convert string representation of role to numeric
if ( !is_numeric($role) ) {
$role = strtolower($role);
$role = array_key_exists($role, $this->roleNames) ? $this->roleNames[$role] : false;
}
// get field name by role
return array_key_exists($role, $cache[$form_id]) ? 'fld_' . $cache[$form_id][$role] : false;
}
/**
* Returns submission field based on given name
*
* @param kDBItem $form_submission
* @param string $name
* @param bool $formatted
* @param string $format
* @return string
* @todo Might not work correctly!
*/
function getFieldByName(&$form_submission, $name, $formatted = false, $format = null)
{
static $cache = Array ();
$form_id = $form_submission->GetDBField('FormId');
if ( !array_key_exists($form_id, $cache) ) {
$config = $this->Application->getUnitConfig('formflds');
$sql = 'SELECT ' . $config->getIDField() . ', FieldName
FROM ' . $config->getTableName() . '
WHERE FormId = ' . $form_id;
$cache[$form_id] = $this->Conn->GetCol($sql, 'FieldName');
}
// get field by name
$field_id = array_key_exists($name, $cache[$form_id]) ? $cache[$form_id][$name] : false;
if ( $field_id ) {
return $formatted ? $form_submission->GetField('fld_' . $field_id, $format) : $form_submission->GetDBField('fld_' . $field_id);
}
return false;
}
/**
* Returns form object field based on form submission
*
* @param $form_submission kDBItem
* @return kDBItem
*/
function &getForm(&$form_submission)
{
$form_id = $form_submission->GetDBField('FormId');
$form = $this->Application->recallObject('form', null, Array ('skip_autoload' => true));
/* @var $form kDBItem */
if ( !$form->isLoaded() || ($form->GetID() != $form_id) ) {
$form->Load($form_id);
}
return $form;
}
}

Event Timeline