/**
 * Article images service
 *
 * @param	site_manager	$sm
 * @param	string			$action
 * @param	string			$command
 * @return	boolean			true on succes, or false on fail
 */
function article_images_service(&$sm, $action, $command)
{
    require_once dirname(__FILE__) . '/article_images.service.php';
    $article_images_service = new article_images_service($sm);
    $um =& $sm->get_url_manager();
    $dm =& $sm->get_dump_manager();
    $lang =& $sm->get_lang();
    $lang->load_from_source('plugins/image_gallery/admin/lang/');
    if ($command == 'add') {
        $in_article_images = $um->get_param('PLUGIN_IMAGE_GALLERY_data');
        $response = $article_images_service->add($in_article_images);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_IMAGE_GALLERY_ADMIN_ADD_SUCCESS'));
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'editImage') {
        $image_id = $um->get_param('PLUGIN_IMAGE_GALLERY_id');
        $in_image = $um->get_param_regexp('/^PLUGIN_IMAGE_GALLERY_/');
        $response = $article_images_service->edit_image($image_id, $in_image);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_IMAGE_GALLERY_ADMIN_EDIT_SUCCESS'));
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'remove') {
        $image_id = $um->get_param('PLUGIN_IMAGE_GALLERY_id');
        $response = $article_images_service->remove($image_id);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_IMAGE_GALLERY_ADMIN_REMOVE_SUCCESS'));
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'reorder') {
        $in_order = $um->get_param('PLUGIN_IMAGE_GALLERY_order');
        $response = $article_images_service->reorder($in_order);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_IMAGE_GALLERY_ADMIN_REORDER_SUCCESS'));
            return true;
        } else {
            return false;
        }
    }
    return true;
}
/**
 * Article attachments service
 *
 * @param	site_manager	$sm
 * @param	string			$action
 * @param	string			$command
 * @return	boolean			true on succes, or false on fail
 */
function article_attachments_service(&$sm, $action, $command)
{
    require_once dirname(__FILE__) . '/article_attachments.service.php';
    $article_attachments_service = new article_attachments_service($sm);
    $um =& $sm->get_url_manager();
    $dm =& $sm->get_dump_manager();
    $lang =& $sm->get_lang();
    $lang->load_from_source('plugins/multiple_attachments/admin/lang/');
    if ($command == 'add') {
        $in_article_attachments = $um->get_param('PLUGIN_MULTIPLE_ATTACHMENTS_data');
        $response = $article_attachments_service->add($in_article_attachments);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_MULTIPLE_ATTACHMENTS_ADD_SUCCESS'));
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'editAttachment') {
        $attachment_id = $um->get_param('PLUGIN_MULTIPLE_ATTACHMENTS_id');
        $in_attachment = $um->get_param_regexp('/^PLUGIN_MULTIPLE_ATTACHMENTS_/');
        $response = $article_attachments_service->edit_attachment($attachment_id, $in_attachment);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_MULTIPLE_ATTACHMENTS_EDIT_SUCCESS'));
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'remove') {
        $attachment_id = $um->get_param('PLUGIN_MULTIPLE_ATTACHMENTS_id');
        $response = $article_attachments_service->remove($attachment_id);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_MULTIPLE_ATTACHMENTS_REMOVE_SUCCESS'));
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'reorder') {
        $in_order = $um->get_param('PLUGIN_MULTIPLE_ATTACHMENTS_order');
        $response = $article_attachments_service->reorder($in_order);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_MULTIPLE_ATTACHMENTS_REORDER_SUCCESS'));
            return true;
        } else {
            return false;
        }
    }
    return true;
}
/**
 * Form builder manager service
 *
 * @param	site_manager	$sm
 * @param	string			$action
 * @param	string			$command
 * @return	boolean			true on succes, or false on fail
 */
function form_builder_service($sm, $action, $command)
{
    require_once dirname(__FILE__) . '/form_builder.service.php';
    $form_builder_service = new form_builder_service($sm);
    $um = $sm->get_url_manager();
    $dm = $sm->get_dump_manager();
    $lang = $sm->get_lang();
    $pm = $sm->get_plugin_manager();
    $lang->load_lang(VIVVO_ADMIN_LANG, $pm->plugins['form_builder']['root_dir'] . 'admin/lang/');
    if ($command == 'add') {
        $in_form = $um->get_param_regexp('/^PLUGIN_FORM_BUILDER_/');
        unset($in_form['elements']);
        $in_form_elemnet = $um->get_param('PLUGIN_FORM_BUILDER_elements');
        $response = $form_builder_service->add($in_form, $in_form_elemnet);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_FORM_BUILDER_FORM_ADD_SUCCESS'));
            header('X-Vivvo-Action-Status: 1');
            vivvo_hooks_manager::call('form_builder_add_postaction');
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'edit') {
        $form_id = $um->get_param('PLUGIN_FORM_BUILDER_id');
        $in_form = $um->get_param_regexp('/^PLUGIN_FORM_BUILDER_/');
        unset($in_form['elements']);
        $in_form_elemnet = $um->get_param('PLUGIN_FORM_BUILDER_elements');
        $response = $form_builder_service->edit($form_id, $in_form, $in_form_elemnet);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_FORM_BUILDER_FORM_EDIT_SUCCESS'));
            header('X-Vivvo-Action-Status: 1');
            vivvo_hooks_manager::call('form_builder_edit_postaction');
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'delete') {
        $form_id = $um->get_param('PLUGIN_FORM_BUILDER_id');
        $response = $form_builder_service->delete($form_id);
        if ($response) {
            $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_PLUGIN_FORM_BUILDER_FORM_DELETE_SUCCESS'));
            header('X-Vivvo-Action-Status: 1');
            vivvo_hooks_manager::call('form_builder_delete_postaction');
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'mail') {
        $form_id = $um->get_param('PFB_form_id');
        $in_form = $um->get_param_regexp('/^PFB_/');
        $response = $form_builder_service->mail($form_id, $in_form);
        if ($response !== false) {
            $dm->add_dump('info', 0, $response);
            header('X-Vivvo-Action-Status: 1');
            vivvo_hooks_manager::call('form_builder_mail_postaction');
            return true;
        } else {
            return false;
        }
    } elseif ($command == 'my_action') {
        $form_id = $um->get_param('PFB_form_id');
        $in_form = $um->get_param_regexp('/^PFB_/');
        $response = $form_builder_service->my_action($form_id, $in_form);
        if ($response !== false) {
            $dm->add_dump('info', 0, $response);
            return true;
        } else {
            return false;
        }
    }
    header('X-Vivvo-Action-Status: 0');
    header('X-Vivvo-Action-Error: ' . json_encode($form_builder_service->get_error_info()));
    return false;
}