self::TYPE_NEW_TEAM
);
protected $_teamFormat = '%s';
protected $_teamFormatLimited = '%s
%d of %d spots available';
protected $_teamFormatNoSpots = '%s
No more spots available';
protected $_teams;
protected function _getSubData($field = null, $default = null)
{
$prefix = "team";
$field = (null === $field) ? $prefix : $prefix . "[{$field}]";
return $this->_getData($field, $default);
}
protected function _initDFRelations()
{
if (null == ($name = trim($this->getName(), '[]'))) {
throw new Exception('Element name can not be empty');
}
$teamRules = array(
array(
'value' => 'newTeam',
'_elements' => array('teamName'),
),
array(
'value' => 'individual',
'_elements' => array(),
),
);
if ($this->_getTeams()) {
$teamRules[] = array(
'value' => 'existingTeam',
'_elements' => array('teamId'),
);
}
$this->_dfRelations = array(
array(
'_element' => 'type',
'node' => '[name="' . $name . '[type]"]:checked',
'group' => '[name="' . $name . '[type]"]',
'event' => 'change',
'rules' => $teamRules
)
);
return $this;
}
/**
* @param string $name element name
* @return string element label id (e.g. foo-label)
*/
protected function _dfGetElementLabelId($name)
{
return $this->getId()
. '-' . $this->getElement($name)->getId()
. '-label';
}
/**
* @param string $name element name
* @return string element container id (e.g. foo-element)
*/
protected function _dfGetElementContainerId($name)
{
return $this->getId()
. '-' . $this->getElement($name)->getId()
. '-element';
}
public function getValues($suppressArrayNotation = false)
{
$values = parent::getValues($suppressArrayNotation);
$this->_initDFRelations();
$this->dfClearUnusedValues($values);
return $values;
}
protected function _initElements()
{
$type = $this->_getSubData('type');
$teams = $this->_getTeams();
$teamOptions = array(
self::TYPE_NEW_TEAM => 'New Team',
self::TYPE_EXISTING_TEAM => 'Existing Team',
self::TYPE_INDIVIDUAL => 'Individual'
);
if (empty($teams)) {
unset($teamOptions[self::TYPE_EXISTING_TEAM]);
}
$this->addElement(
'radio',
'type',
array(
'label' => 'How are You Registering?',
'separator' => ' ',
'multiOptions' => $teamOptions,
'required' => true
)
);
$this->addElement(
'text',
'teamName',
array(
'label' => 'Team Name',
'required' => (self::TYPE_NEW_TEAM === $type),
'filters' => array('StringTrim'),
)
);
$this->getElement('teamName')->getDecorator('label')->setOption('class', 'required');
if ($teams) {
$this->addElement(
'radio',
'teamId',
array(
'label' => 'Choose Team',
'separator' => '',
'multiOptions' => $teams,
'escape' => false,
'required' => (self::TYPE_EXISTING_TEAM === $type)
)
);
$this->getElement('teamId')->getDecorator('label')->setOption('class', 'required');
}
return $this;
}
protected function _getTeams()
{
if (null === $this->_teams) {
$result = array();
$groupLimit = (int) $this->getDataObj()->getData('groupLimit');
foreach ($this->getDataObj()->getEventTeams() as $id => $team) {
$title = htmlspecialchars($team['title']);
if (0 == $groupLimit) {
$result[$id] = sprintf($this->_teamFormat, $title);
} else {
$freeSpots = $groupLimit - $team['attendeeCount'];
$result[$id] = (0 == $freeSpots)
? sprintf($this->_teamFormatNoSpots, $title)
: sprintf($this->_teamFormatLimited, $title, $groupLimit - $team['attendeeCount'], $groupLimit);
}
}
$this->_teams = $result;
}
return $this->_teams;
}
protected function _addResources()
{
$subFormId = $this->getId() . '-element';
$this->_initDFRelations();
$dynamicFormOptions = array(
'relations' => $this->dfConvertRelations()
);
/** @var $doc \Qs_Doc */
$doc = Zend_Registry::get('doc');
$doc->addScript('js/jquery.dynamicForm.js');
$doc->addInitFunction("$('#{$subFormId}').dynamicForm", array($dynamicFormOptions));
return parent::_addResources();
}
public function setDataObj(Obj $dataObj)
{
$this->_dataObj = $dataObj;
return $this;
}
public function getDataObj()
{
if (null === $this->_dataObj) {
throw new \Qs_Exception_EmptyPropertyException($this, '_dataObj');
}
return $this->_dataObj;
}
}