vendor/contao-community-alliance/contao-clipboard-bundle/src/Clipboard.php line 100

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of contao-community-alliance/contao-clipboard-bundle.
  4.  *
  5.  * (c) 2013 MEN AT WORK.
  6.  * (c) 2021 The CCA team.
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  *
  11.  * This project is provided in good faith and hope to be usable by anyone.
  12.  *
  13.  * @package    Clipboard
  14.  * @author     Ingolf Steinhardt <info@e-spin.de>
  15.  * @copyright  2013 MEN AT WORK.
  16.  * @copyright  2021 The CCA team.
  17.  * @license    https://spdx.org/licenses/LGPL-3.0-or-later.html LGPL-3.0-or-later
  18.  * @filesource
  19.  */
  20. namespace ContaoCommunityAlliance\ClipboardBundle;
  21. use Contao\BackendTemplate;
  22. use Contao\BackendUser;
  23. use Contao\Environment;
  24. use Contao\Input;
  25. use Contao\Session;
  26. use ContaoCommunityAlliance\ClipboardBundle\Helper\Base as HelperBase;
  27. use ContaoCommunityAlliance\ClipboardBundle\Helper\Database as HelperDatabase;
  28. use ContaoCommunityAlliance\ClipboardBundle\Xml\Base as XmlBase;
  29. /**
  30.  * Class Clipboard
  31.  */
  32. class Clipboard
  33. {
  34.     /**
  35.      * Contains some helper functions
  36.      *
  37.      * @var HelperBase
  38.      */
  39.     private $helper;
  40.     /**
  41.      * Contains all xml specific functions and all information to the
  42.      * clipboard elements
  43.      *
  44.      * @var XmlBase
  45.      */
  46.     private $xml;
  47.     /**
  48.      * Contains specific database request
  49.      *
  50.      * @var HelperDatabase
  51.      */
  52.     private $database;
  53.     /**
  54.      * Session Container
  55.      *
  56.      * @var Session
  57.      */
  58.     private $session;
  59.     /**
  60.      * Current backend user.
  61.      *
  62.      * @var BackendUser
  63.      */
  64.     private $user;
  65.     /**
  66.      * Current backend user.
  67.      *
  68.      * @var Environment
  69.      */
  70.     private $environment;
  71.     /**
  72.      * Prevent constructing the object (Singleton)
  73.      *
  74.      * @param HelperBase     $clipboardHelper
  75.      *
  76.      * @param XmlBase        $clipboardXml
  77.      *
  78.      * @param HelperDatabase $clipboardDatabase
  79.      *
  80.      */
  81.     public function __construct(
  82.         HelperBase $clipboardHelper,
  83.         XmlBase $clipboardXml,
  84.         HelperDatabase $clipboardDatabase
  85.     ) {
  86.         $this->helper   $clipboardHelper;
  87.         $this->xml      $clipboardXml;
  88.         $this->database $clipboardDatabase;
  89.         $this->session     Session::getInstance();
  90.         $this->user        BackendUser::getInstance();
  91.         $this->environment Environment::getInstance();
  92.     }
  93.     /**
  94.      * Get clipboard container object
  95.      *
  96.      * @return XmlBase
  97.      *
  98.      * @deprecated Use getXml instead.
  99.      */
  100.     public function cb()
  101.     {
  102.         return $this->getXml();
  103.     }
  104.     /**
  105.      * Get clipboard container object
  106.      *
  107.      * @return XmlBase
  108.      */
  109.     public function getXml()
  110.     {
  111.         return $this->xml;
  112.     }
  113.     /**
  114.      * Return boolean if the clipboard is for given dca and user allowed
  115.      *
  116.      * @param string $dca
  117.      *
  118.      * @return boolean
  119.      */
  120.     public function isClipboard($dca null): bool
  121.     {
  122.         $arrAllowedLocations $GLOBALS['CLIPBOARD']['locations'];
  123.         if ($dca == null || !isset($GLOBALS['CLIPBOARD']['locations']) || !$this->user->clipboard) {
  124.             return false;
  125.         }
  126.         if (in_array($dca$arrAllowedLocations)) {
  127.             if (TL_MODE == 'BE' && in_array($this->helper->getPageType(), $arrAllowedLocations)) {
  128.                 return true;
  129.             }
  130.         }
  131.         return false;
  132.     }
  133.     /**
  134.      * Handle all main operations, clean up the url and redirect to itself
  135.      */
  136.     public function init()
  137.     {
  138.         $arrSession $this->session->get('clipboardExt');
  139.         if ($arrSession['readXML']) {
  140.             return;
  141.         }
  142.         if (stristr(
  143.                 Input::get('key'),
  144.                 'cl_'
  145.             )
  146.             || Input::post('FORM_SUBMIT') == 'tl_select' && isset($_POST['cl_group'])) {
  147.             $arrUnsetParams = [];
  148.             foreach (array_keys($_GET) as $strGetParam) {
  149.                 switch ($strGetParam) {
  150.                     case 'key':
  151.                         switch (Input::get($strGetParam)) {
  152.                             // Set new favorite
  153.                             case 'cl_favor':
  154.                                 if (strlen(Input::get('cl_id'))) {
  155.                                     $this->favor(Input::get('cl_id'));
  156.                                 }
  157.                                 break;
  158.                             // Delete an element
  159.                             case 'cl_delete':
  160.                                 if (strlen(Input::get('cl_id'))) {
  161.                                     $this->delete(Input::get('cl_id'));
  162.                                 }
  163.                                 break;
  164.                             // Edit Element
  165.                             case 'cl_edit':
  166.                                 $arrTitles Input::post('title');
  167.                                 if (is_array($arrTitles)) {
  168.                                     $this->edit($arrTitles);
  169.                                 }
  170.                                 break;
  171.                             // Create new entry
  172.                             case 'cl_copy':
  173.                                 $this->copy();
  174.                                 break;
  175.                             case 'cl_header_pastenew':
  176.                             case 'cl_paste_into':
  177.                                 $this->pasteInto();
  178.                                 break;
  179.                             case 'cl_paste_after':
  180.                                 $this->pasteAfter();
  181.                                 break;
  182.                         }
  183.                         $arrUnsetParams[$strGetParam] = Input::get($strGetParam);
  184.                         break;
  185.                     case 'act':
  186.                         if (Input::get('key') != 'cl_delete') {
  187.                             // Copy multi edit elements to clipboard
  188.                             $ids deserialize(Input::post('IDS'));
  189.                             if (!is_array($ids) || empty($ids)) {
  190.                                 $this->reload();
  191.                             }
  192.                             $this->copy(true$ids);
  193.                             $arrUnsetParams[$strGetParam] = Input::get($strGetParam);
  194.                         }
  195.                         break;
  196.                     case 'childs':
  197.                     case 'mode':
  198.                     case 'cl_id':
  199.                         $arrUnsetParams[$strGetParam] = Input::get($strGetParam);
  200.                         break;
  201.                 }
  202.             }
  203.             foreach ($arrUnsetParams as $k => $v) {
  204.                 Input::setGet($knull);
  205.                 $this->environment->request     str_replace("&$k=$v"''$this->environment->request);
  206.                 $this->environment->queryString str_replace("&$k=$v"''$this->environment->queryString);
  207.                 $this->environment->requestUri  str_replace("&$k=$v"''$this->environment->requestUri);
  208.             }
  209.             $arrUnsetKeyParams = [
  210.                 'cl_copy',
  211.                 'cl_paste_into',
  212.                 'cl_paste_after'
  213.             ];
  214.             if (in_array($arrUnsetParams['key'], $arrUnsetKeyParams) && $this->helper->getPageType() == 'content') {
  215.                 $objArticle $this->database->getArticleObjectFromContentId(Input::get('id'));
  216.                 $strRequestWithoutId str_replace(
  217.                     substr($this->environment->requeststrpos($this->environment->request'&id')),
  218.                     '',
  219.                     $this->environment->request
  220.                 );
  221.                 \Contao\Backend::redirect($strRequestWithoutId '&id=' $objArticle->id);
  222.             } elseif (in_array(
  223.                           $arrUnsetParams['key'],
  224.                           $arrUnsetKeyParams
  225.                       )
  226.                       && $this->helper->getPageType() == 'module') {
  227.                 $objTheme $this->database->getThemeObjectFromModuleId(Input::get('id'));
  228.                 $strRequestWithoutId str_replace(
  229.                     substr($this->environment->requeststrpos($this->environment->request'&id')),
  230.                     '',
  231.                     $this->environment->request
  232.                 );
  233.                 \Contao\Backend::redirect($strRequestWithoutId '&id=' $objTheme->id);
  234.             }
  235.             \Contao\Backend::redirect($this->environment->request);
  236.         }
  237.     }
  238.     /**
  239.      * Add the Clipboard to the backend template
  240.      *
  241.      * HOOK: $GLOBALS['TL_HOOKS']['outputBackendTemplate']
  242.      *
  243.      * @param string $strContent
  244.      * @param string $strTemplate
  245.      *
  246.      * @return string
  247.      */
  248.     public function outputBackendTemplate($strContent$strTemplate): string
  249.     {
  250.         $this->session->set('clipboardExt', ['readXML' => false]);
  251.         if ($strTemplate == 'be_main' && $this->user->clipboard && $this->getXml()->hasElements()) {
  252.             $objTemplate = new BackendTemplate('be_clipboard');
  253.             $arrClipboard $this->getXml()->getElements();
  254.             $objTemplate->clipboard $arrClipboard;
  255.             $objTemplate->isContext $this->helper->isContext();
  256.             $objTemplate->action    $this->environment->request '&key=cl_edit';
  257.             if (!$this->helper->isContext()) {
  258.                 $strContent preg_replace('/<body.*class="/'"$0clipboard "$strContent1);
  259.             }
  260.             return preg_replace('/<div.*id="container".*>/'$objTemplate->parse() . "\n$0"$strContent1);
  261.         }
  262.         return $strContent;
  263.     }
  264.     /**
  265.      * Prepare context if is set or disable context
  266.      */
  267.     public function prepareContext()
  268.     {
  269.         if (!$this->helper->isContext()) {
  270.             foreach ($GLOBALS['CLIPBOARD'] as $key => $value) {
  271.                 if (array_key_exists('attributes'$GLOBALS['CLIPBOARD'][$key])) {
  272.                     $GLOBALS['CLIPBOARD'][$key]['attributes'] = 'onclick="Backend.getScrollOffset();"';
  273.                 }
  274.             }
  275.         }
  276.     }
  277.     /**
  278.      * Paste favorite into
  279.      */
  280.     public function pasteInto()
  281.     {
  282.         $this->getXml()->read('pasteInto'Input::get('id'));
  283.     }
  284.     /**
  285.      * Paste favorite after
  286.      */
  287.     public function pasteAfter()
  288.     {
  289.         $this->getXml()->read('pasteAfter'Input::get('id'));
  290.     }
  291.     /**
  292.      * Delete the given element
  293.      *
  294.      * @param string $hash
  295.      */
  296.     public function delete($hash)
  297.     {
  298.         $this->getXml()->deleteFile($hash);
  299.     }
  300.     /**
  301.      * Make the given element favorit
  302.      *
  303.      * @param string $hash
  304.      */
  305.     public function favor($hash)
  306.     {
  307.         $this->getXml()->setFavor($hash);
  308.     }
  309.     /**
  310.      * Rename all given clipboard titles
  311.      *
  312.      * @param array $arrTitles
  313.      */
  314.     public function edit($arrTitles)
  315.     {
  316.         $this->getXml()->editTitle($arrTitles);
  317.     }
  318.     /**
  319.      * Return the title for the given id
  320.      *
  321.      * @param mixed $mixedId
  322.      *
  323.      * @return string
  324.      */
  325.     public function getTitle($mixedId)
  326.     {
  327.         $arrTitle   = [];
  328.         $booClGroup false;
  329.         if (is_array($mixedId)) {
  330.             $booClGroup true;
  331.         }
  332.         switch ($this->helper->getPageType()) {
  333.             case 'page':
  334.                 if ($booClGroup) {
  335.                     $mixedId $mixedId[0];
  336.                 }
  337.                 $objElem  $this->database->getPageObject($mixedId);
  338.                 $arrTitle = ['title' => $objElem->title];
  339.                 break;
  340.             case 'article':
  341.                 $arrTitle = [
  342.                     'title' => call_user_func_array([
  343.                                                         $this->database,
  344.                                                         'get' $this->helper->getPageType() . 'Object'
  345.                                                     ], [$mixedId])->title
  346.                 ];
  347.                 break;
  348.             case 'content':
  349.                 if (!$booClGroup) {
  350.                     $mixedTitle $this->helper->createContentTitle($mixedId$booClGroup);
  351.                     if (!is_object($mixedTitle) && is_array($mixedTitle)) {
  352.                         $arrTitle $mixedTitle;
  353.                     } else {
  354.                         $arrTitle = [
  355.                             'title'     => $GLOBALS['TL_LANG']['MSC']['noClipboardTitle'],
  356.                             'attribute' => $GLOBALS['TL_LANG']['CTE'][$mixedTitle->type][0]
  357.                         ];
  358.                     }
  359.                 } else {
  360.                     $strTitle '';
  361.                     foreach ($mixedId as $intId) {
  362.                         $mixedTitle $this->helper->createContentTitle($intId$booClGroup);
  363.                         if (!is_object($mixedTitle) && is_array($mixedTitle)) {
  364.                             $strTitle $mixedTitle['title'];
  365.                             break;
  366.                         }
  367.                     }
  368.                     if (strlen($strTitle) > 0) {
  369.                         $arrTitle = ['title' => $strTitle];
  370.                     } else {
  371.                         $arrTitle = ['title' => $GLOBALS['TL_LANG']['MSC']['noClipboardTitle']];
  372.                     }
  373.                 }
  374.                 break;
  375.             case 'module':
  376.                 $objElem  $this->database->getModuleObject($mixedId);
  377.                 $arrTitle = ['title' => $objElem->name];
  378.                 break;
  379.             default:
  380.                 $arrTitle = ['title' => $GLOBALS['TL_LANG']['MSC']['noClipboardTitle']];
  381.         }
  382.         $arrTitle['title'] = \StringUtil::substr($arrTitle['title'], '24');
  383.         return $arrTitle;
  384.     }
  385.     /**
  386.      * Copy element to clipboard and write xml
  387.      *
  388.      * @param bool  $booClGroup
  389.      * @param array $ids
  390.      */
  391.     public function copy(bool $booClGroup, array $ids = [])
  392.     {
  393.         $arrSet = [
  394.             'user_id' => $this->user->id,
  395.             'table'   => $this->helper->getDatabasePageType()
  396.         ];
  397.         if ($booClGroup == true && count($ids) > 1) {
  398.             $arrSet['childs']     = 0;
  399.             $arrSet['elem_id']    = $ids;
  400.             $arrSet['grouped']    = true;
  401.             $arrSet['groupCount'] = count($ids);
  402.             $arrSet               array_merge($arrSet$this->getTitle($ids));
  403.         } else {
  404.             if (count($ids) == 1) {
  405.                 $intId $ids[0];
  406.             } else {
  407.                 $intId Input::get('id');
  408.             }
  409.             $arrSet['childs']     = ((Input::get('childs') == 1) ? 0);
  410.             $arrSet['elem_id']    = $intId;
  411.             $arrSet['grouped']    = false;
  412.             $arrSet['groupCount'] = 0;
  413.             $arrSet               array_merge($arrSet$this->getTitle($intId));
  414.         }
  415.         if (!$arrSet['attribute']) {
  416.             $arrSet['attribute'] = '';
  417.         }
  418.         $this->getXml()->write($arrSet);
  419.     }
  420. }