Friday, December 9, 2011

Adding more parameters to the GET request in the JQuery UI Autocomplete

Working with the JQuery UI Autocomplete in the remote datasource way, my problem begun when I needed to add more parameters when making the call by GET method. Lucky, I found this  page, but in that example the author was using a simple JSON structure:
[{"name":"name 1"},{"name":"name 2"}]

Here is the URL request


As you can see the parameter term is where we send our request. 

By another hand, isn't explained in the JQuery UI Autocomplete how the JSON should be return. 


Here is the structure
 -id
 -label
 -value

Example 
[{"id":"id 1","label":"label 1","value":"value 1"},{"id":"id 2","label":"label 2","value":"value 2"}]

So, knowing these details I made the little change.

$("#searchBox").autocomplete({ 
    source: function(req,add){
        $.getJSON("source.php?parameter1=param1&meter2=param2", req  , function(data){
            var suggestions = [];
            $.each(data,function(i,val){
                suggestions.push(val);
            });
            add(suggestions);
        });
    },
    select: function(event,ui){
        alert("The ID is "+ui.item.id+" the value is "+ui.item.value);
      },
    change: function(){
     $('#searchBox').val('');
    }
  });


Here an example with the parameter clientid added
Here is how must be the structure of the returned JSON

Sunday, November 27, 2011

Taking data submited from ChronoForms in Joomla

Chronoforms is a good extension for building forms in Joomla.
There are good documentation in their site and is very easy to work with it.

But, how to interact with the data sent in the form build with Chronoforms ?

Here is an example.
Let's say that when we are sending an e-amil we want to make some changes if one variable has a value.
See in the picture



We know that for print a variable defined in Chronoforms we call it in this way 

{variable}

But for interact with it we do it in this way

$form->data['variableName']

Create your own Debian or Ubuntu repository and certified by your own *.gpg using Reprepo and Python



We are going to work in a Linux environment using tools like Python, Reprepro and GPG key. 

GPG key
With this we can sign our software and guarantee that we are using software from a trusted site.


Reprepro 
Powerful tool for generate  a Debian repository.

Python-pexpect Python's module for work with interactive applications.

Let's make it by step:

1- Create your own *.gpg key. 

TODO

2- Create a folder and name as packages, here we'll place all the *.deb.

mkdir /home/packages
cd /home/packages

3- Create a folder and name it as conf inside it create a file and call it distributions.

mkdir conf
gedit conf/distributions

We'll do this for distribution  lucid and the component main. Also we'll generate the Packages, Release, .gz y .bz2 files.  SignWith is the variable that says who is going to sign .


distributions file

Codename: lucid
Components: main
Architectures: i386 amd64
Description: My repository
SignWith: abelbmartinez@gmail.com
DebIndices: Packages Release . .gz .bz2

4- Make a folder and name it as repository, there we'll have our new repository.

mkdir /home/repository

5- Create a script file and name it as pycrearepo_lucid.py. Copy and paste all the code below.

pycrearepo_lucid.py file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
[EN]
This Script generate a DEBIAN repository using the tool 'reprepo'.
It works like this, first get all the *.deb from a folder, then obtain 
the size of all these packages, the list from all the sizes is 
ordered descendent, 'reprepro' interact more stable with 
'python-pexpect' adding packages in this way.

[ES]
Este Script genera un repositorio DEBIAN usando la herremienta 'reprepro'. 
La sintaxis es la siguiente, se obtinen todos los *.deb de una carpeta
luego se obtiene el tamaño de estos paquetes, el listado del tamaño de los
paquetes es ordenado de mayor a menor, ya que 'reprepo' trabaja junto con
'python-pexpect' más estable cuando se le adicionan los paquetes de esta manera.

Created on 04.06.2010

@requires: reprepro, python-pexpect

@author: Abel Bolaños Martínez
@contact: abelbmartinez@gmail.com

@author: Oscar Martínez Lopez 
@contact: oscar.martinez@etecsa.cu

