Àrea voluntaris

‘; echo ‘___’ . $n . ‘________________________
‘; print_r($v); echo ‘

‘;
}

function sticpa_load_languages()
{
$text_domain = ‘sticpa’;
$path_languages = basename(dirname(__FILE__)) . ‘/languages/’;
load_plugin_textdomain($text_domain, false, $path_languages);
}

include plugin_dir_path(__FILE__) . ‘inc/stic-action.php’;

add_action(‘admin_menu’, ‘sugar_crm_portal_create_menu’);

// Add JS script for form management
// Don’t add the action to avoid including the js in all the pages of the site. Instead it is loaded when the shortcode is applied
// add_action(«wp_enqueue_scripts», «dcms_insertar_js»);
function dcms_insertar_js()
{
wp_register_script(‘sugarcrm’, plugin_dir_url(__FILE__) . ‘js/iban.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘sugarcrm’);
wp_register_script(‘fullcalendar’, plugin_dir_url(__FILE__) . ‘js/fullcalendar/lib/main.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘fullcalendar’);
wp_register_script(‘fullcalendar-locale’, plugin_dir_url(__FILE__) . ‘js/fullcalendar/lib/locales-all.min.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘fullcalendar-locale’);
wp_register_script(‘sugarcrm-own’, plugin_dir_url(__FILE__) . ‘js/stic-utils.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘sugarcrm-own’);
wp_register_script(‘custom-utils’, plugin_dir_url(__FILE__) . ‘js/custom-utils.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘custom-utils’);
// We use only one file for plugin literals, so although theoretically we should call this function twice (one efor each js), we only call it once.
wp_localize_script(‘sugarcrm-own’, ‘stic_script_vars’, getSticScriptVars());
wp_register_script(‘multiselect’, plugin_dir_url(__FILE__) . ‘js/selectize.min.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘multiselect’);
wp_register_script(‘datatables’, plugin_dir_url(__FILE__) . ‘js/jquery.dataTables.min.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘datatables’);
wp_register_script(‘moment’, ‘https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘moment’);
wp_register_script(‘datetime-moment’, ‘https://cdn.datatables.net/plug-ins/1.10.21/sorting/datetime-moment.js’, array(‘jquery’), ‘1’, true);
wp_enqueue_script(‘datetime-moment’);
}

function sugar_crm_portal_create_menu()
{

//create admin side menu
add_menu_page(‘SinergiaCRM Private Area’, __(‘SinergiaCRM Private Area’, ‘sticpa’), ‘administrator’, ‘sugar-crm-portal’, ‘sugar_crm_portal_settings_page’);

//call register settings function
add_action(‘admin_init’, ‘register_sugar_crm_portal_settings’);
}

function register_sugar_crm_portal_settings()
{
//register our settings
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_name’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_host_url’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_rest_url’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_username’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_password’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_module’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_case_per_page’);
register_setting(‘sugar_crm_portal-settings-group’, ‘sticpa_scp_sugar_crm_version’);
}

function sugar_crm_portal_settings_page()
{
; // Admin side page options
?>


» name=’sticpa_scp_host_url’>

» name=’sticpa_scp_password’>
Module

:

https://wikisuite.sinergiacrm.org/index.php?title=Plugin_Wordpress_para_gesti%C3%B3n_de_%C3%81rea_Privada


login() != null) {
?>

«.__(‘Sign in’, ‘sticpa’).»

«;
} else {
$html .= «

Private Area

«;
}
$languageHtml = «»;
if (function_exists(‘pll_the_languages’)) {
$languageSelector = pll_the_languages(array(‘dropdown’ => 1,’show_flags’=>1,’show_names’=>0,’hide_current’=>1, ‘echo’ => 0));

$languageHtml = »

«;
} elseif (shortcode_exists(‘wpml_language_selector_widget’)) {
$languageHtml = »

«;
}
$moduleSelectHTML = «»;
if(get_option(‘sticpa_scp_module’) == «Any») {
$moduleSelectHTML = »

«;
}
$html .= »

«;
return $html;
}

function modify_plugin_locale_defaults($locale, $domain) {
$locale = ‘ca_ES’;
return $locale;
}

