Welcome

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

Fix for EE Multi-relationship field in PHP4

// http://expressionengine.com/forums/viewreply/192755/

Open your /system/modules/weblog/mod.weblog.php file and find from this line:
    
    // -------------------------------------------
    // 'weblog_entries_tagdata' hook.

to this line:
    
    //
    // -------------------------------------------

Replace between those comments with this:

    if (isset($EXT->extensions['weblog_entries_tagdata']))
    {
        // -- PHP4 Fix For call_user_func_array not passing by reference
        global $Weblog; $Weblog = $this;
        // -- End Fix
        
        $tagdata = $EXT->call_extension('weblog_entries_tagdata', $tagdata, $row, $this);
        if ($EXT->end_script === TRUE) return $tagdata;
        
        // -- PHP4 Fix For call_user_func_array not passing by reference
        $this = $Weblog; unset($Weblog);
        // -- End Fix
    

recursevly remove .svn files from an existing repo

If you need to take a project out of scm, you can run this bash script from the top folder - and it will remove all .svn folders recursevly (sp)

Works well for
- changing an svn checkout into and svn export
- switching from svn to git

find . -type d -name '.svn' -exec rm -rf {} \;

Show baloon hint in delphi

// Function to show baloon hint
// (found on pl.comp.lang.delphi newsgroup - posted by spook)

function ShowBaloonHint(Point: TPoint; Handle: THandle; Title: String;
Msg: String; Icon: Integer): Boolean;
var
  hwnd: THandle;
  ti: TToolInfo;
  hCursor: THandle;
  Rect: TRect;
  IconData: TNotifyIconData;

const
  TTS_BALLOON = $40;
  TTS_CLOSE = $80;

  procedure SetToolTipTitle(tt: THandle; IconType: Integer; Title: string);
  var
    buffer: array[0..255] of Char;
  const
    TTM_SETTITLE = (WM_USER + 32);
  begin
    FillChar(buffer, SizeOf(buffer), #0);
    lstrcpy(buffer, PChar(Title));
    SendMessage(tt, TTM_SETTITLE, IconType, Integer(@buffer));
  end;

begin
  hwnd:= CreateWindowEx(0,
                        TOOLTIPS_CLASS,
                        nil,
                        TTS_ALWAYSTIP or TTS_BALLOON or TTS_CLOSE,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        Application.MainForm.Handle,
                        0,
                        Application.Handle,
                        0);

  SetWindowPos( hwnd,
                HWND_TOPMOST,
                0,
                0,
                0,
                0,
                SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE);

  GetClientRect(Handle, Rect);

  with ti do
    begin
      cbSize:= Sizeof(TToolInfo);
      uFlags:= TTF_TRACK;
      hwnd:= Handle;
      hInst:= Application.Handle;
      uId:= Handle;
      lpszText:= PChar(Msg);
    end;

  ti.Rect.Left:= Rect.Left;
  ti.Rect.Top:= Rect.Top;
  ti.Rect.Right:= Rect.Right;
  ti.Rect.Bottom:= Rect.Bottom;

  SendMessage(hwnd,TTM_ADDTOOL,1,Integer(@ti));
  SetToolTipTitle(hwnd,Icon,Title);

  SendMessage(hwnd, TTM_TRACKPOSITION, 0, MakeLParam(Point.x,Point.y));

  SendMessage(hwnd, TTM_TRACKACTIVATE, Integer(True), Integer(@ti));
end; 

Rails CSV Export



# require 'rubygems' if using this outside of Rails
require 'fastercsv'

def dump_csv
  @users = User.find(:all, :order => "lastname ASC")
  @outfile = "members_" + Time.now.strftime("%m-%d-%Y") + ".csv"
  
  csv_data = FasterCSV.generate do |csv|
    csv << [
    "Last Name",
    "First Name",
    "Username",
    "Email",
    "Company",
    "Phone",
    "Fax",
    "Address",
    "City",
    "State",
    "Zip Code"
    ]
    @users.each do |user|
      csv << [
      user.lastname,
      user.firstname,
      user.username,
      user.email,
      user.company,
      user.phone,
      user.fax,
      user.address + " " + user.cb_addresstwo,
      user.city,
      user.state,
      user.zip
      ]
    end
  end

  send_data csv_data,
    :type => 'text/csv; charset=iso-8859-1; header=present',
    :disposition => "attachment; filename=#{@outfile}"

  flash[:notice] = "Export complete!"
end

Export french characters

// description of your code here

java -Dfile.encoding=CP850 YourClassName
export JAVA_OPTS=-Dfile.encoding=UTF-8

String Samples

String samples
  
     System.setProperty("file.encoding","UTF-8") 
     System.setProperty("file.encoding","CP850")
     def delimiters = [:]

     //String data = new String((byte[])"é():;éééçç,.<>[]","UTF-8");

     def data =   "é():;éùùçç,.<>[]" 
     
     data = data.replace('é', 'e');
     println data
     println data.size()
     
     data.each { c ->
        // def str = Long.toHexString(c as long)
       def str = c
       //  if (!(c =~ /\w/)) {
         if (c == 'é') {
        	delimiters[c] = "Annul\u00E7e"
         }
         else if(c == 'ù') {
        	 delimiters[c] = "\u00E7"
         }         
         else
         {
        	 delimiters[c] = str
         }
     }
     println delimiters

Misc. terminal command reference


# Creating symlink
ln -s   [destination file/dir]    [link name]

# Import svn 
svn import -m "initial import" . http://svn.cdnm.com/repo

# Untar all .tar files in current directory:
for i in *.tar; do tar -xvzf $i; done

# Creating an archive
$ tar cjf outputfile.tar.bz2 inputs
Extract said archive to the current directory. For a compressed archive, you’ll again need to add the z for a .tar.gz, or j for .tar.bz2.
$ tar xf inputfile.tar
$ tar xjf inputfile.tar.bz2

Helpful localhosting terminal commands

// description of your code here


# Adding vhosts
mate /etc/httpd/httpd.conf
mate /etc/hosts

# Restart Apache
sudo apachectl graceful

# Recursively remove .svn folders
rm -rf `find . -type d -name .svn`

Force traffic over HTTPS

// Force traffic over HTTPS to avoid weird session dropping issues, Also handles addition or removal of www prefix as needed for your security cert,


<IfModule mod_rewrite.c>

RewriteEngine On

  # Force removal of www
  RewriteCond %{HTTP_HOST} ^www\.(.+)$
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # (Or force addition of www depending on your cert)
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

  # Now Force traffic to use HTTPS
  RewriteCond %{SERVER_PORT} !443
  RewriteRule ^(.*)$ https://securesiteurl.com/$1 [R=301,L]

</IfModule>

scp to my journal

// copy entries to journal

// simple snippet that shows taking a command line variable and doing a longer command that includes that variable

#!/bin/bash        
if [ -z "$1" ]; then 
    echo usage: $0 "MMM (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)"
    exit
fi
MMM=$1
scp *.html robnugen.com:$MMM"_journal"