Commit 1c58fefe authored by theo lem's avatar theo lem
Browse files

Translatable module

parent 1127d0ad
......@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Number of forms / user limit
* Additional filter/check on user email
* Variables can now be reset from the adminstration pannel
* `translations/` subfolder to all custom modules, containing `.pot` and French `.po` file to allow translation.
#### New modules
* Anti-spam module `framaforms_spam` that put forms in quarantine containing suspicious words in title, or don't have any webform component. (Needs work)
......@@ -26,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
* the notification email is now loaded from an external template.
* removed unused and unsecure `webform_report` module.
* Usage of `t()` function and other system functions to allow translation.
### Fixed
* Empty "Share" tab on forms (#106)
......
......@@ -9,12 +9,12 @@ dependencies[] = webform
dependencies[] = webform_confirm_email
dependencies[] = webform_validation
dependencies[] = webform_share
dependencies[] = webform_chart
dependencies[] = webform_charts
dependencies[] = webform_calculator
dependencies[] = webform_tokens
dependencies[] = views
dependencies[] = views_bulk_operations
dependencies[] = views_access_callbacks
dependencies[] = views_access_callback
dependencies[] = form_builder
dependencies[] = pathauto
......@@ -31,4 +31,4 @@ configure = admin/config/system/framaforms
version = "7.x-1.0"
core = "7.x"
project = "framaforms"
datestamp = "1382691029"
\ No newline at end of file
datestamp = "1382691029"
<?php
/**
* @file
* Framaforms module install.
*/
* @file
* Framaforms module install.
*/
/**
* Implements hook_schema();
*/
function framaforms_schema(){
$schema['framaforms_expired'] = array(
'description' => 'Table containing expiration information for Framaforms webforms',
'fields' => array(
'nid' => array(
'description' => 'NID of the webform (foreign key)',
'type' => 'int',
'not null' => FALSE,
),
'date_notified' => array(
'description' => 'Date the user was notified (using the associated email).',
'type' => 'int',
),
'notified' => array(
'description' => 'Set to 1 if the user was notified (default 0).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'unique keys' => array(
'nid' => array(
'nid'
)
),
'foreign key'=>array(
'fk_name_node' => array(
'table'=>'node',
'column'=>array('nid'=>'nid')
),
),
);
return $schema;
* Implements hook_schema();.
*/
function framaforms_schema() {
$schema['framaforms_expired'] = array(
'description' => 'Table containing expiration information for Framaforms webforms',
'fields' => array(
'nid' => array(
'description' => 'NID of the webform (foreign key)',
'type' => 'int',
'not null' => FALSE,
),
'date_notified' => array(
'description' => 'Date the user was notified (using the associated email).',
'type' => 'int',
),
'notified' => array(
'description' => 'Set to 1 if the user was notified (default 0).',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'unique keys' => array(
'nid' => array(
'nid',
),
),
'foreign key' => array(
'fk_name_node' => array(
'table' => 'node',
'column' => array('nid' => 'nid'),
),
),
);
return $schema;
}
/**
* Implements hook_install
* Implements hook_install.
*/
function framaforms_install () {
framaforms_set_default_variables();
function framaforms_install() {
framaforms_set_default_variables();
}
/**
* Implements hook_uninstall
* Implements hook_uninstall.
*/
function framaforms_uninstall () {
db_delete('variable')->condition('name', db_like('framaforms_') . '%', 'LIKE')->execute();
cache_clear_all('framaforms', 'cache', TRUE);
function framaforms_uninstall() {
db_delete('variable')->condition('name', db_like('framaforms_') . '%', 'LIKE')->execute();
cache_clear_all('framaforms', 'cache', TRUE);
}
/**
* Set persistent variables regarding mails sent by Framaforms
* Set persistent variables regarding mails sent by Framaforms.
*
* @return void
*/
function framaforms_set_variables_mail () {
// sprintf-ready mail for notifying a user of his⋅her form expiration.
$file_path = drupal_get_path('module', 'framaforms'). '/includes/templates/notification_email_fr.txt';
$framaforms_mail_user_notification_body = t("file_get_contents($file_path)");
$framaforms_mail_user_notification_subject = t("Framaforms : Votre formulaire « [form_title] » expirera dans deux semaines.");
variable_set('framaforms_mail_user_notification_subject', $framaforms_mail_user_notification_subject);
variable_set('framaforms_mail_user_notification_body', $framaforms_mail_user_notification_body);
function framaforms_set_variables_mail() {
// sprintf-ready mail for notifying a user of his⋅her form expiration.
$file_path = drupal_get_path('module', 'framaforms') . '/includes/templates/notification_email_fr.txt';
$framaforms_mail_user_notification_body = file_get_contents($file_path);
$framaforms_mail_user_notification_subject = t("Framaforms : your form « [form_title] » will expire soon.");
variable_set('framaforms_mail_user_notification_subject', $framaforms_mail_user_notification_subject);
variable_set('framaforms_mail_user_notification_body', $framaforms_mail_user_notification_body);
}
function framaforms_set_variables_pages () {
$expiration_page_content = "<p>Le formulaire que vous essayez de voir a expiré.</p><p>Si vous êtes l'auteur de ce formulaire, vous pouvez peut-être le réactiver en vous identifiant, puis <strong>en éditant sa date d'expiration</strong> (en cliquant sur <b>Modifier</b> > <b>Autres options</b>) après avoir cliqué sur le lien <a href='[my_forms_url]'>Mes formulaires.</a></p><p>Si vous n'êtes pas l'auteur, contactez-le, en utilisant le moyen que l'auteur a utilisé pour vous enjoindre à remplir son formulaire (mail, réseau social, autres) et signalez-lui que son formulaire est expiré.</p>";
variable_set('framaforms_expiration_page_content', $expiration_page_content);
/**
*
*/
function framaforms_set_variables_pages() {
$language = language_default(); // the site default language
$file_path = drupal_get_path('module', 'framaforms') . "/includes/html/expiration_page_{$language}.html";
$expiration_page_content = file_get_contents($file_path);
variable_set('framaforms_expiration_page_content', $expiration_page_content);
}
/**
* Sets global variables to default value
* Sets global variables to default value.
*
* @return
*/
function framaforms_set_default_variables () {
module_load_include('inc', 'framaforms', 'includes/framaforms.pages');
function framaforms_set_default_variables() {
module_load_include('inc', 'framaforms', 'includes/framaforms.pages');
framaforms_set_variables_pages();
framaforms_set_variables_mail();
framaforms_set_variables_pages();
framaforms_set_variables_mail();
$now = date('Y/m/d/ h:m:s', time());
variable_set('framaforms_cron_last', new DateTime()); // the last framforms cron run
variable_set('framaforms_cron_frequency', '1 hour');
variable_set('framaforms_expiration_period_default_value', 26); // default creation-expiration time interval
variable_set('framaforms_deletion_period_value', 9); // default expiration-deletion time interval
variable_set('framaforms_notification_period_value', 2); // default notification-expiration time interval
variable_set('framaforms_site_contact_url', 'https://contact.framasoft.org/#framaforms');
variable_set('framaforms_forms_per_user_limit', 200);
/* various other custom manipulation */
/* Hide Drupal default banner */
$theme_settings = variable_get('theme_settings');
$theme_settings['toggle_logo'] = 0;
$theme_settings['toggle_slogan'] = 0;
$theme_settings['toggle_name'] = 0;
variable_set('theme_settings', $theme_settings);
$now = date('Y/m/d/ h:m:s', time());
// The last framforms cron run.
variable_set('framaforms_cron_last', new DateTime());
variable_set('framaforms_cron_frequency', '1 hour');
// Default creation-expiration time interval.
variable_set('framaforms_expiration_period_default_value', 26);
// Default expiration-deletion time interval.
variable_set('framaforms_deletion_period_value', 9);
// Default notification-expiration time interval.
variable_set('framaforms_notification_period_value', 2);
variable_set('framaforms_site_contact_url', 'https://contact.framasoft.org/#framaforms');
variable_set('framaforms_forms_per_user_limit', 200);
/* various other custom manipulation */
/* Hide Drupal default banner */
$theme_settings = variable_get('theme_settings');
$theme_settings['toggle_logo'] = 0;
$theme_settings['toggle_slogan'] = 0;
$theme_settings['toggle_name'] = 0;
variable_set('theme_settings', $theme_settings);
/* Set default favicon for Frama theme */
$theme_frama_settings = variable_get('theme_frama_settings');
$theme_frama_settings['favicon_path'] = drupal_get_path('theme', 'frama'). '/images/Framasoft_Logo.png';
variable_set('theme_frama_settings', $theme_frama_settings);
/* Set default favicon for Frama theme */
$theme_frama_settings = variable_get('theme_frama_settings');
$theme_frama_settings['favicon_path'] = drupal_get_path('theme', 'frama') . '/images/Framasoft_Logo.png';
variable_set('theme_frama_settings', $theme_frama_settings);
/* Enable webform option for the form1 type */
variable_set('webform_node_form1', 1);
/* Enable webform option for the form1 type */
variable_set('webform_node_form1', 1);
/* Pathauto configuration */
$form1_settings = variable_get('pathauto_node_form1_pattern');
$page_settings = variable_get('pathauto_node_page_pattern');
/* Pathauto configuration */
$form1_settings = variable_get('pathauto_node_form1_pattern');
$page_settings = variable_get('pathauto_node_page_pattern');
if(!isset($form1_settings)){
variable_set('pathauto_node_form1_pattern', '[node:title]-[node:created:raw]');
}
if(!isset($page_settings)){
variable_set('pathauto_node_page_pattern', '[node:title]');
}
if (!isset($form1_settings)) {
variable_set('pathauto_node_form1_pattern', '[node:title]-[node:created:raw]');
}
if (!isset($page_settings)) {
variable_set('pathauto_node_page_pattern', '[node:title]');
}
}
function framaforms_update_7110 (&$sandbox) {
$file_path = drupal_get_path('module', 'framaforms'). '/includes/templates/notification_email_fr.txt';
$framaforms_mail_user_notification_body = file_get_contents($file_path);
variable_set('framaforms_mail_user_notification_body', $framaforms_mail_user_notification_body);
}
\ No newline at end of file
/**
*
*/
function framaforms_update_7110(&$sandbox) {
$file_path = drupal_get_path('module', 'framaforms') . '/includes/templates/notification_email_fr.txt';
$framaforms_mail_user_notification_body = file_get_contents($file_path);
variable_set('framaforms_mail_user_notification_body', $framaforms_mail_user_notification_body);
}
<?php
/**
* Contains Framaforms custom function related to block contents
* @file
* Contains Framaforms custom function related to block contents.
*/
/**
* Returns the content for the personal contact form block.
*
* @param int $nid : the current page nid
* @param int $nid
* : the current page nid.
*
* @return string : the markup content to be rendered.
*/
function get_contact_form_content ($nid) {
$current_path = current_path();
$content = "";
try{
if(isset($nid)){
$query_str = sprintf("SELECT users.name, users.uid FROM node INNER JOIN users ON node.uid = users.uid WHERE node.nid=%s;", $nid);
$results = db_query($query_str)->fetchAssoc();
$name = $results ? $results['name'] : NULL;
$uid = $results ? $results['uid'] : NULL;
function get_contact_form_content($nid) {
$content = "";
try {
if (isset($nid)) {
$query_str = sprintf("SELECT users.name, users.uid FROM node INNER JOIN users ON node.uid = users.uid WHERE node.nid=%s;", $nid);
$results = db_query($query_str)->fetchAssoc();
$name = $results ? $results['name'] : NULL;
$uid = $results ? $results['uid'] : NULL;
$query_str = sprintf("SELECT users.data FROM node INNER JOIN users ON node.uid = users.uid WHERE node.nid=%s;", $nid);
$results = db_query($query_str)->fetchAssoc();
$contact = unserialize($results['data'])['contact'];
}
if($nid && isset($name) && isset($uid) && $contact == 1 ){
$url_contact = '/' .drupal_get_path_alias('user/'.$uid) .'/contact';
$content .= t("<h3>Contacter l'auteur du formulaire</h3>");
$content .= sprintf("<p> Pour contacter l'auteur du formulaire, <a href='%s'> cliquez ici</a><p>", $url_contact);
$content .= "<br>";
}
}
catch(Exception $e){
watchdog('framaforms', 'Erreur dans le block de formulaire de contact (modules/framaforms/includes/framaforms.block.inc)' . $e);
}
return $content;
$query_str = sprintf("SELECT users.data FROM node INNER JOIN users ON node.uid = users.uid WHERE node.nid=%s;", $nid);
$results = db_query($query_str)->fetchAssoc();
$contact = unserialize($results['data'])['contact'];
}
if ($nid && isset($name) && isset($uid) && $contact == 1) {
$url_contact = '/' . drupal_get_path_alias('user/' . $uid) . '/contact';
$content .= "<h3>" . t("Contact the author of this form") . "</h3>";
$content .= "<p>" . t("To contact the author of this form, <a href='@link'> click here</a>", array('@link' =>$url_contact)) . "<p>" ;
$content .= "<br>";
}
}
catch (Exception $e) {
watchdog('framaforms', 'Error in the contact form block (modules/framaforms/includes/framaforms.block.inc) : %error', array('%error' => $e), WATCHDOG_WARNING);
}
return $content;
}
/**
* Returns true if the node whose NID is passed is of type form1
* Returns true if the node whose NID is passed is of type form1.
*
* @param int $nid : the NID of the node
* @return void
* @param int $nid
* : the NID of the node.
*
* @return boolean : TRUE if the node is a form1 node
*/
function node_is_form1($nid){
if(!isset($nid)){
function node_is_form1($nid) {
if (!isset($nid)) {
return FALSE;
}
$node = node_load($nid);
if(isset($node->type) && $node->type == 'form1'){
return TRUE;
}
return FALSE;
$node = node_load($nid);
if (isset($node->type) && $node->type == 'form1') {
return TRUE;
}
return FALSE;
}
<?php
/**
* @file
* Contain Framaforms custom function related to automatic expiration of user
* webforms.
*/
/**
* Get all webforms that will expired between now and 2 weeks in the future,
* and insert them into framaforms_expired table.
* @return void
*/
function make_forms_expire(){
$expiration_period = variable_get('framaforms_notification_period_value');
// If the user set the expiration period to 0, don't make the nodes expire.
if($expiration_period == 0){
return;
}
$expiration_period .= ' weeks';
$transaction = db_transaction();
try{
// Select nodes expiring within the two next weeks.
// Limit query to batch of 1000 rows in order to avoid errors
db_query(sprintf("
* Get all webforms that will expired between now and 2 weeks in the future,
* and insert them into framaforms_expired table.
*
* @return void
*/
function make_forms_expire() {
$expiration_period = variable_get('framaforms_notification_period_value');
// If the user set the expiration period to 0, don't make the nodes expire.
if ($expiration_period == 0) {
return;
}
$expiration_period .= ' weeks';
$transaction = db_transaction();
try {
// Select nodes expiring within the two next weeks.
// Limit query to batch of 1000 rows in order to avoid errors.
db_query(sprintf("
INSERT INTO framaforms_expired (nid)
(SELECT n.nid
FROM node n
......@@ -33,16 +35,16 @@
(SELECT nid FROM framaforms_expired)
LIMIT 1000)
ON CONFLICT DO NOTHING;
",$expiration_period));
}
catch(Exception $e){
$transaction->rollback();
watchdog('framaforms', $e);
}
/* Get all the webforms that are about to expire and whose owner wasn't yet notified */
try{
$results = db_query("
", $expiration_period));
}
catch (Exception $e) {
$transaction->rollback();
watchdog('framaforms', "There was an error inserting expired forms into framaforms_expired : %error", array('%error' => $e), WATCHDOG_WARNING);
}
/* Get all the webforms that are about to expire and whose owner wasn't yet notified */
try {
$results = db_query("
SELECT fex.nid, u.mail, u.uid, n.title
FROM framaforms_expired fex
INNER JOIN node n
......@@ -51,79 +53,83 @@
ON u.uid = n.uid
WHERE fex.notified = 0
LIMIT 1000;
") ;
// Notify the users of their forms expiration
watchdog('framaforms', "Notifying users of their expired forms.");
foreach ($results as $record){
$user_notified = notify_user_of_expiration($record);
// Update the database with the notification date and set notified to 1
if($user_notified){
db_update('framaforms_expired')
->fields(array(
'notified' => 1,
'date_notified' => time()
))
->condition('nid', $record->nid, '=')
->execute();
continue;
}
watchdog('framaforms', t("Error while trying to notify the user : the user's email could be badly configured."), array(), WATCHDOG_WARNING);
");
// Notify the users of their forms expiration.
watchdog('framaforms', "Notifying users of their expired forms.", array(), WATCHDOG_NOTICE);
foreach ($results as $record) {
$user_notified = notify_user_of_expiration($record);
// Update the database with the notification date and set notified to 1.
if ($user_notified) {
db_update('framaforms_expired')
->fields(array(
'notified' => 1,
'date_notified' => time(),
))
->condition('nid', $record->nid, '=')
->execute();
continue;
}
watchdog('framaforms', "Notified all users of their expired forms.");
}
catch(Exception $e){
$transaction->rollback();
watchdog('framaforms', "There was an error notifying users.");
watchdog('framaforms', $e);
}
}
/**
* Sends an email by calling drupal_mail to a user,
* notifying them of the upcoming webform expiration.
*
* @param DatabaseStatementInterface $record : Single database statement containing node and user information
* @param string $expiration_period : notification-expiration time interval
* @param string $deletion_period : expiration-deletion time interval
* @return void
*/
function notify_user_of_expiration ($record) {
global $base_url; //use the global variable of Drupal for the site's URL
// First check if the user has a correct email
if(!filter_var($record->mail, FILTER_VALIDATE_EMAIL)) {
watchdog('framaforms', t("Invalid email for user ") .$record->uid, array(), WATCHDOG_WARNING);
return FALSE;
watchdog('framaforms', "Error while trying to notify the user : the user's email could be badly configured.", array(), WATCHDOG_WARNING);
}
watchdog('framaforms', "Notified all users of their expired forms.", array(), WATCHDOG_NOTICE);
}
catch (Exception $e) {
$transaction->rollback();
watchdog('framaforms', "There was an error notifying users : %error", array('%error' => $e), WATCHDOG_WARNING);
}
}
$expiration_period = variable_get('framaforms_notification_period_value') .' weeks';
$deletion_period = variable_get('framaforms_deletion_period_value') .' weeks';
$expiration_date = new DateTime();
$expiration_date->modify("+".$expiration_period);
$delete_date = clone $expiration_date;
$delete_date->modify($deletion_period);
$delete_date = date("d/m/Y", $delete_date->getTimestamp());
$expiration_date = date("d/m/Y", $expiration_date->getTimestamp());
$node_url = $base_url . url('node/'.$record->nid);
$mail_body = variable_get('framaforms_mail_user_notification_body');
$mail_subject = variable_get('framaforms_mail_user_notification_subject');
// replace placeholders in the body and subject.
$placeholders = ['[form_title]', '[form_expires_on]', '[form_delete_on]', '[form_url]'];
$replacers = [$record->title, $expiration_date, $delete_date, $node_url];
$mail_subject = str_replace($placeholders, $replacers, $mail_subject);
$mail_body = str_replace($placeholders, $replacers, $mail_body);
$mail_params = array(
'body' => $mail_body,
'subject' => $mail_subject,
'headers' => array(),
);
$message = drupal_mail('framaforms', 'expired_webform', (string)$record->mail, user_preferred_language($user), $mail_params);
return TRUE;
/**
* Sends an email by calling drupal_mail to a user,
* notifying them of the upcoming webform expiration.
*
* @param DatabaseStatementInterface $record
* : Single database statement containing node and user information.
* @param string $expiration_period
* : notification-expiration time interval.
* @param string $deletion_period
* : expiration-deletion time interval.
*
* @return void
*/
function notify_user_of_expiration($record) {
// Use the global variable of Drupal for the site's URL.
global $base_url;
// First check if the user has a correct email.
if (!filter_var($record->mail, FILTER_VALIDATE_EMAIL)) {
watchdog('framaforms', "Invalid email for user %uid", array('%uid' => $record->uid), WATCHDOG_WARNING);
return FALSE;
}
$expiration_period = variable_get('framaforms_notification_period_value') . ' weeks';
$deletion_period = variable_get('framaforms_deletion_period_value') . ' weeks';
$expiration_date = new DateTime();
$expiration_date->modify("+" . $expiration_period);
$delete_date = clone $expiration_date;
$delete_date->modify($deletion_period);
$delete_date = date("d/m/Y", $delete_date->getTimestamp());
$expiration_date = date("d/m/Y", $expiration_date->getTimestamp());
$node_url = $base_url . url('node/' . $record->nid);
$mail_body = variable_get('framaforms_mail_user_notification_body');
$mail_subject = variable_get('framaforms_mail_user_notification_subject');
// Replace placeholders in the body and subject.
$placeholders = ['[form_title]', '[form_expires_on]', '[form_delete_on]', '[form_url]'];
$replacers = [$record->title, $expiration_date, $delete_date, $node_url];
$mail_subject = str_replace($placeholders, $replacers, $mail_subject);
$mail_body = str_replace($placeholders, $replacers, $mail_body);