function sugar_crm_portal_check_user_and_login($html = «»)
{
if (isset($_REQUEST[‘scp_login_form_submit’]) == true) {
$scp_module = getDestinationModule();
$scp_username = $_REQUEST[‘scp_username’];
$scp_password = $_REQUEST[‘scp_password’];

$objSCP = SugarRestApiCall::getObjSCP();

$isLogin = $objSCP->PortalLogin($scp_username, $scp_password, $scp_module);
if ((isset($isLogin->entry_list[0]) && $isLogin->entry_list[0] != null) && ($scp_username != null) && ($scp_password != null)) {
$_SESSION[‘scp_module’] = $scp_module;
$_SESSION[‘scp_user_id’] = $isLogin->entry_list[0]->id;
$_SESSION[‘scp_user_contact_name’] = $isLogin->entry_list[0]->name_value_list->name->value;
$_SESSION[‘scp_account_id’] = isset($isLogin->entry_list[0]->name_value_list->account_id) ? $isLogin->entry_list[0]->name_value_list->account_id->value : null;
$_SESSION[‘scp_user_account_name’] = $isLogin->entry_list[0]->name_value_list->stic_pa_username_c->value;
$_SESSION[‘scp_user_assigned_user_id’] = $isLogin->entry_list[0]->name_value_list->assigned_user_id->value;
$relationshipTypes = array();
if (defined(‘RELATIONSHIP_TUTOR_TYPES’)) {
$relationshipTypes = RELATIONSHIP_TUTOR_TYPES;
$_SESSION[‘scp_user_adult’] = check_user_adult($_SESSION[‘scp_user_id’], $relationshipTypes);
} else {
$_SESSION[‘scp_user_adult’] = true;
}
$html .= sugar_crm_portal_index();
} else {
$html .= «

«;

}

} else {
$scp_password = $_REQUEST[‘scp_password’] ?? null;
$html .= «

«;
}
return $html;
}

function check_user_adult($userId, $relationshipTypes = array()) {
$objSCP = SugarRestApiCall::getObjSCP();
if (empty($relationshipTypes)) {
return false;
}
$query = «((stic_personal_environment.start_date <= DATE(NOW()) AND (stic_personal_environment.end_date >= DATE(NOW()) OR stic_personal_environment.end_date IS NULL)) AND stic_personal_environment.relationship_type in («;

foreach($relationshipTypes as $key => $type) {
if ($key) {
$query .= ‘,’;
}
$query.= «‘».$type .»‘»;
}
$query .= «))»;
$params = array(
‘module_name’ => ‘Contacts’,
«module_id» => $userId,
«link_field_name» => ‘stic_personal_environment_contacts_1’,
«related_module_query» => $query,
«related_fields» => array(‘id’),
«related_module_link_name_to_fields_array» => array(),
«deleted» => 0, //show or not deleted elements (usually 0)
«order_by» => «»,
«offset» => «»,
«limit» => 0,
);

$getRelatedElements = $objSCP->getRelatedElementsForLoggedUser($params);
if (empty($getRelatedElements)) {
return true;
}
return false;
}

function sugar_crm_portal_forgot_password($html = «»)
{
// forgot password
$current_url = explode(‘?’, $_SERVER[‘REQUEST_URI’], 2);
$current_url = $current_url[0] . ‘?internalpage=stic_forgot_password’;

$html .= «

» . __(‘Password’, ‘sticpa’) . «

«;

if (isset($_REQUEST[‘success’]) && $_REQUEST[‘success’] == true) {
$html .= «» . __(‘Your password was successfully sent.’, ‘sticpa’) . ««;
}

if (isset($_REQUEST[‘error’]) && $_REQUEST[‘error’] == 1) {
$html .= «» . __(‘Failed to send your password. Please, try it again later or contact the administrator.’, ‘sticpa’) . ««;
}

if (isset($_REQUEST[‘error’]) && $_REQUEST[‘error’] == 2) {
$html .= «» . __(‘Your email address does not match.’, ‘sticpa’) . ««;
}

if (isset($_REQUEST[‘error’]) && $_REQUEST[‘error’] == 3) {
$html .= «» . __(‘Your username does not exist.’, ‘sticpa’) . ««;
}

$moduleSelectHTML = «»;
if(getDestinationModule() == «Any») {
$moduleSelectHTML = »

«;
}

$html .= »

    «.$moduleSelectHTML.»



«;
$html .= «

«;

return $html;
}

function sugar_crm_portal_index($html = «»)
{
// index
$current_url = explode(‘?’, $_SERVER[‘REQUEST_URI’], 2);
$current_url = $current_url[0];

$objSCP = SugarRestApiCall::getObjSCP();

$html .= menu();

if (!isset($_SESSION[‘scp_tutor_user_id’]) && !isset($_REQUEST[‘internalpage’])) {
if (isset($_SESSION[‘scp_user_adult’]) && $_SESSION[‘scp_user_adult’]) {
$currentPage = defaultMenuElement();
} else {
$currentPage = ‘single_stic_profile_selection’;
}
} else {
$currentPage = $_REQUEST[‘internalpage’];
}
if (!$currentPage == ») {
ob_start();
include plugin_dir_path(__FILE__) . ‘pages/’ . $currentPage . ‘.php’;
$returned = ob_get_contents();
$html .= $returned;
ob_end_clean();

}
$html .= «

«;

return $html;
}

