CVS edit link for hiddentao

I wanted to be able to easily bulk-edit Ubercart product stock levels on one page. I searched extensively on the web and was only able to find the following modules which didn't quite meet my requirements:

* Multiple stock edit (http://www.ubercart.org/contrib/5411) - This was originally built for Drupal 5 and Ubercart 1 and as of today there is still no official version for Ubercart 2. Users have been maintaining it bit by bit (see that page) but another issue with it is that it is overkill for what I want - a simple stock editor.

* Stock & Price updater (http://www.ubercart.org/contrib/12428) - This does allow for bulk updating but it's designed for use with a CSV file. I wanted something that would allow me to edit values directly through the webpage.

* Views Bulk Operations (http://drupal.org/project/views_bulk_operations) - This seemed unable to expose the stock level as and update-able field. Also it's sense of bulk-editing involves setting the same value for all products, rather than per-product values.

My solution is modelled on the 'Stock report' page displayed by the uc_stock module. However it makes the stock level editable in place and utilises AJAX (jQuery) to provide instant submission of the updated value (with error/success feedback) to the server. The user can choose to view all products on a single page or opt for a paged view of products (the default view) in case their product catalogue is very large.

I've tested my module on a client website and have placed the code available for download at this page: http://www.hiddentao.com/code/drupal-bulk-stock-updater/

Thankyou.

Comments

hiddentao’s picture

Status: Postponed (maintainer needs more info) » Active
StatusFileSize
new6.21 KB

Please find attached the full code for the module.

Instructions available here -> http://www.hiddentao.com/code/drupal-bulk-stock-updater/

hiddentao’s picture

Status: Active » Needs review
avpaderno’s picture

Status: Needs review » Needs work
Issue tags: +Module review

Hello, and thanks for applying for a CVS account. I am adding the review tags, and some volunteers will review your code, pointing out what needs to be changed.

As per http://drupal.org/cvs-application/requirements, the motivation message should be expanded to contain more details about the features of the proposed module, and it should include also a comparison with the existing solutions.

hiddentao’s picture

Status: Needs review » Needs work

How my solution works:

My solution is a bulk product stock level updater for Ubercart 2.x running on Drupal 6.x. Its interface is modelled on the 'Stock report' page displayed by the uc_stock module. It adds a menu item:

Store administration > Stock > Bulk update

To view the page the user must have the 'administer products' permission (same as for editing Ubercart products).

The page lists all products along with their stock levels and minimum stock thresholds. The stock levels are shown as editable text fields. When the user changes the value within a text field and then takes the focus away from it an AJAX call is made to the server (and handled by uc_bulk_stock_updater) to update the stock level for that particular product with what the user entered.

A progress indicator is shown whilst the AJAX call is taking place and the user is given success/error feedback once complete. If the call fails or the update on the server-side fails then the stock level for the product is set back to its original value. The user can choose to view all products on a single page or opt for a paged view of products (the default view) in case their product catalogue is very large.

The benefit of using AJAX to perform the update rather than the standard form submission mechanism is that it minimises the chances of a HTTP call timeout (due to lots of data having to be updated) and it is also quicker since less data needs to be passed between client and server.

Comparing uc_bulk_stock_updater to existing solutions:

* Multiple stock edit (http://www.ubercart.org/contrib/5411) - This supports the editing of stock levels of multiple products in one go. However the official version is only for Ubercart 1.x running on Drupal 5. Forum users have tried to provide patches now and then to make it work with Ubercart 2.x however it is still not fully functional on this newer version. What's more the stock editing form utilises the standard Drupal form mechanism whereby the user enters the updated stock levels for 30+ items and then saves them by submitting the form. When updating such a large number of items there is potential for a HTTP timeout to occur if, for instance, the database accesses are being particularly slow at that point. The approach taken in my module avoids this problem and it also works quicker because it uses AJAX (meaning less data needs to be passed between client adn server).

* Stock & Price updater (http://www.ubercart.org/contrib/12428) - This does allow for bulk updating but it's designed for use with a CSV file. The user uploads a CSV file which then gets imported line by line. The problem with this is that it doesn't allow you to quickly update a few stock levels without first having to create a CSV file to upload. It's designed more for updating stock levels in Ubercart from an existing supply spreadsheet that the business may be using. My solution allows for updates on the webpage itself so you can choose how many to update in any one go whilst at the same time avoiding problems due to corrupt CSV files. If a user wishes to do a CSV-style update they can simply use this module instead of mine.

* Views Bulk Operations (http://drupal.org/project/views_bulk_operations) - This claims to be able to provide a bulk stock level updater. I tested it out and was unable to expose the stock level as an update-able field. What's more, its idea of bulk updating involves setting the same value for all the items in the list. My solution allows for each product to have a different stock level set for it and it's also more intuitive to get going with.

Ubercart project page -> http://www.ubercart.org/project/uc_bulk_stock_updater

avpaderno’s picture

Status: Needs work » Needs review
hiddentao’s picture

StatusFileSize
new7.23 KB
hiddentao’s picture

Status: Needs work » Needs review
StatusFileSize
new7.26 KB

Most recent code attached.

hiddentao’s picture

StatusFileSize
new7.34 KB

Bulk stock updater now uses the 'bulk update stock' permission.

calbasi’s picture

Subscriving (I'll test it later ;-) ). Thanks for the ubercart bulk stock comparing :-)

hiddentao’s picture

Title: hiddentao [hiddentao] » Bulk Stock Updater module
hiddentao’s picture

Title: Bulk Stock Updater module » Bulk Stock Updater for Ubercart 2
avpaderno’s picture

Title: Bulk Stock Updater for Ubercart 2 » hiddentao [hiddentao]
Assigned: Unassigned » avpaderno
Status: Needs review » Needs work
  1. hook_uninstall() is not implemented; the module needs to remove the Drupal variables it defines.
  2.     $rows[] = array(
          'data' => array(
            array('data' => "<span id=\"$filter_val\">$stock->sku</span>"),
            array('data' => l($stock->title, 'node/' . $stock->nid)),
            array('data' => "<input size=\"3\" maxlength=\"9\" class=\"uc_bulk_stock_updater-value\" name=\"$stock->sku\" value=\"$stock->stock\" />"),
            array('data' => $stock->threshold),
          ),
          'class' => (intval($stock->threshold) >= intval($stock->stock)) ? 'uc-stock-below-threshold' : 'uc-stock-above-threshold',
        );
    

    Form fields needs to be output using the FAPI functions provided by Drupal. Avoid escaping the string delimiter inside strings; if you need to use one of the string delimiters inside a string, then use the other delimiter for the string.

  3.   	$filter_val = strtolower($stock->sku . $stock->title . $stock->stock . $stock->threshold);
    

    See http://drupal.org/coding-standards to understand how a module should be written. In particular, see which Unicode functions should be used, and how the code should be formatted.

  4.   $sql = "SELECT s.nid, sku, title, stock, threshold FROM {uc_product_stock} as s LEFT JOIN {node} as n ON s.nid = n.nid WHERE active = 1 AND title <> ''";
    

    SQL reserved words are written in uppercase letters; the reserved words include AS (see http://drupal.org/node/141051 for a more complete list of reserved words).

  5. Remove any reference to the license, or the license conditions from the code. Each module is automatically licensed with the same license used by Drupal. Any license reference would become obsolete when Drupal would change its own license.
    Also the copyright information would not be correct when you accept patches provided from other users; in such cases, the code is not copyrighted by Ramesh Nair anymore.
  6.   $items[_UC_BULK_STOCK_UPDATER_MENU_UPDATE_AJAX] = array(
        'access arguments' => uc_bulk_stock_updater_perm(),
        'page callback' => 'uc_bulk_stock_updater_stock_update_ajax',
      	'type' => MENU_CALLBACK,
        'file' => 'uc_bulk_stock_updater.inc',
      );
    

    I would not suggest to use the implementation of hook_perm() in that way; if the module would define more than one permission, the code would probably fail to verify if the user has the permission to access the menu callback. Then, that code could not be used for Drupal 7.

[Edited by kiamlaluno to remove an extraneous tag.]

hiddentao’s picture

Status: Needs work » Needs review
StatusFileSize
new5.68 KB

Hi kiamlaluno, thanks for the reply.

I've made changes according to your suggestions and attached the latest code to this comment. Please let me know if these are sufficient.

hiddentao’s picture

Status: Needs review » Needs work

Just came across the #ahah keyword: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

I'm going to try and make my forms work this way.

hiddentao’s picture

Status: Needs work » Needs review

According to http://drupal.org/node/305747:

If you are just looking to load some normal content (as opposed to form elements) dynamically, then AHAH, in this Drupal sense, is not the right tool for you - have a look at section 6 of this guide, AJAX in Drupal using jQuery

I'm only performing a POST operation via AJAX and not recieving any content back except an error message (in case the server-side update failed). So I don't think #ahax is appropriate for my functionality.

hiddentao’s picture

Regarding hook_uninstall() is not implemented; the module needs to remove the Drupal variables it defines., the module does not define any variables of its own. It re-uses the uc_reports_table_size variable from the uc_reports module.

Do I still need to implement hook_uninstall() ?

avpaderno’s picture

Status: Needs review » Fixed
  1. uc_bulk_stock_updater_stock_update() should probably use a theme function, as it is outputting HTML.
  2. JavaScript code should use Drupal behaviors.
  3. The version line needs to be removed from uc_block_updater.info; the packaging script already adds that line, and reporting two different versions can confuse the update manager.
  4. The file README should be renamed to README.txt to avoid problems for who is using Windows.
  5. /**
     * Main admin page.
     * @return HTML.
     */
    function uc_bulk_stock_updater_stock_overview() {
    	drupal_goto(_UC_BULK_STOCK_UPDATER_MENU_UPDATE);
    	return;
    }
    

    Why is the module defining a menu callback to just redirect the user to a different page?

Thank you for your contribution! I am going to update your account.

These are some recommended readings to help with excellent maintainership:

You can find more contributors chatting on the IRC #drupal-contribute channel. So, come hang out and stay involved.
Thank you, also, for your patience with the review process.
Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.

I thank all the dedicated reviewers as well.

Status: Fixed » Closed (fixed)
Issue tags: -Module review

Automatically closed -- issue fixed for 2 weeks with no activity.

avpaderno’s picture

Component: Miscellaneous » new project application
Issue summary: View changes