public static function loadTags($strTable, $intId = null)
 {
     if ($intId) {
         if (($objTag = \Contao\TagModel::findByIdAndTable($intId, $GLOBALS['TL_DCA'][$strTable]['fields']['tags']['eval']['table'] ?: $strTable)) !== null) {
             return array_values($objTag->fetchEach('tag'));
         }
     } else {
         if (($objTag = \Contao\TagModel::findBy('from_table', $GLOBALS['TL_DCA'][$strTable]['fields']['tags']['eval']['table'] ?: $strTable)) !== null) {
             return array_values($objTag->fetchEach('tag'));
         }
     }
 }
 public function saveTagsFromDefaults(\DataContainer $dc)
 {
     $arrDefaults = $dc->getDefaults();
     $arrDca = $dc->getDca();
     if (empty($arrDefaults)) {
         return false;
     }
     foreach ($arrDefaults as $strField => $varDefault) {
         $arrData = $arrDca['fields'][$strField];
         if (!is_array($arrData) || $arrData['inputType'] != 'tag') {
             continue;
         }
         $arrValues = trimsplit(',', $varDefault);
         $blnTag = false;
         if (!is_array($arrValues) || empty($arrValues)) {
             $blnTag = false;
         } else {
             $objTags = TagModel::findByIdAndTable($dc->activeRecord->id, $dc->table);
             $arrSavedTags = array();
             if ($objTags !== null) {
                 $arrSavedTags = $objTags->fetchEach('tag');
                 $blnTag = true;
             }
             $arrNewTags = array_diff($arrValues, $arrSavedTags);
             if (is_array($arrNewTags) && !empty($arrNewTags)) {
                 $blnTag = true;
                 foreach ($arrNewTags as $strTag) {
                     $objModel = new TagModel();
                     $objModel->tag = $strTag;
                     $objModel->from_table = $dc->table;
                     $objModel->tid = $dc->activeRecord->id;
                     $objModel->save();
                 }
             }
         }
         $dc->activeRecord->{$strField} = $blnTag;
         $dc->activeRecord->save();
     }
 }
 public function __construct($arrAttributes = null)
 {
     parent::__construct($arrAttributes);
     $dca = $GLOBALS['TL_DCA'][$arrAttributes['tagTable']];
     $this->strTable = $dca['fields']['tags']['eval']['optionsTable'] ?: ($dca['fields']['tags']['eval']['table'] ?: $arrAttributes['tagTable']);
     $arrOptions = $dca['fields']['tags']['options'];
     if ($arrOptions && is_array($arrOptions)) {
         $this->arrOptions = array();
         if ($dca['fields']['tags']['eval']['includeBlankOption']) {
             $this->arrOptions = array(array('value' => '', 'label' => '-'));
         }
         foreach ($arrOptions as $strValue => $strLabel) {
             $this->arrOptions[] = array('label' => $strLabel, 'value' => $strValue, 'group' => false);
         }
     } elseif (($objTags = TagModel::findByfrom_table($this->strTable)) !== null) {
         $this->arrOptions = array();
         while ($objTags->next()) {
             if (!in_array(array('label' => $objTags->tag, 'value' => $objTags->tag, 'group' => false), $this->arrOptions)) {
                 $this->arrOptions[] = array('label' => $objTags->tag, 'value' => $objTags->tag, 'group' => false);
             }
         }
     }
 }