function sugar_crm_portal_signup($html = «»)
{
$current_url = explode(‘?’, $_SERVER[‘REQUEST_URI’], 2);
$current_url = $current_url[0];

$html .=’

‘;
//We include the corresponding form based on the content of $_REQUEST
if (!$_REQUEST[‘internalpage’] == ») {
ob_start();
require_once plugin_dir_path(__FILE__) . ‘pages/’ . $_REQUEST[‘internalpage’] . ‘.php’;
$returned = ob_get_contents();
ob_end_clean();
$html .= $returned;
}
$html .= «

«;

return $html;

}

add_shortcode(‘sinergiacrm-private-area’, ‘sinergiacrm_private_area_shortcode’); // add shortcode


function sinergiacrm_private_area_shortcode()
{
// Load js only when shortcode is present
dcms_insertar_js();

if (isset($_SESSION[‘scp_user_id’]) == true) {
$content .= sugar_crm_portal_index();
} else {
if (isset($_REQUEST[‘internalpage’]) && $_REQUEST[‘internalpage’] == ‘single_stic_signup’) {
$content .= sugar_crm_portal_signup();

} else if (isset($_REQUEST[‘internalpage’]) && $_REQUEST[‘internalpage’] == ‘stic_forgot_password’) {
$content .= sugar_crm_portal_forgot_password();
} else {
$content .= sugar_crm_portal_check_user_and_login();
}
}
return $content;
}

add_action(‘init’, ‘sugar_crm_portal_start_session’, 1); // start session
function sugar_crm_portal_start_session()
{
if (!session_id()) {
session_start();
}
}

if (isset($_REQUEST[‘logout’]) == ‘true’) // logout
{
add_action(‘init’, ‘sugar_crm_portal_louout’, 1);
function sugar_crm_portal_louout()
{
unset($_SESSION[‘scp_user_id’]);
unset($_SESSION[‘scp_tutor_user_id’]);
unset($_SESSION[‘scp_tutor_user_contact_name’]);
unset($_SESSION[‘scp_account_id’]);
unset($_SESSION[‘scp_user_account_name’]);
unset($_SESSION[‘scp_user_contact_name’]);
unset($_SESSION[‘api_session_id’]);
unset($_SESSION[‘scp_user_securitygroups’]);
unset($_SESSION[‘scp_user_assigned_user_id’]);
unset($_SESSION[‘scp_user_adult’]);
unset($_SESSION[‘scp_tutor_is_user’]);
unset($_SESSION[‘scp_module’]);

$redirect_url = explode(‘?’, $_SERVER[‘REQUEST_URI’], 2);
$redirect_url = $redirect_url[0];
wp_redirect($redirect_url);
exit;
}
}

add_action(‘wp_enqueue_scripts’, ‘sugar_crm_portal_style_and_script’); // add custom style and script
function sugar_crm_portal_style_and_script()
{
global $post;

// only loads css if the shortcode is present, not polluting the rest of the site
if (is_a($post, ‘WP_Post’) && has_shortcode($post->post_content, ‘sinergiacrm-private-area’)) {
wp_enqueue_style(‘stic-style’, plugins_url(‘css/stic-style.css’, __FILE__));
wp_enqueue_style(‘stic-multiselect’, plugins_url(‘css/selectize.css’, __FILE__));
wp_enqueue_style(‘custom-style’, plugins_url(‘css/custom-style.css’, __FILE__));
wp_enqueue_style(‘fullcalendar’, plugins_url(‘js/fullcalendar/lib/main.css’, __FILE__));
}

}

register_activation_hook(__FILE__, ‘scp_folder’);
function scp_folder()
{
$upload_dir = wp_upload_dir();
$upload_scp_uploads = $upload_dir[‘basedir’] . «/stic-uploads»;
if (!is_dir($upload_scp_uploads)) {
wp_mkdir_p($upload_scp_uploads);
}
}

function scp_deleteDirectory($dir)
{
if (!file_exists($dir)) {
return true;
}

if (!is_dir($dir)) {
return unlink($dir);
}

foreach (scandir($dir) as $item) {
if ($item == ‘.’ || $item == ‘..’) {
continue;
}

if (!scp_deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}

}

return rmdir($dir);
}

register_uninstall_hook(__FILE__, ‘sugar_crm_portal_uninstall’); // uninstall plug-in
function sugar_crm_portal_uninstall()
{
delete_option(‘sticpa_scp_name’);
delete_option(‘sticpa_scp_host_url’);
delete_option(‘sticpa_scp_rest_url’);
delete_option(‘sticpa_scp_username’);
delete_option(‘sticpa_scp_password’);
delete_option(‘sticpa_scp_module’);

$upload_dir = wp_upload_dir();
$upload_scp_uploads = $upload_dir[‘basedir’] . «/stic-uploads»;

if (is_dir($upload_scp_uploads)) {
scp_deleteDirectory($upload_scp_uploads);
}
}