Skip to content

Instantly share code, notes, and snippets.

@arpitr
Created May 8, 2012 14:30
Show Gist options
  • Save arpitr/2635591 to your computer and use it in GitHub Desktop.
Save arpitr/2635591 to your computer and use it in GitHub Desktop.
hook_uc_order()
<?php
/**
* Implementation of hook_ca_action().
*/
function webmobilize_ca_action() {
$order_arg = array(
'#entity' => 'uc_order',
'#title' => t('Order'),
);
$actions['webmobilize_order_email_invoice'] = array(
'#title' => t('Email an order invoice (WebMobilize Custom)'),
'#category' => t('Order'),
'#callback' => 'webmobilize_order_action_email_invoice',
'#arguments' => array(
'order' => $order_arg,
),
);
return $actions;
}
/**
* Email an invoice.
*
* The subject and addresses fields take order token replacements.
*
* @see webmobilize_order_action_email_invoice_form()
*/
function webmobilize_order_action_email_invoice($order, $settings) {
// Token replacements for the subject and body
$settings['replacements'] = array(
'global' => NULL,
'order' => $order,
);
$recipients = array();
$addresses = token_replace_multiple($settings['addresses'], $settings['replacements']);
$sender = token_replace_multiple($settings['from'], $settings['replacements']);
foreach (explode("\n", $addresses) as $address) {
$recipients[] = trim($address);
}
$settings['message'] = theme('uc_order', array('order' => $order, 'op' => $settings['view'], 'template' => $settings['template']));
if (empty($recipients)) {
watchdog('ca', 'Attempted to e-mail an invoice with no recipient.', array(), WATCHDOG_ERROR);
return;
}
foreach ($recipients as $email) {
$sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $settings, empty($sender) ? uc_store_email_from() : $sender);
if (!$sent['result']) {
watchdog('ca', 'Attempt to e-mail invoice for order @order_id to @email failed.', array('@email' => $email, '@order_id' => $order->order_id), WATCHDOG_ERROR);
}
}
}
/**
* @see webmobilize_order_action_email_invoice()
*/
function webmobilize_order_action_email_invoice_form($form_state, $settings = array()) {
$form['from'] = array(
'#type' => 'textfield',
'#title' => t('Sender'),
'#default_value' => isset($settings['from']) ? $settings['from'] : uc_store_email_from(),
'#description' => t('The "From" address.'),
'#required' => TRUE,
);
$form['addresses'] = array(
'#type' => 'textarea',
'#title' => t('Recipients'),
'#default_value' => isset($settings['addresses']) ? $settings['addresses'] : '[order-email]',
'#description' => t('Enter the email addresses to receive the notifications, one on each line. You may use order tokens for dynamic email addresses.'),
'#required' => TRUE,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => $settings['subject'],
'#required' => TRUE,
);
$form['token_help'] = array(
'#type' => 'fieldset',
'#title' => t('Replacement patterns'),
'#description' => t('You can make use of the replacement patterns in the recipients, the subject, and the template file.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
foreach (array('global', 'order') as $name) {
$form['token_help'][$name] = array(
'#type' => 'fieldset',
'#title' => t('@name replacement patterns', array('@name' => drupal_ucfirst($name))),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// TODO Please change this theme call to use an associative array for the $variables parameter.
$form['token_help'][$name]['content'] = array(
'#value' => theme('token_help', $name),
);
}
$form['template'] = array(
'#type' => 'select',
'#title' => t('Invoice template'),
'#description' => t('Select the invoice template to use for this email.'),
'#options' => uc_order_template_options(),
'#default_value' => $settings['template'],
);
$form['view'] = array(
'#type' => 'radios',
'#title' => t('Included information'),
'#options' => array(
'print' => t('Show the business header and shipping method.'),
'admin-mail' => t('Show all of the above plus the help text, email text, and store footer.'),
'checkout-mail' => t('Show all of the above plus the "thank you" message.'),
),
'#default_value' => $settings['view'],
);
return $form;
}
function webmobilize_uc_order($op, $order, $arg2) {
global $user;
$lang = LANGUAGE_NONE;
$user = user_load($order->uid);
$username = $user->name;
$nids = array(175, 174, 173);
foreach ($order->products as $product) {
if (in_array($product->nid, $nids)) {
// initiate website creation
//$name = $form_state['values']['user_name'];
$name = $order->billing_first_name;
//$desiredDomain = $form_state['values']['desired_domain_name'];
$id = str_rand(5);
$domain = sprintf("%s.webmobilize.mobi", $id);
$passwordWebsite = $form_state['values']['password'];
$password = str_rand(20);
$email = $order->primary_email;
$dbname = sprintf("%s_%s_%s", $order->uid, $product->nid, $id);
$queryBuild = sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'localhost' IDENTIFIED BY '%s' ", $dbname, $dbname, $password);
$query = db_query($queryBuild);
$result = $query->execute();
$command = sprintf('drush site-install standard --yes --db-url=mysql://%s:%s@localhost:3306/%s --account-name=%s --account-pass=%s --account-mail=%s --site-name=%s --sites-subdir=%s', $dbname, $password, $dbname, $name, $passwordWebsite, $email, $domain, $domain);
exec($command);
$node = new StdClass();
$node->type = 'client_history';
$node->status = 1;
$node->title = $username;
$node->field_user_reference[$lang][0] = $username;
$node->field_history[$lang][0]['value'] = $desiredDomain;
$node = node_submit($node);
node_save($node);
$symlinkDestinationLocation = sprintf("/var/www/newProject/webmobi/sites/%s", $desiredDomain);
$symlinkTargetLocation = sprintf("/var/www/newProject/webmobi/sites/%s", $domain);
symlink($symlinkTargetLocation, $symlinkDestinationLocation);
$editHosts = "/etc/hosts";
if (!($fp = fopen($editHosts, "r+")))
exit("unable to open the file");
$buffer = fread($fp, filesize($editHosts));
$add = sprintf("127.0.0.1 %s", $desiredDomain);
$add.= sprintf("\n #flag\n");
$new = str_replace("#flag", $add, $buffer);
rewind($fp);
fwrite($fp, "$new");
fclose($fp);
$runScript = "/var/www/siteScript.sh";
if (!($fp = fopen($runScript, "w+")))
exit("unable to open the script file");
$bufferScript = fread($fp, filesize($runScript));
$addScript = sprintf("#!/bin/bash\n");
$addScript.= sprintf("a2ensite %s", $desiredDomain);
$addScript.= sprintf("\n sudo service apache2 reload");
fwrite($fp, "$addScript");
fclose($fp);
$editVirtualHost = sprintf("/etc/apache2/sites-available/%s", $desiredDomain);
if (!($fp = fopen($editVirtualHost, "a+")))
exit("unable to open the file");
$a = sprintf("\n<VirtualHost 127.0.0.1:80>");
$a.= "\n";
$a.= sprintf(" DocumentRoot /var/www/newProject/drupal_multisite");
$a.= sprintf("\n ServerName %s", $desiredDomain);
$a.= sprintf("\n ServerAlias %s", $domain);
$a.= sprintf("\n</VirtualHost>");
fwrite($fp, "$a");
fclose($fp);
exec("sudo /var/www/siteScript.sh");
}
}
}
function str_rand($length = 8, $seeds = 'alphanum') {
// Possible seeds
$seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
// Choose seed
if (isset($seedings[$seeds])) {
$seeds = $seedings[$seeds];
}
// Seed generator
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
mt_srand($seed);
// Generate
$str = '';
$seeds_count = strlen($seeds);
for ($i = 0; $length > $i; $i++) {
$str .= $seeds{mt_rand(0, $seeds_count - 1)};
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment