Created
June 24, 2016 11:43
-
-
Save isholgueras/4d982da795c1973f316b8e557e71cbb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Fichero de funcionalidad de módulo de lowercase. | |
*/ | |
define("LOWERCASE_BY_DEFAULT", 0); | |
define("LOWERCASE_CACHE_PREFIX", "idn_lw1_"); | |
define("LOWERCASE_CACHE_PREFIX_2", "idn_lw2_"); | |
/** | |
* Implements hook_menu(). | |
*/ | |
function idn_lowercase_menu() { | |
// Sección de configuración. | |
$items['admin/config/idealista/idn-lowercase'] = array( | |
'title' => 'Configuración de minúsculas', | |
'description' => 'Configurar la conversión a minúsculas.', | |
'access arguments' => array('administer site configuration'), | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('idn_lowercase_admin_form'), | |
'file' => 'idn_lowercase.admin.inc', | |
); | |
return $items; | |
} | |
function idn_lowercase_is_active() { | |
if (variable_get('idn_lowercase_active', LOWERCASE_BY_DEFAULT) == 1 && | |
!(path_is_admin(current_path())) | |
) { | |
return TRUE; | |
} | |
return FALSE; | |
} | |
/* | |
* Devuelve el markup de un $element pasándo el output por idntolower si está habilitado. | |
* | |
* Se suele usar en llamadas desde un #pre_render de un $element, actualizándose el #markup | |
* de dicho $element con lo devuelto por esta función. | |
* Podemos ver en este enlace (en el min 22 aprox) cómo se usa el #pre_render. | |
* Digamos que esta función es la que obtiene el markup llamándose desde el pre_render | |
* | |
* @see http://asociaciondrupal.es/es/sesion-sobre-render-api-en-drupal-7 | |
*/ | |
function idn_lowercase_render_element_markup($element) { | |
$output = drupal_render($element); | |
if (idn_lowercase_is_active()) { | |
$output = idntolower($output); | |
} | |
return $output; | |
} | |
/** | |
* Implements of hook_preprocess_page(). | |
*/ | |
function idn_lowercase_preprocess_page(&$variables) { | |
if (idn_lowercase_is_active()) { | |
drupal_add_css(drupal_get_path('module', 'idn_lowercase') . '/css/idn_lowercase.css'); | |
} | |
} | |
/** | |
* Implements of hook_preprocess_field(). | |
*/ | |
function idn_lowercase_preprocess_field(&$variables) { | |
if (idn_lowercase_is_active()) { | |
foreach ($variables['items'] as $key => $item) { | |
if (isset($variables['items'][$key]['#markup'])) { | |
$variables['items'][$key]['#markup'] = idntolower($variables['items'][$key]['#markup']); | |
} | |
} | |
} | |
} | |
/** | |
* Implements of hook_preprocess_block(). | |
*/ | |
function idn_lowercase_preprocess_block(&$variables) { | |
if (idn_lowercase_is_active()) { | |
$excluded_blocks = array( | |
'block-system-main', | |
'block-idn-smart-megabanner', | |
'block-idn-smart-roba', | |
'block-idn-smart-boton', | |
'block-idn-xiti-xiti-page-markup', | |
); | |
// Lowercase the title. | |
$subject = idntolower($variables['block']->subject); | |
$variables['block']->subject = $subject; | |
// Lowercase the body of the block. | |
if (!in_array($variables['block_html_id'], $excluded_blocks)) { | |
$content = idntolower($variables['content']); | |
$variables['content'] = $content; | |
} | |
} | |
} | |
/** | |
* Implements of hook_preprocess_link(). | |
*/ | |
function idn_lowercase_preprocess_link(&$variables) { | |
if (idn_lowercase_is_active()) { | |
$variables['text'] = idntolower($variables['text']); | |
} | |
} | |
/** | |
* Implements of hook_preprocess_link(). | |
*/ | |
function idn_lowercase_views_pre_render(&$view) { | |
$is_in_msn_view = strpos($view->current_display, "msn") !== FALSE; | |
if (idn_lowercase_is_active() && !$is_in_msn_view) { | |
foreach ($view->result as $key => $result) { | |
foreach ($result as $name => $field) { | |
if (in_array($name, fields_to_lower()) && gettype($field) == 'string') { | |
$view->result[$key]->{$name} = idntolower($view->result[$key]->{$name}); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Funcion para poner a minúsculas y quitar el último punto y a parte. | |
* | |
* @param string $text | |
* Texto a poner a minúsculas | |
* | |
* @return string | |
* Texto lowercased | |
*/ | |
function idntolower($text) { | |
$chopped_text = $lowered_text = $text; | |
if (idntolower_has_uppercase_letters($text)) { | |
// Check if HTML. | |
if (idntolower_has_HTML($text)) { | |
$chopped_text = idntolower_remove_VBscript_tag($text); | |
$chopped_text = idntolower_remove_script_tag($chopped_text); | |
$chopped_text = idntolower_remove_HTML($chopped_text); | |
} | |
$chopped_text = idntolower_remove_URLs($chopped_text); | |
$chopped_text = strtolower($chopped_text); | |
$lowered_text = idntolower_merge_strings($text, $chopped_text); | |
} | |
if (strtolower($text) != strtolower($lowered_text)) { | |
$lowered_text = $text; | |
} | |
$lowered_text = remove_last_dot($lowered_text); | |
return $lowered_text; | |
} | |
/** | |
* Funcion que convierte un texto a un array. | |
* | |
* @param string $text | |
* Texto a convertir a array | |
* | |
* @return array | |
* Array del texto. | |
*/ | |
function idntolower_prepare_array($text) { | |
return str_split($text); | |
} | |
/** | |
* Comprueba si hay mayúsculas en un string. | |
* | |
* @param string $text | |
* Texto a comprobar | |
* | |
* @return bool | |
* TRUE|FALSE si detecta mayúsculas. | |
*/ | |
function idntolower_has_uppercase_letters($text) { | |
return strip_tags($text) != strtolower(strip_tags($text)); | |
} | |
/** | |
* Comprueba si hay HTML en un string. | |
* | |
* @param string $text | |
* Texto a comprobar | |
* | |
* @return bool | |
* TRUE|FALSE si detecta HTML. | |
*/ | |
function idntolower_has_HTML($text) { | |
return preg_match("/<(\\/)*\\s*\\w.*?>/", $text) == 1; | |
} | |
/** | |
* Elimina completamente el tag script, incluyendo el contenido | |
* y las cambia por espacios. | |
* | |
* @param string $text | |
* Texto a borrar el script | |
* | |
* @return string | |
* Texto sin script. | |
*/ | |
function idntolower_remove_script_tag($text) { | |
return preg_replace_callback("/(<script[\\s\\S]*?>[\\s\\S]*?<\\/script>)*/", "idntolower_replace_with_empty_string", $text, -1); | |
} | |
/** | |
* Elimina completamente el tag de vbscript, incluido en el newsletter, | |
* y las cambia por espacios. | |
* | |
* @param string $text | |
* Texto a borrar el vbscript | |
* | |
* @return string | |
* Texto sin vbscript. | |
*/ | |
function idntolower_remove_VBscript_tag($text) { | |
return preg_replace_callback("/(<%[\\s\\S]*?[\\s\\S]*?%>)*/", "idntolower_replace_with_empty_string", $text, -1); | |
} | |
/** | |
* Elimina todos los tags HTML que encuentra, SIN incluir el contenido, | |
* y las cambia por espacios. | |
* | |
* @param string $text | |
* Texto a borrar el tag HTML | |
* | |
* @return string | |
* Texto sin HTML. | |
*/ | |
function idntolower_remove_HTML($text) { | |
return preg_replace_callback("/<(\\/)*\\s*\\w.*?>/", "idntolower_replace_with_empty_string", $text, -1); | |
} | |
/** | |
* Elimina todas las URLs que encuentra y las cambia por espacios. | |
* | |
* @param string $text | |
* Texto a borrar el tag HTML | |
* | |
* @return string | |
* Texto sin URLs. | |
*/ | |
function idntolower_remove_URLs($text) { | |
return preg_replace_callback("/((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s<\"]*))/is", "idntolower_replace_with_empty_string", $text, -1); | |
} | |
/** | |
* Devuelve un string vacío con la misma longitud enviado por argumento. | |
* | |
* @param array $matches | |
* Matches obtenidos en el preg_match o preg_replace_callback. | |
* | |
* @return string | |
* String vacío de la longitud de $matches[0]. | |
*/ | |
function idntolower_replace_with_empty_string($matches) { | |
return idntolower_get_emtpy_string(strlen($matches[0])); | |
} | |
/** | |
* Devuelve un string vacío con una longitud pasada por argumento. | |
* | |
* @param int $length | |
* Longitud del array vacío. | |
* | |
* @return string | |
* String vacío de la longitud de $length. | |
*/ | |
function idntolower_get_emtpy_string($length) { | |
return str_pad("", $length, " "); | |
} | |
/** | |
* Mezcla dos strings sobreescribiendo todo menos espacios. | |
* | |
* @param string $original_string | |
* String original. | |
* @param string $chopped_string | |
* String reducido sin html, script,... | |
* | |
* @return string | |
* String final manteniendo todo menos las mayúsculas que no corresponden. | |
*/ | |
function idntolower_merge_strings($original_string, $chopped_string) { | |
$new_chopped_array = idntolower_merge_arrays($chopped_string, $original_string); | |
$lowered_text = implode($new_chopped_array); | |
return $lowered_text; | |
} | |
/** | |
* Mezcla dos arrays de strings. | |
* | |
* @param string $chopped_string | |
* String reducido sin html, script,... | |
* @param string $original_string | |
* Array de string original. | |
* | |
* @return array | |
* Array de string final manteniendo todo menos las mayúsculas. | |
*/ | |
function idntolower_merge_arrays($chopped_string, $original_string) { | |
$chopped_array = idntolower_prepare_array($chopped_string); | |
$original_array = idntolower_prepare_array($original_string); | |
$final_array = array(); | |
foreach ($chopped_array as $key => $item) { | |
$final_array[$key] = ($item !== ' ') ? $item : $original_array[$key]; | |
} | |
return $final_array; | |
} | |
/** | |
* Borra el último punto y aparte de un texto enviado. | |
* | |
* @param string $string | |
* String al que se le tiene que quitar los puntos y aparte. | |
* | |
* @return array | |
* Strings sin punto y aparte. | |
*/ | |
function remove_last_dot($string) { | |
$string = trim($string); | |
$string_not_dot = $string; | |
if (preg_match("/\\.$/", $string) > 0) { | |
$string_not_dot = substr($string, 0, strlen($string) - 1); | |
} | |
if (preg_match('"\\.(\\s)*</p>"', $string) > 0) { | |
$string_not_dot = preg_replace('"\\.(\\s)*</p>"', "</p>", $string); | |
} | |
return $string_not_dot; | |
} | |
/** | |
* Campos a pasar a minúsculas. | |
* | |
* @return array | |
* lista de campos a pasar a minúsculas. | |
*/ | |
function fields_to_lower() { | |
return array( | |
'node_title', | |
'users_node_name', | |
'_field_data', | |
'field_body', | |
'field_body_1', | |
'field_field_image_title', | |
'field_field_image_credit', | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment