Entries Tagged as 'Scripting'

Managing Drupals Primary Links menus from template.php preprocess functions

You can manage display of primary menu links from your template.php file. If you have used theme(”links”,”$primary_links,array(”class”=>”primary menu”)); Then you can preprocess links from template.php in following manner.

/**
* * @global <type> $language
* @param <type> $links
* @param <type> $attributes
* @return <type>
*/
function phptemplate_links($links, $attributes = array(’class’ => ‘links’)) {
global $language;
$output = ”;

if (count($links) > 0) {
$output = ‘<ul’. drupal_attributes($attributes) .’>’;

$num_links = count($links);
$i = 1;

foreach ($links as $key => $link) {
$class = $key;

// Add first, last and active classes to the list of links to help out themers.
if ($i == 1) {
$class .= ‘ first’;
}
if ($i == $num_links) {
$class .= ‘ last’;
}
//print_r($link['href'].”<br/>”);

if(arg(0)==$link['href']){
$class .= ‘ active-trail’;
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == ‘<front>’ && drupal_is_front_page()))
&& (empty($link['language']) || $link['language']->language == $language->language)) {
$class .= ‘ active’;
}
$output .= ‘<li’. drupal_attributes(array(’class’ => $class)) .’>’;

if (isset($link['href'])) {
// Pass in $link as $options, they share the same keys.
$output .= l($link['title'], $link['href'], $link);
}
else if (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = ”;
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= ‘<span’. $span_attributes .’>’. $link['title'] .’</span>’;
}

$i++;
$output .= “</li>\n”;
}

$output .= ‘</ul>’;
}

return $output;
}


jCalendar: A jQuery Based Calendar Goes The Drupal Way for date type fields jstools drupal5

Jscalender module come with Jstools package in drupal 5 you can download drupal jstools package from the drupal.org site.Jscalender uses the jsCalendar javascript library, like using a special CSS class called via #attribute in the form element array to convert textfields into jcalendar enabled.

The jcalendar module uses hook_form_alter() to look for textfields with the add-jcalendar class, I had to use something different than jcalendar which is used later on the generated form elements by the jQuery code.

This is a sample of how to enable jcalendar from one of your textfields once you’ve installed the module:

To include a jscalendar popup with a textfield, just add the class ‘jscalendar’:(from readme.txt)

$form['date'] = array(
‘#type’ => ‘textfield’,
‘#attributes’ => array(’class’ => ‘jscalendar’),
);

To change the default display and functionality of the calendar, set startup
parameters by adding selectors to your element. The three configurable options
are ‘ifFormat’ (the format of the date/time written to the text field),
’showsTime’ (boolean: should time be displayed on the calendar), and
‘timeFormat’ (values of ‘12′ for 12-hour clock, which is the default, or ‘24′
for 24-hour clock).

Example:
$form['date'] = array(
‘#type’ => ‘textfield’,
‘#attributes’ => array(’class’ => ‘jscalendar’),
// Use only year, month, and day in textfield.
‘#jscalendar_ifFormat’ => ‘%Y-%m-%d’,
// Don’t show time.
‘#jscalendar_showsTime’ => ‘false’,
// Show 24-hour clock.
‘#jscalendar_timeFormat’ => ‘24′,
);

$form['firstdate'] = array(
‘#type’ => ‘textfield’,
‘#title’ => ‘The first date’,
‘#default_value’ => ‘2007-09-29′,
‘#weight’ => -20,
‘#attributes’ => array(’class’ => ‘add-jscalendar’),
‘#description’ => t(’YYYY-MM-DD’),
);

The #jcalendar_year_range custom property allows you to provide a range of years, look at the associative array $years, to show in the calendar. If the property is not passed jcalendar.module defaults to the current year plus 20.

#jcalendar_plot_dates, which is not working yet, should allow passing an array of dates to the calendar to plot. I’m not sure if this should be the job of the Drupal module or jCalendar’s jQuery code.

This first version of jcalendar.module works, it converts your textfield to three select boxes, day, month, year, and a nice table based date picker, easy to theme via CSS, and once you have the right date you can do whatever you want with it. The original textfield is removed later so your form validation and processing functions should consider if jcalendar is enabled (a simple if (module_exists(’jcalendar’))) to handle form elements in a different way.


jquery quick tips to replace javascript code from file learn jquery

The jquery is a simple and quick to use javascript library and provides a
rich set of functionality in just few lines of code.The $ symbol is also set up as
a shortcut for jQuery.If you want the convenience of the $ function for jQuery without colliding with some other use of the global $ function, the jQuery documentation suggests the following idiom:

(function($) {
    // Within this block, $ is a reference to jQuery
    // Neat, and clean
})(jQuery);

Referring elements of document in jquery code.
jQuery(’div.panel’)
    All divs with class=“panel”
