/**
  * Almacena la noticia enviada por el usuario a traves de una instancia
  * del módulo mod_soycorresponsal.
  */
 function saveCorresponsalContent()
 {
     $mainframe = JFactory::getApplication();
     jimport('json.json');
     $helper = new comZonalesHelper();
     $response = array();
     // chequea irregularidades en el request
     JRequest::checkToken() or jexit('Invalid Token');
     // titulo del modulo que envio el request
     $moduleTitle = JRequest::getVar('module', NULL, 'post', 'string');
     // chequea que el modulo especificado en el request sea valido
     if (!$moduleTitle) {
         jexit($helper->getJsonResponse('failure', 'Error interno', 'No module name'));
     } else {
         jimport('joomla.application.module.helper');
         $module = JModuleHelper::getModule('soycorresponsal', $moduleTitle);
         if ($module->id == 0) {
             jexit($helper->getJsonResponse('failure', 'Error interno', 'Invalid module'));
         }
     }
     // recupera parametros del módulo
     $modparams = new JParameter($module->params);
     // librería recaptcha
     jimport('recaptcha.recaptchalib');
     // parametros del componente
     $zonalesParams =& JComponentHelper::getParams('com_zonales');
     $privatekey = $zonalesParams->get('recaptcha_privatekey', null);
     if (!$privatekey) {
         jexit($helper->getJsonResponse('failure', $modparams->get('error'), 'No recaptcha private key'));
     } else {
         // validamos la respuesta del usuario
         $challenge = JRequest::getVar('recaptcha_challenge_field', NULL, 'post', 'string');
         $response = JRequest::getVar('recaptcha_response_field', NULL, 'post', 'string');
         $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $challenge, $response);
         if (!$resp->is_valid) {
             jexit($helper->getJsonResponse('captcha-failure', $modparams->get('error'), 'Invalid response'));
         } else {
             // inicializa variables a utilizar
             $db =& JFactory::getDBO();
             $user =& JFactory::getUser();
             if ($user->guest) {
                 $user =& JFactory::getUser($modparams->get('user'));
             }
             //$catid = $modparams->get('category', 0);
             $nullDate = $db->getNullDate();
             // tabla de contenidos joomla
             $row =& JTable::getInstance('content');
             /* if ($catid > 0) {
                $category =& JTable::getInstance('category');
                $category->load($catid);
                $sectionid = $category->section;
                } */
             $nullDate = $db->getNullDate();
             $row->title = JRequest::getVar('title', NULL, 'post', 'string');
             $row->sectionid = 0;
             $row->catid = 0;
             $row->version = 0;
             $row->state = 1;
             $row->ordering = 0;
             $row->images = array();
             $row->publish_down = $nullDate;
             $row->created_by = $user->get('id');
             $row->modified = gmdate('Y-m-d H:i:s');
             // corrección de la fecha
             $config =& JFactory::getConfig();
             $row->created = gmdate('Y-m-d H:i:s');
             $row->publish_up = gmdate('Y-m-d 00:00:00');
             // se redondea timestamp de creación
             if ($row->created && strlen(trim($row->created)) <= 10) {
                 $row->created .= ' 00:00:00';
             }
             // Prepare the content for saving to the database
             require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_content' . DS . 'helper.php';
             ContentHelper::saveContentPrep($row);
             // Se agregan nombre de usuario, correo y telefono
             $enviaNombre = JRequest::getVar('nombre', NULL, 'post', 'string');
             $enviaEmail = JRequest::getVar('email', NULL, 'post', 'string');
             $enviaTel = JRequest::getVar('telefono', NULL, 'post', 'string');
             $row->introtext = $row->introtext . "<p>Envio esta noticia:</p><p>Nombre: {$enviaNombre}<br/>Email: {$enviaEmail}<br/>";
             /**
              * Para videos de YouTube, arreglo la url para que se pueda ver
              */
             $searchString = "watch?v=";
             $imgPos = 0;
             while ($imgPos = strpos($row->introtext, $searchString, $imgPos)) {
                 $strPre = substr($row->introtext, 0, $imgPos);
                 $strPos = substr($row->introtext, $imgPos + strlen($searchString));
                 $row->introtext = $strPre . 'v/' . $strPos;
             }
             // Make sure the data is valid
             if (!$row->check()) {
                 JError::raiseError(500, $db->stderr());
             }
             // Store the content to the database
             if (!$row->store()) {
                 JError::raiseError(500, $db->stderr());
             }
             // Check the article and update item order
             $row->checkin();
             $row->reorder('catid = ' . (int) $row->catid . ' AND state >= 0');
             // Asignamos los tags de Custom Properties según los valores de zonal y localidad
             $partidoId = JRequest::getVar('provincias_sc', NULL, 'post', 'int');
             $zonaId = JRequest::getVar('localidad', NULL, 'post', 'int');
             $query = "REPLACE INTO #__custom_properties (ref_table, content_id,field_id,value_id)\n\t\t\t\t\tSELECT 'content','{$row->id}',v.field_id AS field, v.id AS value\n\t\t\t\t\tFROM #__custom_properties_values v\n                                        WHERE v.parent_id = {$partidoId}\n\t\t\t\t\tAND v.id = {$zonaId}\n                                        OR v.name = 'la_voz_del_vecino'";
             $database = JFactory::getDBO();
             $database->setQuery($query);
             $database->query();
             // Process the content preparation plugins
             JPluginHelper::importPlugin('content');
             $dispatcher =& JDispatcher::getInstance();
             $dispatcher->trigger('onAfterContentSave', array(&$row, $row->id < 1));
             // Todo ok, enviamos confirmación
             echo $helper->getJsonResponse('success', $modparams->get('confirmacion'));
             return;
         }
     }
 }