@license: Public Domain
'''

import pexpect
import glob
import os
import time

#packages *.deb
packagesSources = "/home/packages/"
distribution = "lucid"
#repositorio
repoMirror = "/home/repository/"
#private key
gpgKeys = "/home/mirrorGPG/"
#Secret phrase from the private key
keyringPhrase = "yourSecretPhrase"


os.chdir(packagesSources)
#get all the *.deb
listPackages = glob.glob("*.deb")
dictSizePackage = dict()
#dict de {size:package}
for package in listPackages:
    dictSizePackage.update({os.path.getsize(package):package})
#sizes
sizes = dictSizePackage.keys()
#sort
sizes.sort()
#backToFront
sizes.reverse()

for size in sizes:
    time.sleep(1.5)#let's wait for 'reprepro' delete the 'lockfile'
    command = pexpect.spawn("reprepro -b . --gnupghome %s --outdir %s --ask-passphrase --waitforlock 2 --keepunusednewfiles includedeb %s %s" % (gpgKeys,repoMirror,distribution,dictSizePackage[size]))
    print " "
    print "Copying the package %s to repository." % dictSizePackage[size]
    i = command.expect(["Please enter passphrase:","Skipping inclusion of","The lock file './db/lockfile' already exists"],timeout=520) 
    if i==0:
        command.sendline(keyringPhrase) 
        print "Added to repository package %s with succeed." % dictSizePackage[size]
    if i==1:
        print 'Package already %s in the repository, not included.' % dictSizePackage[size]
    if i==2:
        print "Attention !!! : Database is locked now. Execute the Script again if the problem persist YOU must delete the file '%sdb/lockfile' and execute the Script again" % packagesSources

Variables packagesSources, distribution, repoMirror, gpgKeys, keyringPhrase must be filled.

  • packagesSources: path to the packages.
  • distribution: distribution to generate.
  • repoMirror: the new repository path.
  • gpgKeys: path to the GPG key for sign the packages.
  • keyringPhrase: secret key phrase.

6- Completed!
7- At least you must create a package for share your public GPG key. Here you can learn how to make a simple deb package.
8- Publish it to the web and test it.

Thursday, November 17, 2011

Mixing Bing ver. 7, JQuery and JQuery UI with a simple search box.

Working with the Bing API is not so difficult but the most important thing is waste sometime reading and looking the examples in the Big documentation. JQuery is another powerful tool for work more easier JavaScript.

In this example we are going to make that while we are writing in a TextBox, indeed is an Autocomplete, it search in the Bing and if there are more than one result we'll show it as a list of the Autocomplete.




Here is the code, for the easier way to test it, I mixed all in the same *.html.

These are the results:





Is mandatory  to use a Key from Bing to test this code. Make yours before start.




 






Monday, August 29, 2011

Creating basic *.deb packages for Debian or Ubuntu

Basic *.deb packages are easy to build, but it can turn more complicated due to the task it should do after or before the install process.

This is a simple  *.deb package.

Name-package_version_architecture/
|
|_DEBIAN/
|      |_control
|
|_Folders that contain the program from the root system/

The folder called DEBIAN is the most important, due to inside it we'll place the scripts for the installation. In this example we only use the control file, which is mandatory in a package.

Let's make this example. Let's say we have this little script called myPC.sh and we want to build it as a package.

Follow this steps:

1.  Make a folder in any place and call it myPC, inside create a folder like this myPC_0.1_all where myPC is the name of the package, 0.1 is the version and all is the architecture, in this example we use all because it doesn't matter if it is i386 or amd64. And the use of the underline(_) is for join the name.

2.  Inside myPC_0.1_all create a folder named as DEBIAN and inside it create a file called control.
Write this in the control file.

Package: myPC
Version: 0.1
Section: MyPrograms
Priority: optional 
Maintainer: Abel Bolaños Martínez <abelbmartinez@gmail.com>
Architecture: all 
Description: My first package
This program show my system name
.
I did it by myself.

The control file has a lot of field but for now we use only these. As you can appreciate we can write in the Description field  an information of our program.

Like this: 

myPC_0.1_all/
|
|_DEBIAN/
        |_control

3.  We want that our script install in /bin folder in our system, that help us to execute the script in any place. So, go to myPC_0.1_all folder and create the bin folder. Inside bin with any text editor create a file called myPC.sh and write this:
uname -a

Save it and open a terminal and change the properties of the file so it can be executed and not modified.
Write this in the terminal:
chmod 755 myPC.sh

We should have something like this:

myPC_0.1_all/
|
|_DEBIAN/
|      |_control
|
|_bin/
     |_myPC.sh

4.  Enough, let's build our package. Open a terminal and placed in myPC folder and type:
dpkg-deb --build myPC_0.1_all/

It's done! you'll see your myPC_0.1_all.deb inside myPC folder.

Now you can install it.