Important Notice: Effective immediately, all support will be handled through Discord. Read more about the changes coming to Anodyne's services.

Add Author Once

A place to show off (and others to download) completed MODs. Note: support for MODs is handled by third parties, not Anodyne Productions.

ohm Mon Sep 27, 2010 9:46 pm

Add Author Once

Something that's always annoyed me was that you could keep adding the same author to a mission post... You could even add the "Please select an Author" text to the post, and while when saving it would parse these properly it just... Well annoyed me.

So I added an extra line to the jscript to ensure this doesn't happen, and thought maybe this annoyed others as well so have some instructions on how to achieve this quick hack.

Step 1: Copy manage_posts_js.php and write_missionpost_js.php from /application/views/_base/admin/js to /application/views/_base_override/admin/js
Step 2: Edit manage_posts_js.php within the _base_override folder and overwrite this:
Code: Select all
      $('a#add_author').click(function() {
         var user = $('#all').val();
         var hidden = $('#authors_hidden').val();
         var name = $("option[value='" + user + "']").html();
         
         if (hidden == 0)
         {
            hidden = '';
         }
            
         $('#authors_hidden').val(hidden + ',' + user + ',');
            
         $('#authors').append('<span class="' + user + '"><a href="#" id="remove_author" class="image" myID="' + user + '" myName="' + name + '"><?php echo $remove;?></a>' + name + '<br /></span>');
            
         $("#all option[value='" + user + "']").attr('disabled', 'yes');
               
         return false;
      });


with this:
Code: Select all
      $('a#add_author').click(function() {
         var user = $('#all').val();
         var hidden = $('#authors_hidden').val();
         var name = $("option[value='" + user + "']").html();
         
         if (user != 0 && $("#all option[value='" + user + "']").is(':disabled') == false)
         {
            if (hidden == 0)
            {
               hidden = '';
            }
            
            $('#authors_hidden').val(hidden + ',' + user + ',');
            
            $('#authors').append('<span class="' + user + '"><a href="#" id="remove_author" class="image" myID="' + user + '" myName="' + name + '"><?php echo $remove;?></a>' + name + '<br /></span>');
            
            $("#all option[value='" + user + "']").attr('disabled', 'yes');
         }
         
         return false;
      });


Step 3: Repeat step 2 for write_missionpost_js.php
Step 4: Test it out!

Regards,

Commander Allen Phillips
Commanding Officer, USS Firebird

Posts: 12


ohm Tue Sep 28, 2010 11:04 pm

Re: Add Author Once

Additionally another thing may come to annoy you, as it did me, when editing a post that's previously saved or indeed posted your authors will not be marked as disabled in the authors list. This allows double ups to occur. The fix? Simple!

Note that I have modified a "core" file in step 1, or at least what may be considered core by some as it ships with the nova release. Perhaps David can provide some advise on a more wholesome way to add this function as a helper?

Step 1: Just before the end of the MY_form_helper.php in /application/helpers/ add this function:
Code: Select all
/**
 * Drop-down Menu
 *
 * @access   public
 * @param   string
 * @param   array
 * @param   string
 * @param   string
 * @return   string
 */
if ( ! function_exists('form_dropdown_with_disabled'))
{
   function form_dropdown_with_disabled($name = '', $options = array(), $selected = array(), $disabled = array(), $extra = '')
   {
      if ( ! is_array($selected))
      {
         $selected = array($selected);
      }
      
      if ( ! is_array($disabled))
      {
         $disabled = array($disabled);
      }

      // If no selected state was submitted we will attempt to set it automatically
      if (count($selected) === 0)
      {
         // If the form name appears in the $_POST array we have a winner!
         if (isset($_POST[$name]))
         {
            $selected = array($_POST[$name]);
         }
      }

      if ($extra != '') $extra = ' '.$extra;

      $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

      $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

      foreach ($options as $key => $val)
      {
         $key = (string) $key;

         if (is_array($val))
         {
            $form .= '<optgroup label="'.$key.'">'."\n";

            foreach ($val as $optgroup_key => $optgroup_val)
            {
               $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
               
               $dis = (in_array($optgroup_key, $disabled)) ? ' disabled="yes"' : '';

               $form .= '<option value="'.$optgroup_key.'"'.$sel.' '.$dis.'>'.(string) $optgroup_val."</option>\n";
            }

            $form .= '</optgroup>'."\n";
         }
         else
         {
            $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
            
            $dis = (in_array($key, $disabled)) ? ' disabled="yes"' : '';
            
            $form .= '<option value="'.$key.'"'.$sel.' '.$dis.'>'.(string) $val."</option>\n";
         }
      }

      $form .= '</select>';

      return $form;
   }
}


Step 2: Copy 2 files (write_missionpost.php and manage_posts_edit.php) from /application/views/_base/admin/pages/ to /application/views/_base_override/admin/pages/
Step 3: Edit the newly copied manage_posts_edit.php and replace the following line (line 18ish):
Code: Select all
<?php echo form_dropdown('other_authors', $all, '', 'id = "all"');?>

with:
Code: Select all
<?php echo form_dropdown_with_disabled('other_authors', $all, '', preg_split("/[\s,]+/", $to), 'id = "all"');?>

Step 4: Edit the newly copied write_missionpost.php and replace the following line (line 29ish):
Code: Select all
<?php echo form_dropdown('authors', $all, $key['all'], 'id = "all"');?>

with:
Code: Select all
<?php echo form_dropdown_with_disabled('authors', $all, $key['all'], preg_split("/[\s,]+/", $to), 'id = "all"');?>


There you go! A fully fixed author selection box.

Posts: 12


AgentPhoenix Wed Sep 29, 2010 7:33 am

Re: Add Author Once

This can actually be accomplished without the form_dropdown_disabled function you created, though it does require extending CI's core form_dropdown function.

Since both of these issues are actually bugs, I've talked to Patrick and he's agreed to let me use his code in Nova 1.1.2 to fix these issues. Thanks to Patrick for finding these and being so proactive to address them. :)
User avatar

Posts: 7596



Return to Nova MODs

Who is online

Users browsing this forum: No registered users and 1 guest

cron