Skip to content

Commit

Permalink
adjusting tagger so it doesnt save empty tag values. updating admin c…
Browse files Browse the repository at this point in the history
…reate/update forms for pages to use tagging. should revisit it and turn it into a helper.
  • Loading branch information
tmaiaroto committed Nov 8, 2011
1 parent e573a43 commit 501eacd
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 22 deletions.
63 changes: 55 additions & 8 deletions views/_admin/pages/create.html.php
@@ -1,3 +1,6 @@
<?php
$tagger_javascript = '';
?>
<div class="grid_16">
<h2 id="page-heading">Create <?=$display_name; ?></h2>
</div>
Expand All @@ -15,10 +18,28 @@
<?php
foreach($fields as $k => $v) {
if(!isset($v['form']['position']) || $v['form']['position'] != 'options') {
?>
<?=$this->form->field($k, $v['form']);?>
<?php
}
// if(!isset($v['type']) || $v['type'] != 'array') {
if(!isset($v['form']['class']) || $v['form']['class'] != 'tagger') {
echo $this->form->field($k, $v['form']);
} else {
// For tags... it's an array of values, so it breaks the form->field() helper method
if(isset($v['form']['class']) && $v['form']['class'] == 'tagger') {
// So capture its value and we'll use it to add the tags with the jQuery plugin
if(is_object($document) && isset($document->$k)) {
$v['value'] = $document->$k->data();
} else {
$v['value'] = array();
}
$v['form']['id'] = ucfirst($k);
$tagger_javascript .= '$("#' . ucfirst($k) . '").addTag("' . implode(',', $v['value']) . '");';
unset($document->$k);
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
}
}
}
}
}
?>
<?=$this->form->submit('Add ' . $display_name); ?> <?=$this->html->link('Cancel', array('admin' => $this->minervaHtml->admin_prefix, 'library' => 'minerva', 'controller' => 'pages', 'action' => 'index', 'args' => array($document_type))); ?>
Expand All @@ -35,10 +56,31 @@
foreach($fields as $k => $v) {
if(isset($v['form'])) {
if(isset($v['form']['position']) && $v['form']['position'] == 'options') {
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
// if(!isset($v['type']) || $v['type'] != 'array') {
if(!isset($v['form']['class']) || $v['form']['class'] != 'tagger') {
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
}
} else {
// For tags... it's an array of values, so it breaks the form->field() helper method
if(isset($v['form']['class']) && $v['form']['class'] == 'tagger') {
// So capture its value and we'll use it to add the tags with the jQuery plugin
if(is_object($document) && isset($document->$k)) {
$v['value'] = $document->$k->data();
} else {
$v['value'] = array();
}
$v['form']['id'] = ucfirst($k);
$tagger_javascript .= '$("#' . ucfirst($k) . '").addTag("' . implode(',', $v['value']) . '");';
unset($document->$k);
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
}
}
}

}
}
}
Expand All @@ -56,4 +98,9 @@
</div>

<?=$this->form->end(); ?>
<div class="clear"></div>
<div class="clear"></div>
<script type="text/javascript">
$(document).ready(function() {
<?php echo $tagger_javascript; ?>
});
</script>
66 changes: 55 additions & 11 deletions views/_admin/pages/update.html.php
@@ -1,3 +1,6 @@
<?php
$tagger_javascript = '';
?>
<div class="grid_16">
<h2 id="page-heading">Update <?=$display_name; ?></h2>
</div>
Expand All @@ -11,9 +14,28 @@
<?php
foreach($fields as $k => $v) {
if(!isset($v['form']['position']) || $v['form']['position'] != 'options') {
?>
<?=$this->form->field($k, $v['form']);?>
<?php
//if(!isset($v['type']) || $v['type'] != 'array') {
if(!isset($v['form']['class']) || $v['form']['class'] != 'tagger') {
echo $this->form->field($k, $v['form']);
} else {
// For tags... it's an array of values, so it breaks the form->field() helper method
if(isset($v['form']['class']) && $v['form']['class'] == 'tagger') {
// So capture its value and we'll use it to add the tags with the jQuery plugin
if(is_object($document) && isset($document->$k)) {
$v['value'] = $document->$k->data();
} else {
$v['value'] = array();
}
$v['form']['id'] = ucfirst($k);
$tagger_javascript .= '$("#' . ucfirst($k) . '").addTag("' . implode(',', $v['value']) . '");';
unset($document->$k);
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
}
}
}

}
}
?>
Expand All @@ -30,14 +52,31 @@
<?php
foreach($fields as $k => $v) {
if(isset($v['form']['position']) && $v['form']['position'] == 'options') {
?>
<?=$this->form->field($k, $v['form']);?>

<?php
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
// if(!isset($v['type']) || $v['type'] != 'array') {
if(!isset($v['form']['class']) || $v['form']['class'] != 'tagger') {
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
}
} else {
// For tags... it's an array of values, so it breaks the form->field() helper method
if(isset($v['form']['class']) && $v['form']['class'] == 'tagger') {
// So capture its value and we'll use it to add the tags with the jQuery plugin
if(is_object($document) && isset($document->$k)) {
$v['value'] = $document->$k->data();
} else {
$v['value'] = array();
}
$v['form']['id'] = ucfirst($k);
$tagger_javascript .= '$("#' . ucfirst($k) . '").addTag("' . implode(',', $v['value']) . '");';
unset($document->$k);
echo $this->form->field($k, $v['form']);
if(isset($v['form']['help_text'])) {
echo '<div class="help_text">' . $v['form']['help_text'] . '</div>';
}
}
}
}
}
}
?>
</fieldset>
Expand All @@ -46,4 +85,9 @@
</div>

<?=$this->form->end(); ?>
<div class="clear"></div>
<div class="clear"></div>
<script type="text/javascript">
$(document).ready(function() {
<?php echo $tagger_javascript; ?>
});
</script>
37 changes: 34 additions & 3 deletions webroot/js/jquery/jquery.tagger.js
Expand Up @@ -14,6 +14,14 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
This is a modified version of Tagger, for use with Minerva CMS.
The edits shouldn't change how Tagger works, but it enhances it with some
sanity checking and appends the inputs directly to the form element instead
of to the .data('list') where the tags are added visually (with removal option).
This was the only way the form data seemed to see the fields in my experience.
I'm not sure how it worked before. It could be the positioning of the form
within the layout or something weird. This makes it all work though.
*/

(function($){
Expand All @@ -33,15 +41,38 @@
})
.data('hidden',i);
var l = $(this).data('list');
$(l).append(t).append(i);
// Originally, it appended both label and input field
//$(l).append(t).append(i);
//
// Append the tag (and remove option) to the list of tags visually, but not the hidden input
$(l).append(t);

// Must append the input to the form! It's not being seen otherwise.
var elem = $(this);
var form = elem.length > 0 ? $(elem[0].form) : $();
$(form).append(i);
}
return this;
};

})(jQuery);

jQuery(function(){
jQuery(function(){
// This ensures that the tagger text input field is disabled upon submit.
// Otherwise, we're saving empty values in a tag array in the database.
var input = $('.tagger');
var form = input.length > 0 ? $(input[0].form) : $();
$(form).bind('submit', function() {
$(input).attr('disabled', true);
});

$('.tagger').each(function(i){
// Make the input a multiple (array) input if it isn't already
var n = $(this).attr('name');
if(n.substr(-2) != '[]') {
$(this).attr('name', n + '[]');
}

$(this).data('name', $(this).attr('name'));
var b = $('<button type="button" />').html('Add').addClass('tagAdd')
.click(function(){
Expand Down

0 comments on commit 501eacd

Please sign in to comment.