Friday, April 15, 2011

Reading .NET JSON Date's in jQuery

ASP.Net writes out date objects to a JSON stream in the format { "\/Date(x)\/" } but jQuery will not automatically convert this to a JavaScript date but rather keeps it as a string. To fix that this simple converter script can be included in your global JavaScript header.

Note that this does requite jQuery 1.5+.

$.ajaxSetup({
    // Add a custom converter to convert ASP.NET JSON dates to JS Date object
    // That is convert { "\/Date(...)\/" } to { new Date(...) }
    converters: {
        "text json": function( textValue ) {
            return jQuery.parseJSON(textValue.replace(/"\\\/Date\((-?\d*)\)\\\/"/g, "new Date($1)"));
        }
    }
});

Wednesday, April 13, 2011

Adding Better Ajax/JSON methods to HighCharts

I recently started using Highcharts as the charting options in Crystal Reports are rather limited. Here is a small patch I made to simplify the loading of chart data from JSON server calls via AJAX. This does not do anything incredibly unique, just simplifies the code to do so by adding an ajaxSource property to the chart and series configurations that is loaded on init and a reloadAjax method to automatically reload the data.

05/03/2011 - Rewrote the code as a extension instead of a patch and added support for setting multiple series via options.chart.ajaxSource.

Thursday, August 12, 2010

Mass rename files in Midnight Commander

Here is another script for Midnight Commander to allow for mass renaming of flagged files. To use it you will need to add it to your MC menu file in /etc/mc/mc.menu or ~/.mc/menu.

+ t t
e       Rename tagged files
        set %t; CMD=%{Enter regex}
        while [ -n "$1" ]; do
          echo rename -\"s"$CMD"e\" "$1"
          rename -\"s"$CMD"e\" "$1"
          shift
        done

first you will need to flag the files toy want to rename then execute the user operation. When you do so you will be prompted for the regular expression you with to execute.

Some examples:
 /oldword/newword/   # Change a word.
 /_/ /g   # Replace all _'s with spaces.
 /_\[.*\]//   # Remove _[CRCHASH] tags.
 /\(.*\)//   # Remove (...) sections.

Comapring files with md5 Checksums in Midnight Commander

I wrote a few scripts for Midnight Commander to easily compare multiple copies of a directory via checksums.  There are a few variations listed below along with a bash script that does the same thing.



First off is this simple bash script that can be used stand alone but is also required for the MC menu entries. 

/usr/local/bin/md5compare
#!/usr/local/bin/bash
echo -n "Comparing $1 to $2: "
if [ ! -e "$1" ] || [ ! -e "$2" ]
then
 echo File Missing
 exit 1; 
fi

if [ $(md5 -q "$1") = $(md5 -q "$2") ]
then
 echo Files Match
 exit 0;
else
 echo Files Different
 exit 1;
fi

You may need to tweak the bash and script paths for your installation. Also remember to make it executable.

sudo chmod +x /usr/local/bin/md5compare

To execute this command directly just run the following.

md5compare file1 file2



Next up are two comparison scripts that can be added MC's user menu.  To do this append them to the mc.menu file in your system /etc or users ~/.mc/menu.

This script will compare the currently highlighted file with one of the same name in the other panel.

+ t n
w       Compare files via MD5 checksum
        /usr/local/bin/md5compare %f %D/%f
        echo Press any key to continue
        read key

This script on the other hand will compare all tagged files to any matching files in the other panel.

+ t t
W       Compare tagged files via MD5 checksum
        for i in %t
        do 
          /usr/local/bin/md5compare "$i" %D/"$i"
        done
        echo Press any key to continue
        read key

Finally here is a menu item to simply dispaly the checksum of the selected file.

+ t n
s       Calculate MD5 checksum
        echo Calculating checksum of %f
        md5 -q %f
        echo Press any key to continue
        read key

Monday, June 28, 2010

Tracking Customer Companies in OTRS.

I have been playing abound with OTRS for the past few days and ran into a bit of a snag.  I installed it from the Ubuntu repositories but noticed that the Customers can't be linked to the Companies witch would be a useful feature.  I also found out after some digging that there was no option in the SysConfig to fix this.  I found the setting by manually editing the config files but this was of limited use.  In the end I had to make a few manual edits to make the system work as expected.  I outlined the changes below to the base 2.4.7 install.

First open a terminal and cd to your otrs install directory.
cd /usr/share/otrs

Enable CustomerCompanySupport
sudo vim Kernel/Config.pm
Add the following line inside the sub Load method.
# Enable Customer Company linking.
$Self->{CustomerUser}->{CustomerCompanySupport} = 1;

Run the mysql client and add a field to the customer table.
mysql -u root -p
use otrs2;
alter table customer_user add column company_id varchar(100);
exit

Register the extra row with the editor.
sudo vim Kernel/Config/Defaults.pm
Find the CustomerUser variable and then the Map array within it.  Add the following line after the UserCustomerID field.
[ 'UserCompanyID',  'CompanyID',  'company_id',  0, 1, 'var', '', 0 ],

Change the company dropdown list to trigger on the new CompanyID field.
sudo vim Kernel/Modules/AdminCustomerUser.pm
Look for the reference to UserCustomerID and change it to UseCompanyID. It should look like this now.
$Entry->[0] =~ /^UserCompanyID$/i

Change the dispaly logic to match against the new CompanyID.
sudo vim Kernel/System/CustomerUser.pm
Look for the reference to UserCustomerID and change it to UseCompanyID. It should look like this now.
CustomerID => $Customer{UserCompanyID},

You can now restart apache and the changes should show up.
sudo /etc/init.d/apache2 restart