{headCell}'; var $_tplActElem = '{title}'; var $_tplTblBody = '{head}
'; var $_actions = array(); var $_colWidth = 130; var $_cols = array( array('name' => 'one', 'title' => 'One') ); var $_additionalContent = array( 'begin'=>'', 'end'=>'', ); function HTML_QuickForm_grid($elementName = '', $elementLabel = '', $cols = Null, $value = array(), $attributes=array()) { if (is_array($cols)){ $this->_cols = $cols; } parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); $this->setValue($value); $this->setAction('add', 'Add', ' function multiVal_add_row(tableId, cells) { var tbody = document.getElementById(tableId); var new_row = document.createElement("tr"); var i; for(i in cells){ var newCol = document.createElement("td"); newInput = document.createElement("input"); newInput.setAttribute("type", "text"); newInput.setAttribute("name", "{elementName}["+i+"][]"); newInput.setAttribute("value", cells[i]); newInput.style.width = "{colWidth}px"; newCol.appendChild(newInput); new_row.appendChild(newCol); } var newCol = document.createElement("td"); newCol.style.width = "{colWidth}px"; newCol.innerHTML = "{options}"; new_row.appendChild(newCol); tbody.appendChild(new_row); } '); $this->setAction('del', 'Delete', ' function multiVal_del_row(delElem) { var td = delElem.parentNode; var tr = td.parentNode; var tbody = tr.parentNode; tbody.removeChild(tr); } '); } function setTplHead($template) { $this->_tplHead = $template; } function setAction($name, $title = null, $function = null) { if (!is_null($title)){ $this->_actions[$name]['title'] = $title; } if (!is_null($function)){ $this->_actions[$name]['function'] = $function; } return true; } function getAction($name) { return $this->_actions[$name]; } function getActions() { return $this->_actions; } function delAction($name) { $this->_actions[$name] = null; return true; } function _getOptions() { $options = $this->_tplOptions; foreach ($this->getActions() as $key => $act) { $options = str_replace('{'.$key.'}', $this->_getActElem("multiVal_{$key}_row(this)", $act['title']), $this->_tplOptions); } return addslashes($options); } function _toHtmlBody() { $head = ''; foreach ($this->_cols as $col) { $head .= str_replace(array('{headCell}', '{options}'), array($col['title'], ' style="width:'.($this->_colWidth).'px;" ') , $this->_tplHead); } $actions = $this->_actions; $actCount = count($this->_actions); $head .= str_replace(array('{headCell}', '{options}'), array($this->_optionsTitle, ' width="'. $this->_colWidth.'" ') , $this->_tplHead); $head = ''.$head.''; $html = str_replace(array('{name}', '{head}'), array($this->getName(), $head), $this->_tplTblBody); $names = array(); foreach ($this->_cols as $col) { $names[ $col['name'] ] = ''; } $html .= $this->_getActElem($this->_getColls2Add($names), $this->_actions['add']['title']); return $html; } function _getActElem($function, $title, $options = '' ) { return str_replace(array('{function}', '{title}', '{options}'), array($function, $title, $options), $this->_tplActElem); } function toHtml() { $actionsJsFunctions = ''; $options = $this->getActions(); foreach ($options as $name => $action) { if ('add' == $name){ $actionsJsFunctions .= str_replace(array('{colWidth}', '{elementName}', '{options}'), array($this->_colWidth, $this->getName(), $this->_getOptions()), $action['function']); }else { $actionsJsFunctions .= $action['function']; } } $actionsJsFunctions .= $this->_getJs2Add(); $JS = ' '; $html = $this->_toHtmlBody(); $html .= str_replace('{actions_js_functions}', $actionsJsFunctions, $JS); $html = $this->_additionalContent['begin'].$html.$this->_additionalContent['end']; return $html; } function _getJs2Add() { $js = ''; $firstName = $this->_cols[0]['name']; $value = $this->getValue(); foreach ((array)$value[$firstName] as $key => $val) { $row = array(); foreach ($this->_cols as $col) { $colName = $col['name']; $row[$colName] = $value[$colName][$key]; } $js .= $this->_getColls2Add($row); } return $js; } function _getColls2Add($list) { $colls = '{'; foreach ($list as $key => $val) { $colls .= "'".$key."':'".addslashes($val)."', "; } $names = substr($colls, 0, -2) .'}'; return ' multiVal_add_row(\''.$this->getName().'\', '.$names.'); '; } function setOptionsTitle($title) { $this->_optionsTitle = $title; return true; } } ?>