function display($tpl = null) { // Get data from the model require_once JPATH_COMPONENT . '/models/ajaxvote.php'; $model = new MijopollsModelAjaxvote(); $vote = $model->getVoted(); $data = $model->getData(); $total = $model->getTotal(); $poll_id = JRequest::getInt('id', 0, 'post'); if (MijopollsHelper::is15()) { // create root node $xml = new JSimpleXMLElement('poll', array('id' => $poll_id)); //get total votes $sum = 0; foreach ($data as $row) { $sum += $row->votes; } $number_voters = 0; $options = $xml->addChild('options'); for ($i = 0; $i < $total; $i++) { $option = $options->addChild('option', array('id' => $data[$i]->id, 'percentage' => self::_toPercent($data[$i]->votes, $sum), 'votes' => $data[$i]->votes, 'color' => $data[$i]->color)); $text = $option->addChild('text'); $text->setData($data[$i]->text); $number_voters += $data[$i]->votes; } $voters = $xml->addChild('voters'); $voters->setData($number_voters); $this->assign('xml', $xml->toString()); } else { // create root node $xml = new JXMLElement('<poll></poll>'); $xml->addAttribute('id', $poll_id); //get total votes $sum = 0; foreach ($data as $row) { $sum += $row->votes; } $number_voters = 0; $options = $xml->addChild('options'); for ($i = 0; $i < $total; $i++) { $option = $options->addChild('option'); $option->addAttribute('id', $data[$i]->id); $option->addAttribute('percentage', self::_toPercent($data[$i]->votes, $sum)); $option->addAttribute('color', $data[$i]->color); $option->addChild('text', $data[$i]->text); $number_voters += $data[$i]->votes; } $xml->addChild('voters', $number_voters); $this->assign('xml', $xml->asFormattedXML()); } $this->setLayout('raw'); parent::display($tpl); }
/** * Testing toString(). * * @return void */ public function testToString() { $this->buildXMLTree(); $this->assertThat( $this->object->toString(), $this->equalTo( "\n<test>\n\t<child1>\n\t\t<child2 height=\"300\">\n\t\t\t<child3 />\n\t\t</child2>\n". "\t</child1>\n\t<child4>Fred</child4>\n</test>" ), "Test with whitespace turned on" ); $this->assertThat( $this->object->toString(false), $this->equalTo( "<test><child1><child2 height=\"300\"><child3 /></child2></child1><child4>Fred</child4></test>" ), "Test without whitespace turned on" ); }
/** * Prepare an xml file content holding * a standard record for returning result * of an ajax request * * @param JView $view the view handling the request */ public static function prepareAjaxResponse($view) { // use Joomla wml object jimport('joomla.utilities.simplexml'); // create a root node $xml = new JSimpleXMLElement('item', array('id' => 'shajax-response')); // add children : status, message, message code, task $status =& $xml->addChild('status'); $message =& $xml->addChild('message'); $messagecode =& $xml->addChild('messagecode'); $taskexecuted =& $xml->addChild('taskexecuted'); // set default values $status->setData('_'); $message->setData('_'); $messagecode->setData('_'); $taskexecuted->setData('_'); // set their respective values $vErrors = $view->getErrors(); if (empty($vErrors)) { // retrieve messagecode and task if (empty($view->messagecode)) { $view->assign('messagecode', 'COM_SH404SEF_OPERATION_COMPLETED'); } if (empty($view->taskexecuted)) { $view->assign('taskexecuted', ''); } // either a success or a redirect if (empty($view->redirectTo)) { // no error $status->setData('success'); $msg = empty($view->message) ? JText16::_('COM_SH404SEF_OPERATION_COMPLETED') : $view->message; $message->setData('<ul>' . $msg . '</ul>'); $messagecode->setData($view->messagecode); } else { $status->setData('redirect'); $glue = strpos($view->redirectTo, '?') === false ? '?' : '&'; $message->setData($view->redirectTo . $glue . 'sh404sefMsg=' . $view->messagecode); } $taskexecuted->setData($view->taskexecuted); } else { $status->setData('failure'); $messageTxt = ''; foreach ($vErrors as $error) { $messageTxt .= '<li>' . $error . '</li>'; } $message->setData('<ul>' . $messageTxt . '</ul>'); } // output resulting text, no need for a layout file I think $output = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; $output .= $xml->toString(); return $output; }
/** * Return a well-formed XML string based on SimpleXML element * * @return string */ function toString($whitespace = true) { $ret = str_replace('_0x3a', ':', parent::toString($whitespace)); $ret = str_replace(" ", ' ', $ret); // Workaround for a bug in JW Media Player return str_replace(array('<tracklist>', '</tracklist>'), array('<trackList>', '</trackList>'), $ret); }