Skip to content

Commit

Permalink
Merge pull request #1463 from dreamsxin/tag_select
Browse files Browse the repository at this point in the history
Fix #1422 Tag select support optgroup
  • Loading branch information
Phalcon committed Oct 28, 2013
2 parents 1f99cb9 + 5bf1bd3 commit 177d4bc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
36 changes: 23 additions & 13 deletions ext/tag/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ PHP_METHOD(Phalcon_Tag_Select, _optionsFromResultset){
PHP_METHOD(Phalcon_Tag_Select, _optionsFromArray){

zval *data, *value, *close_option, *code, *option_text = NULL;
zval *option_value = NULL, *escaped;
zval *option_value = NULL, *escaped, *array_options = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
Expand All @@ -378,22 +378,32 @@ PHP_METHOD(Phalcon_Tag_Select, _optionsFromArray){
PHALCON_GET_HKEY(option_value, ah0, hp0);
PHALCON_GET_HVALUE(option_text);

phalcon_htmlspecialchars(escaped, option_value, NULL, NULL TSRMLS_CC);

if (Z_TYPE_P(value) == IS_ARRAY) {
if (phalcon_fast_in_array(option_value, value TSRMLS_CC)) {
PHALCON_SCONCAT_SVSVV(code, "\t<option selected=\"selected\" value=\"", escaped, "\">", option_text, close_option);
} else {
PHALCON_SCONCAT_SVSVV(code, "\t<option value=\"", escaped, "\">", option_text, close_option);
}
if (Z_TYPE_P(option_text) == IS_ARRAY) {
phalcon_htmlspecialchars(escaped, option_value, NULL, NULL TSRMLS_CC);


PHALCON_INIT_NVAR(array_options);
phalcon_call_self_p3(array_options, this_ptr, "_optionsfromarray", option_text, value, close_option);

PHALCON_SCONCAT_SVSVS(code, "\t<optgroup label=\"", escaped, "\">" PHP_EOL, array_options, "\t</optgroup>" PHP_EOL);
} else {
if (PHALCON_IS_EQUAL(option_value, value)) {
PHALCON_SCONCAT_SVSVV(code, "\t<option selected=\"selected\" value=\"", escaped, "\">", option_text, close_option);
phalcon_htmlspecialchars(escaped, option_value, NULL, NULL TSRMLS_CC);

if (Z_TYPE_P(value) == IS_ARRAY) {
if (phalcon_fast_in_array(option_value, value TSRMLS_CC)) {
PHALCON_SCONCAT_SVSVV(code, "\t<option selected=\"selected\" value=\"", escaped, "\">", option_text, close_option);
} else {
PHALCON_SCONCAT_SVSVV(code, "\t<option value=\"", escaped, "\">", option_text, close_option);
}
} else {
PHALCON_SCONCAT_SVSVV(code, "\t<option value=\"", escaped, "\">", option_text, close_option);
if (PHALCON_IS_EQUAL(option_value, value)) {
PHALCON_SCONCAT_SVSVV(code, "\t<option selected=\"selected\" value=\"", escaped, "\">", option_text, close_option);
} else {
PHALCON_SCONCAT_SVSVV(code, "\t<option value=\"", escaped, "\">", option_text, close_option);
}
}
}

zval_dtor(escaped);
ZVAL_NULL(escaped);

Expand Down
39 changes: 39 additions & 0 deletions unit-tests/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,45 @@
class TagTest extends PHPUnit_Framework_TestCase
{

public function testSelect()
{
$data = array(
"status",
array("Active" => array('A1' => 'A One', 'A2' => 'A Two'), "B" => "B One")
);

$html = <<<HTML
<select id="status" name="status">
<optgroup label="Active">
<option value="A1">A One</option>
<option value="A2">A Two</option>
</optgroup>
<option value="B">B One</option>
</select>
HTML;

$di = new Phalcon\DI\FactoryDefault();
Tag::setDI($di);
$ret = Tag::selectStatic($data);

$this->assertEquals($ret, $html);

$html = <<<HTML
<select id="status" name="status">
<optgroup label="Active">
<option selected="selected" value="A1">A One</option>
<option value="A2">A Two</option>
</optgroup>
<option value="B">B One</option>
</select>
HTML;
Tag::setDefault("status", "A1");

$ret = Tag::selectStatic($data);

$this->assertEquals($ret, $html);
}

public function testSetTitleSeparator()
{

Expand Down

0 comments on commit 177d4bc

Please sign in to comment.