<?php

////////////////////////////////////////////////////////////////////////////////
//
// File:	function.FacebookShareDialog.php
// Project:	Facebook Dialog plugin for CMS Made Simple
// Version:	2.0
// Licence:	Free software under the GNU General Public License
// Created:	20131024, v1.0, Magal Hezi
// Updated:	20250112, v1.0, Magal Hezi - Updated for Facebook SDK v24.0
//
////////////////////////////////////////////////////////////////////////////////

function smarty_cms_function_FacebookShareDialog($params, &$smarty)
{
    $content_obj = \cms_utils::get_current_content();
    $href = $params['href'] ?? $params['url'] ?? $content_obj->GetURL();
    $layout = $params['layout'] ?? 'icon_link'; // 'box_count', 'button_count', 'button', 'icon_link'
    $lazy = isset($params['lazy']) ? ($params['lazy'] === 'true' || $params['lazy'] === true) : false;
    $mobile_iframe = isset($params['mobile_iframe']) ? ($params['mobile_iframe'] === 'true' || $params['mobile_iframe'] === true) : false;
    $size = $params['size'] ?? 'small'; // 'large', 'small'
    $locale = $params['locale'] ?? \CmsNlsOperations::get_current_language();
    $appId = isset($params['appId']) ? trim($params['appId']) : null;
    $id = trim($params['id'] ?? '');
    $class = trim($params['class'] ?? '');

    $div_attrs = '';
    if ($id !== '') {
        $div_attrs .= ' id="' . htmlspecialchars($id, ENT_QUOTES) . '"';
    }
    $div_class = trim("fb-share-button $class");
    $div_attrs .= ' class="' . htmlspecialchars($div_class, ENT_QUOTES) . '"';

    // Add data attributes
    $div_attrs .= ' data-href="' . htmlspecialchars($href, ENT_QUOTES) . '"';
    $div_attrs .= ' data-layout="' . htmlspecialchars($layout, ENT_QUOTES) . '"';
    $div_attrs .= ' data-size="' . htmlspecialchars($size, ENT_QUOTES) . '"';
    
    if ($lazy) {
        $div_attrs .= ' data-lazy="true"';
    }
    
    if ($mobile_iframe) {
        $div_attrs .= ' data-mobile_iframe="true"';
    }

    $sdk_url = "https://connect.facebook.net/{$locale}/sdk.js#xfbml=1&version=v24.0";
    if ($appId) {
        $sdk_url .= "&appId=" . rawurlencode($appId);
    }

    $html = <<<EOT
<!-- Facebook Share Button -->
<div id="fb-root"></div>
<script async defer crossorigin="anonymous"
  src="{$sdk_url}"
  nonce="FBShareNonce">
</script>

<div{$div_attrs}>
</div>
EOT;

    return $html;
}

function smarty_cms_help_function_FacebookShareDialog() {
	?>
	<h3>What does this do?</h3>
	<p>
Facebook offers an easy-to-use Share dialog that you can include on your web page or in your mobile web app so people can share links on their timeline, in groups, or to their friends via a Facebook Message.</p>
<h3>How do I use it?</h3>
	<p>Insert the tag <code>{FacebookShareDialog}</code> into your template or page (case sensitive).</p>
	<p>To ensure that web-designers have maximum control over customization, if you want to style the plugin's output, feel free to use additional external CSS rules in conjunction with the <code>class</code> parameter.</p>
<p>Alternatively, you can always use the <code>custom_url</code> parameters to set manually (or pass them via smarty) the url and title values of the current page without self detection functionality.</p>
	
    <h3>What parameters does it take?	</h3>
	<ul>
      <li><em>(optional)</em> <code>href</code> or <code>url</code>
        <ul>
          <li>The absolute URL of the page that will be shared. Defaults to current page URL.</li>
          <li>E.g. <code>{FacebookShareDialog href="https://example.com/page"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>layout</code>
        <ul>
          <li>Selects the layout for the plugin. Can be: <code>box_count</code>, <code>button_count</code>, <code>button</code>, <code>icon_link</code></li>
          <li>Default: <code>icon_link</code></li>
          <li>E.g. <code>{FacebookShareDialog layout="button_count"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>lazy</code>
        <ul>
          <li>Use browser's lazy-loading mechanism. Can be <code>true</code> or <code>false</code></li>
          <li>Default: <code>false</code></li>
          <li>E.g. <code>{FacebookShareDialog lazy="true"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>mobile_iframe</code> (Deprecated)
        <ul>
          <li>Open share dialog in iframe on mobile instead of popup. Can be <code>true</code> or <code>false</code></li>
          <li>Default: <code>false</code></li>
          <li>E.g. <code>{FacebookShareDialog mobile_iframe="true"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>size</code>
        <ul>
          <li>Button size. Can be <code>large</code> or <code>small</code></li>
          <li>Default: <code>small</code></li>
          <li>E.g. <code>{FacebookShareDialog size="large"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>locale</code>
        <ul>
          <li>Language locale for the button. Defaults to current site language.</li>
          <li>E.g. <code>{FacebookShareDialog locale="en_US"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>appId</code>
        <ul>
          <li>Your Facebook App ID for analytics and insights.</li>
          <li>E.g. <code>{FacebookShareDialog appId="123456789"}</code></li>
        </ul>
      </li>
		<li><em>(optional)</em> <code>class</code>
        <ul>
          <li>CSS class name for styling the button container</li>
          <li>E.g. <code>{FacebookShareDialog class="my-share-button"}</code></li>
        </ul>
      </li>
      <li><em>(optional)</em> <code>id</code>
        <ul>
          <li>HTML ID attribute for the button container</li>
          <li>E.g. <code>{FacebookShareDialog id="share-btn-1"}</code></li>
        </ul>
      </li>
</ul>
	<?php
}

function smarty_cms_about_function_FacebookShareDialog() {
	?>
<p>Author: <a href="https://github.com/magalh">Magal Hezi</a></p>
	<p>Update: Check for updates for this plugin at the <a href="https://dev.cmsmadesimple.org/projects/fbsharedialog" target="_blank">CMS Made Simple Forge page</a></p>
	<p>Feature Requests: If you want to add others social bookmarking services or functionality, please let me know by opening a new feature request in the dedicated <strong>Feature Requests tab</strong> of the CMS Made Simple Forge page for this plugin</p>
	<p>Bugs: If you want to submit a new bug, please let me know by opening a new bug in the dedicated <strong>Bug Tracker tab</strong> of the CMS Made Simple Forge page for this plugin</p>
	<p>Licence: Free software under the GNU General Public License</p>
	<p>Change History:</p>
	<ul>
<li>Version 2.0</li>
	<ul>
		<li>Updated to Facebook SDK v24.0</li>
		<li>Added support for all Facebook Share Button attributes: href, layout, lazy, mobile_iframe, size</li>
		<li>Changed default layout from 'button' to 'icon_link'</li>
		<li>Improved parameter validation and HTML escaping</li>
		<li>Updated documentation with complete parameter reference</li>
	</ul>
<li>Version 1.0</li>
		<ul>
			<li>First release</li>
		</ul>
	</ul>
	<?php
}
?>
