vendor/contao-community-alliance/contao-clipboard-bundle/src/Helper/Base.php line 573

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\Helper;
  21. use Contao\BackendUser;
  22. use Contao\Input;
  23. use Contao\System;
  24. /**
  25.  * Class ClipboardHelper
  26.  */
  27. class Base
  28. {
  29.     /**
  30.      * The clipboard database helper.
  31.      *
  32.      * @var \Contao\Database
  33.      */
  34.     private $database;
  35.     /**
  36.      * The current user.
  37.      *
  38.      * @var BackendUser
  39.      */
  40.     private $user;
  41.     /**
  42.      * Page type
  43.      *
  44.      * @var string
  45.      */
  46.     private $pageType;
  47.     /**
  48.      * Search for special chars
  49.      *
  50.      * @var array
  51.      */
  52.     public $searchFor = [
  53.         "\\",
  54.         "'"
  55.     ];
  56.     /**
  57.      * Replace special chars with
  58.      *
  59.      * @var array
  60.      */
  61.     public $replaceWith = [
  62.         "\\\\",
  63.         "\\'"
  64.     ];
  65.     /**
  66.      * Base constructor.
  67.      *
  68.      * @param Database     $clipboardDatabase The helper class of the clipboard.
  69.      *
  70.      * @param ContaoBridge $contaoBridge      The contao bindings.
  71.      */
  72.     public function __construct(Database $clipboardDatabaseContaoBridge $contaoBridge)
  73.     {
  74.         $this->database $clipboardDatabase;
  75.         $this->user     $contaoBridge->getBackendUser();
  76.         $this->setPageType();
  77.     }
  78.     /**
  79.      * Get the current page type.
  80.      *
  81.      * @return string|null
  82.      */
  83.     public function getPageType(): ?string
  84.     {
  85.         return $this->pageType;
  86.     }
  87.     /**
  88.      * Get the page type as database name
  89.      *
  90.      * @return string
  91.      */
  92.     public function getDatabasePageType(): string
  93.     {
  94.         return 'tl_' $this->pageType;
  95.     }
  96.     /**
  97.      * Set the current page type
  98.      */
  99.     protected function setPageType()
  100.     {
  101.         if (Input::get('table') == 'tl_content') {
  102.             $this->pageType 'content';
  103.         } elseif (Input::get('table') == 'tl_module') {
  104.             $this->pageType 'module';
  105.         } else {
  106.             $this->pageType Input::get('do');
  107.         }
  108.     }
  109.     /**
  110.      * Return if context checkbox is true or false
  111.      *
  112.      * @return boolean
  113.      */
  114.     public function isContext(): bool
  115.     {
  116.         return !$this->user->clipboard_context;
  117.     }
  118.     /**
  119.      * Return the paste button
  120.      *
  121.      * @param array  $row
  122.      * @param string $href
  123.      * @param string $label
  124.      * @param string $title
  125.      * @param string $icon
  126.      * @param string $attributes
  127.      * @param string $table
  128.      *
  129.      * @return string
  130.      */
  131.     public function getPasteButton(array $rowstring $hrefstring $labelstring $titlestring $iconstring $attributesstring $table): string
  132.     {
  133.         $boolFavorit Clipboard::getInstance()->cb()->hasFavorite();
  134.         if ($boolFavorit) {
  135.             $return '';
  136.             if ($this->user->isAdmin
  137.                 || ($this->user->hasAccess($row['type'], 'alpty')
  138.                     && $this->user->isAllowed(
  139.                         2,
  140.                         $row
  141.                     ))) {
  142.                 // Create link
  143.                 $return .= vsprintf('<a href="%s" title="%s" %s>%s</a>', [
  144.                                                                            // Create URL.
  145.                                                                            $this->addToUrl(
  146.                                                                                vsprintf('%s&amp;id=%s', [
  147.                                                                                                           $href,
  148.                                                                                                           $row['id']
  149.                                                                                                       ]
  150.                                                                                )
  151.                                                                            ),
  152.                                                                            specialchars($title),
  153.                                                                            $attributes,
  154.                                                                            // Create link image.
  155.                                                                            \Image::getHtml($icon$label)
  156.                                                                        ]
  157.                 );
  158.             } else {
  159.                 // Create image.
  160.                 $return .= \Image::getHtml(preg_replace('/\.gif$/i''_.gif'$icon)) . ' ';
  161.             }
  162.             return $return;
  163.         } else {
  164.             return '';
  165.         }
  166.     }
  167.     /**
  168.      * Return clipboard button
  169.      *
  170.      * HOOK: $GLOBALS['TL_HOOKS']['clipboardButtons']
  171.      *
  172.      * @param DataContainer $dc
  173.      * @param array         $row
  174.      * @param string        $table
  175.      * @param boolean       $cr
  176.      * @param bool          $arrClipboard
  177.      * @param childs        $childs
  178.      *
  179.      * @return string
  180.      */
  181.     public function clipboardButtons(DataContainer $dc, array $rowstring $tablebool $crbool $arrClipboardchilds $childs): string
  182.     {
  183.         if (!Input::get('act')) {
  184.             $objFavorit Clipboard::getInstance()->cb()->getFavorite();
  185.             if ($dc->table == 'tl_article' && $table == 'tl_page') {
  186.                 // Create button title and lable.
  187.                 $label $title vsprintf($GLOBALS['TL_LANG'][$dc->table]['pasteinto'][1], [
  188.                                                                                               $row['id']
  189.                                                                                           ]
  190.                 );
  191.                 // Create Paste Button.
  192.                 $return $this->getPasteButton(
  193.                     $row,
  194.                     $GLOBALS['CLIPBOARD']['pasteinto']['href'],
  195.                     $label,
  196.                     $title,
  197.                     $GLOBALS['CLIPBOARD']['pasteinto']['icon'],
  198.                     $GLOBALS['CLIPBOARD']['pasteinto']['attributes'],
  199.                     $dc->table
  200.                 );
  201.                 return $return;
  202.             }
  203.         }
  204.         return '';
  205.     }
  206.     public function clipboardActSelectButtons(DataContainer $dc): string
  207.     {
  208.         return '<input id="cl_multiCopy" class="tl_submit" type="submit" value="'
  209.                $GLOBALS['TL_LANG']['MSC']['groupSelected'] . '" name="cl_group">';
  210.     }
  211.     /**
  212.      * Write a message to the customer in TL_INFO and the log
  213.      *
  214.      * @param string $strMessage
  215.      */
  216.     public function writeCustomerInfoMessage($strMessage)
  217.     {
  218.         $this->log($strMessage__FUNCTION__TL_GENERAL);
  219.         if (version_compare(VERSION'2.11''>=')) {
  220.             $this->addInfoMessage($strMessage);
  221.         } else {
  222.             $strType 'TL_INFO';
  223.             if (!is_array($_SESSION[$strType])) {
  224.                 $_SESSION[$strType] = [];
  225.             }
  226.             $_SESSION[$strType][] = $strMessage;
  227.         }
  228.     }
  229.     /**
  230.      * Create title for content element and return it as string. If no title
  231.      * exists return content element as object.
  232.      *
  233.      * @param integer $intId
  234.      * @param boolean $booClGroup
  235.      *
  236.      * @return DB_Mysql_Result|string
  237.      */
  238.     public function createContentTitle(int $intIdbool $booClGroup)
  239.     {
  240.         $objContent  $this->database->getContentObject($intId);
  241.         $strHeadline $this->_getHeadlineValue($objContent);
  242.         $arrTitle = [];
  243.         switch ($objContent->type) {
  244.             case 'headline':
  245.             case 'gallery':
  246.             case 'downloads':
  247.                 if ($strHeadline) {
  248.                     $arrTitle['title'] = $strHeadline;
  249.                 }
  250.                 break;
  251.             case 'text':
  252.                 if ($strHeadline) {
  253.                     $arrTitle['title'] = $strHeadline;
  254.                 } elseif ($objContent->text) {
  255.                     $arrTitle['title'] = $objContent->text;
  256.                 }
  257.                 break;
  258.             case 'html':
  259.                 if ($objContent->html) {
  260.                     $arrTitle['title'] = $objContent->html;
  261.                 }
  262.                 break;
  263.             case 'list':
  264.                 $arrList deserialize($objContent->listitems);
  265.                 if ($strHeadline) {
  266.                     $arrTitle['title'] = $strHeadline;
  267.                 } elseif ($arrList[0]) {
  268.                     $arrTitle['title'] = $arrList[0];
  269.                 }
  270.                 break;
  271.             case 'table':
  272.                 $arrTable deserialize($objContent->tableitems);
  273.                 if ($strHeadline) {
  274.                     $arrTitle['title'] = $strHeadline;
  275.                 } elseif ($arrTable[0][0]) {
  276.                     $arrTitle['title'] = $arrTable[0][0];
  277.                 }
  278.                 break;
  279.             case 'accordion':
  280.                 if ($objContent->mooHeadline) {
  281.                     $arrTitle['title'] = $objContent->mooHeadline;
  282.                 }
  283.                 break;
  284.             case 'code':
  285.                 if ($strHeadline) {
  286.                     $arrTitle['title'] = $strHeadline;
  287.                 } elseif ($objContent->code) {
  288.                     $arrTitle['title'] = $objContent->code;
  289.                 }
  290.                 break;
  291.             case 'hyperlink':
  292.             case 'download':
  293.                 if ($strHeadline) {
  294.                     $arrTitle['title'] = $strHeadline;
  295.                 } elseif ($objContent->linkTitle) {
  296.                     $arrTitle['title'] = $objContent->linkTitle;
  297.                 }
  298.                 break;
  299.             case 'toplink':
  300.                 if ($objContent->linkTitle) {
  301.                     $arrTitle['title'] = $objContent->linkTitle;
  302.                 }
  303.                 break;
  304.             case 'image':
  305.                 if ($strHeadline) {
  306.                     $arrTitle['title'] = $strHeadline;
  307.                 } elseif ($objContent->caption) {
  308.                     $arrTitle['title'] = $objContent->caption;
  309.                 } elseif ($objContent->alt) {
  310.                     $arrTitle['title'] = $objContent->alt;
  311.                 }
  312.                 break;
  313.             default:
  314.                 // HOOK: call the hooks for clipboardContentTitle.
  315.                 if (isset($GLOBALS['TL_HOOKS']['clipboardContentTitle'])
  316.                     && is_array(
  317.                         $GLOBALS['TL_HOOKS']['clipboardContentTitle']
  318.                     )) {
  319.                     foreach ($GLOBALS['TL_HOOKS']['clipboardContentTitle'] as $arrCallback) {
  320.                         $this->import($arrCallback[0]);
  321.                         $strTmpTitle $this->{$arrCallback[0]}->{$arrCallback[1]}(
  322.                             $this,
  323.                             $strHeadline,
  324.                             $objContent,
  325.                             $booClGroup
  326.                         );
  327.                         if ($strTmpTitle !== false && !is_null($strTmpTitle)) {
  328.                             $arrTitle['title'] = $strTmpTitle;
  329.                             break;
  330.                         }
  331.                     }
  332.                 }
  333.                 break;
  334.         }
  335.         if (!$arrTitle['title']) {
  336.             return $objContent;
  337.         }
  338.         $arrTitle['attribute'] = $GLOBALS['TL_LANG']['CTE'][$objContent->type][0];
  339.         return $arrTitle;
  340.     }
  341.     protected function _getHeadlineValue($objElem)
  342.     {
  343.         $arrHeadline deserialize($objElem->headlinetrue);
  344.         if ($arrHeadline['value']) {
  345.             return $arrHeadline['value'];
  346.         }
  347.         return false;
  348.     }
  349.     /**
  350.      * Get field array with all fields from given array
  351.      *
  352.      * @param string $strTable
  353.      *
  354.      * @return array
  355.      */
  356.     public function getFields(string $strTable): array
  357.     {
  358.         $arrTableMetaFields $this->getTableMetaFields($strTable);
  359.         $arrFields          = [];
  360.         foreach ($arrTableMetaFields as $key => $value) {
  361.             $arrFields[] = $key;
  362.         }
  363.         return $arrFields;
  364.     }
  365.     /**
  366.      * Write the field information from the given table string to an array and return it
  367.      *
  368.      * @param string $strTable
  369.      *
  370.      * @return array
  371.      */
  372.     public function getTableMetaFields(string $strTable): array
  373.     {
  374.         $fields $this->database->getFields($strTable);
  375.         $arrFieldMeta = [];
  376.         foreach ($fields as $value) {
  377.             if ($value["type"] == "index") {
  378.                 continue;
  379.             }
  380.             $arrFieldMeta[$value["name"]] = $value;
  381.         }
  382.         return $arrFieldMeta;
  383.     }
  384.     /**
  385.      * Get pid and new sorting for new element
  386.      *
  387.      * @param string $strTable
  388.      * @param int    $intPid
  389.      * @param string $strPastePos
  390.      *
  391.      * @return array
  392.      */
  393.     public function getNewPosition(string $strTableint $intPidstring $strPastePos): array
  394.     {
  395.         // Insert the current record at the beginning when inserting into the parent record.
  396.         if ($strPastePos == 'pasteInto') {
  397.             $newPid     $intPid;
  398.             $objSorting $this->database->getSorting($strTable$intPid);
  399.             // Select sorting value of the first record.
  400.             if ($objSorting->numRows) {
  401.                 $intCurSorting $objSorting->sorting;
  402.                 // Resort if the new sorting value is not an integer or smaller than 1.
  403.                 if (($intCurSorting 2) != || $intCurSorting 1) {
  404.                     $objNewSorting $this->database->getSortingElem($strTable$intPid);
  405.                     $count      2;
  406.                     $newSorting 128;
  407.                     while ($objNewSorting->next()) {
  408.                         $this->database->updateSorting($strTable, ($count++ * 128), $objNewSorting->id);
  409.                     }
  410.                 } // Else new sorting = (current sorting / 2).
  411.                 else {
  412.                     $newSorting = ($intCurSorting 2);
  413.                 }
  414.             } // Else new sorting = 128.
  415.             else {
  416.                 $newSorting 128;
  417.             }
  418.         } // Else insert the current record after the parent record
  419.         elseif ($strPastePos == 'pasteAfter' && $intPid 0) {
  420.             $objSorting $this->database->getDynamicObject($strTable$intPid);
  421.             // Set parent ID of the current record as new parent ID.
  422.             if ($objSorting->numRows) {
  423.                 $newPid        $objSorting->pid;
  424.                 $intCurSorting $objSorting->sorting;
  425.                 // Do not proceed without a parent ID.
  426.                 if (is_numeric($newPid)) {
  427.                     $objNextSorting $this->database->getNextSorting($strTable$newPid$intCurSorting);
  428.                     // Select sorting value of the next record.
  429.                     if ($objNextSorting->sorting !== null) {
  430.                         $intNextSorting $objNextSorting->sorting;
  431.                         // Resort if the new sorting value is no integer or bigger than a MySQL integer.
  432.                         if ((($intCurSorting $intNextSorting) % 2) != || $intNextSorting >= 4294967295) {
  433.                             $count 1;
  434.                             $objNewSorting $this->database->getSortingElem($strTable$newPid);
  435.                             while ($objNewSorting->next()) {
  436.                                 $this->database->updateSorting($strTable, ($count++ * 128), $objNewSorting->id);
  437.                                 if ($objNewSorting->sorting == $intCurSorting) {
  438.                                     $newSorting = ($count++ * 128);
  439.                                 }
  440.                             }
  441.                         } // Else new sorting = (current sorting + next sorting) / 2.
  442.                         else {
  443.                             $newSorting = (($intCurSorting $intNextSorting) / 2);
  444.                         }
  445.                     } // Else new sorting = (current sorting + 128).
  446.                     else {
  447.                         $newSorting = ($intCurSorting 128);
  448.                     }
  449.                 }
  450.             } // Use the given parent ID as parent ID.
  451.             else {
  452.                 $newPid     $intPid;
  453.                 $newSorting 128;
  454.             }
  455.         }
  456.         return ['pid' => intval($newPid), 'sorting' => intval($newSorting)];
  457.     }
  458.     /**
  459.      * Check if the content type exists in this system and return true or false
  460.      *
  461.      * @param array $arrSet
  462.      *
  463.      * @return boolean
  464.      */
  465.     public function existsContentType(array $arrSet): bool
  466.     {
  467.         foreach ($GLOBALS['TL_CTE'] as $group => $arrCElems) {
  468.             foreach ($arrCElems as $strCType => $strCDesc) {
  469.                 if (substr($arrSet['type'], 1, -1) == $strCType) {
  470.                     return true;
  471.                 }
  472.             }
  473.         }
  474.         return false;
  475.     }
  476.     /**
  477.      * Check if the module type exists in this system and return true or false
  478.      *
  479.      * @param array $arrSet
  480.      *
  481.      * @return boolean
  482.      */
  483.     public function existsModuleType(array $arrSet): bool
  484.     {
  485.         foreach ($GLOBALS['FE_MOD'] as $group => $arrMElems) {
  486.             foreach ($arrMElems as $strMType => $strMDesc) {
  487.                 if (substr($arrSet['type'], 1, -1) == $strMType) {
  488.                     return true;
  489.                 }
  490.             }
  491.         }
  492.         return false;
  493.     }
  494.     /**
  495.      * Return given file from path without file extension as array
  496.      *
  497.      * @param string $strFilePath
  498.      *
  499.      * @return array
  500.      */
  501.     public function getArrFromFileName(string $strFilePath): array
  502.     {
  503.         System::loadLanguageFile('default');
  504.         $arrFileInfo pathinfo($strFilePath);
  505.         $arrFileName explode(
  506.             ',',
  507.             $arrFileInfo['filename']
  508.         );
  509.         // Add flag of group for older xml filenames.
  510.         if (!in_array($arrFileName[4], ['G''NG'])) {
  511.             $arrFileName[5] = $arrFileName[4];
  512.             $arrFileName[4] = ((stristr(
  513.                                     $arrFileName[5],
  514.                                     standardize($GLOBALS['TL_LANG']['MSC']['clipboardGroup'])
  515.                                 ) === false) ? 'NG' 'G');
  516.         }
  517.         return $arrFileName;
  518.     }
  519. }