jQuery(’p#intro’)
    The paragraph with id=“intro”
jQuery(’div#content a:visible’)
    All visible links inside the div with id=“content”
jQuery(’input[@name=email]‘)
    All input fields with name=“email”
jQuery(’table.orders tr:odd’)
    “odd” numbered rows in a table with class “orders”
jQuery(’a[@href^="http://"]‘)
    All external links (links that start with http://)
jQuery(’p[a]‘)
    All paragraphs that contain one or more links  if (t.val() == title) {
              t.val();
              t.removeClass(‘blur’);

$(‘input:text).hint();

var title = t.attr(‘title’);
    // only apply logic if the element has the attribute
    if (title) {
      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == ) {
          t.val(title);
          t.addClass(‘blur’);
        }
      });
      // on focus, set value to blank if current value 
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val();
          t.removeClass(‘blur’);
        }
      });
t.parents(‘form:first()’).submit(function(){
  if (t.val() == title) {
    t.val();
    t.removeClass(‘blur’);
  }
})

$(‘#search, #username, input.hint’).hint(); // etc
 <!--adsensestart-->

Javascript from flash script Accessing javascript from flash getUrl flash

Creating pop-up browser windows in flash

Flash can trigger a browser pop-up in many ways, this sample explains how to do this by JavaScript.We can use javascript in flash in very simple way.Just create a function in javascript and place that in header.In flash file you have only to use getUrl function for accessing that javascript function.

<!–adsencestart–>

Code to write in the flash file

Create a new flash file, create a simple button in it, click the button and type this code on the flash button:

on (release) {
getURL(“javascript:openNewWindow(’http://www.macromedia.com’,'thewin’,
‘height=400,width=400,toolbar=no,scrollbars=yes’)”);
} Code to write in the html page:-

Now open the html page that contains the swf button and place this code between <head>…</head> area of the html code:

<script language=“JavaScript”>
function openNewWindow(URLtoOpen, windowName, windowFeatures) {
newWindow=window.open(URLtoOpen, windowName, windowFeatures); }
</script>

Now you are done. When the flash button is clicked then the flash button will trigger the JavaScript from the html page.

Options

You can adjust the first code (the on on the flash button) in order to adjust pop-up window options as follows: url to open, window name, height, width, toolbars (yes/no) and scroll bars (yes/no).

or also you have another like choice:-Code to go in html page top

<script language=”javascript”>
function redirect_now()
{
window.location=”yahoo.com”;
}
</script>

Now code for flash button:-

on (release) {
getURL(”javascript:redirect_now()”);
}


Flash and PHP contact form actionscript Creating contact mail form in flash

About php and flash contact form

This is a contact form made in flash and php, it can be used on ‘contact us’ pages.
A flash file contains the form items and when the ’send’ button is pressed it sends the values to mailer.php, this mailer.php sends the email to the receiver.

Few of the benefits of using a flash form instead of a html contact form are:
better user interface
less junk messages (automated scripts cannot send emails through a flash form unless it is decompiled)
Usage details

Upload mailer.php and form.swf on your server in same directory.
In mailer.php you will have to replace ‘email@yourserver.com’ with your email address. Make sure this is an email hosted on that server where you are testing (most server requires this).
Preview

This is a preview of how the contact form looks, you try sending an email to see how form reacts, email will not be sent. It shows a warning if some of the fields are not filled.

<?php
/* —————————
php and flash contact form.
by www.MacromediaHelp.com
—————————
Note: most servers require that one of the emails (sender or receiver)
to be an email hosted by same server, so make sure your email
(on last line of this file) is one hosted on same server.
————————— */

// read the variables form the string, (this is not needed with some servers).
$subject = $_REQUEST["subject"];
$message = $_REQUEST["message"];
$sender = $_REQUEST["sender"];

// include sender IP in the message.
$full_message = $_SERVER['REMOTE_ADDR'] . “\n\n” . $message;
$message= $full_message;

// remove the backslashes that normally appears when entering ” or ‘
$message = stripslashes($message);
$subject = stripslashes($subject);
$sender = stripslashes($sender);

// add a prefix in the subject line so that you know the email was sent by online form
$subject = “Contact form test”. $subject;

// send the email, make sure you replace email@yourserver.com with your email address
if(isset($message) and isset($subject) and isset($sender)){
mail(”email@yourserver.com”, $subject, $message, “From: $sender”);
}?>
Contents of the FLA flash file (ActionScript)
stop();
send_btn.onRelease = function() {
my_vars = new LoadVars();
my_vars.sender = email_box.text;
my_vars.subject = subject_box.text;
my_vars.message = message_box.text;
if (my_vars.sender != “” and my_vars.subject != “” and my_vars.message != “”) {
my_vars.sendAndLoad(”mailer.php”, my_vars, “POST”);
gotoAndStop(2);
} else {
error_clip.gotoAndPlay(2);
}
my_vars.onLoad = function() {
gotoAndStop(3);
};
};
email_box.onSetFocus = subject_box.onSetFocus=message_box.onSetFocus=function () {
if (error_clip._currentframe != 1) {
error_clip.gotoAndPlay(6);
}
};