* Moved method getPath to abstract and added method to quickly write EXTCONF in localconf
authorChristian Opitz <christian.opitz@netresearch.de>
Wed, 5 Sep 2012 07:29:28 +0000 (07:29 +0000)
committerChristian Opitz <christian.opitz@netresearch.de>
Fri, 31 May 2013 13:22:24 +0000 (15:22 +0200)
* Added provider to export static datastructures from templavoila ds records

provider/class.abstract.php
provider/class.tr2tf.php
provider/class.tvstaticds.php [new file with mode: 0644]

index 779b98b..8189ce4 100644 (file)
@@ -290,4 +290,90 @@ abstract class tx_t3build_provider_abstract {
             echo PHP_EOL;
         }
     }
+
+    /**
+     * Write config to extConf
+     * @param string $extKey
+     * @param array $update
+     */
+    protected function writeExtConf($extKey, array $update)
+    {
+        global $TYPO3_CONF_VARS;
+
+        $absPath = t3lib_extMgm::extPath($extKey);
+        $relPath = t3lib_extMgm::extRelPath($extKey);
+
+        /* @var $tsStyleConfig t3lib_tsStyleConfig */
+       $tsStyleConfig = t3lib_div::makeInstance('t3lib_tsStyleConfig');
+               $theConstants = $tsStyleConfig->ext_initTSstyleConfig(
+                       t3lib_div::getUrl($absPath . 'ext_conf_template.txt'),
+                       $absPath,
+                       $relPath,
+                       ''
+               );
+
+               $arr = @unserialize($TYPO3_CONF_VARS['EXT']['extConf'][$extKey]);
+               $arr = is_array($arr) ? $arr : array();
+
+                       // Call processing function for constants config and data before write and form rendering:
+               if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['typo3/mod/tools/em/index.php']['tsStyleConfigForm'])) {
+                       $_params = array('fields' => &$theConstants, 'data' => &$arr, 'extKey' => $extKey);
+                       foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['typo3/mod/tools/em/index.php']['tsStyleConfigForm'] as $_funcRef) {
+                               t3lib_div::callUserFunction($_funcRef, $_params, $this);
+                       }
+                       unset($_params);
+               }
+
+
+        $arr = t3lib_div::array_merge_recursive_overrule($arr, $update);
+
+               /* @var $instObj t3lib_install */
+               $instObj = t3lib_div::makeInstance('t3lib_install');
+               $instObj->allowUpdateLocalConf = 1;
+               $instObj->updateIdentity = 'TYPO3 Extension Manager';
+
+               // Get lines from localconf file
+               $lines = $instObj->writeToLocalconf_control();
+               $instObj->setValueInLocalconfFile($lines, '$TYPO3_CONF_VARS[\'EXT\'][\'extConf\'][\'' . $extKey . '\']', serialize($arr)); // This will be saved only if there are no linebreaks in it !
+               $instObj->writeToLocalconf_control($lines);
+
+               t3lib_extMgm::removeCacheFiles();
+    }
+
+    /**
+     * Parses $vars into a path mask and makes it FS-safe
+     *
+     * @param string $mask
+     * @param array $vars
+     * @param string $renameMode
+     * @return string
+     */
+    protected function getPath($mask, $vars, $renameMode = 'camelCase')
+    {
+        $replace = array();
+        foreach ($vars as $key => $value) {
+            $replace[] = '${'.$key.'}';
+        }
+        $path = str_replace($replace, $vars, $mask);
+        if (preg_match('/\$\{([^\}]*)\}/', $path, $res)) {
+            $this->_die('Unknown var "'.$res[1].'" in path mask');
+        }
+        $path = strtolower($path);
+        $path = str_replace(':', '-', $path);
+        $path = preg_replace('#[^A-Za-z0-9/\-_\.]+#', ' ', $path);
+        $path = preg_replace('#\s*/+\s*#', '/', $path);
+        $parts = explode(' ', $path);
+        if ($renameMode == 'underscore') {
+            $path = implode('_', $parts);
+        } else {
+            $path = '';
+            $uc = false;
+            foreach ($parts as $part) {
+                $ucPart = ucfirst($part);
+                $path .= ($uc || $renameMode === 'CamelCase') ? $ucPart : $part;
+                $uc = $ucPart != $part;
+            }
+        }
+        return $path;
+    }
 }
index 897eaa3..61b55e0 100644 (file)
@@ -1,4 +1,11 @@
 <?php
+/**
+ * Provider to move typoscript setup and config from records to
+ * files which can be named flexibly
+ *
+ * @package t3build
+ * @author Christian Opitz <co@netzelf.de>
+ */
 class tx_t3build_provider_tr2tf extends tx_t3build_provider_abstract
 {
     /**
@@ -193,7 +200,9 @@ class tx_t3build_provider_tr2tf extends tx_t3build_provider_abstract
         $content = file_exists($file) ? file_get_contents($file) : "<?php\n?>";
         $i = 0;
         foreach ($this->staticDirs as $uid => $dir) {
-            $pattern = "/t3lib_extMgm\:\:addStaticFile\s*\(\s*[\"']$this->extKey[\"']\s*,\s*[\"']".preg_quote($dir, '/').'["\'][^\)]+\)\s*;\s*/';
+            $pattern = "#t3lib_extMgm\:\:addStaticFile\s*\(\s*[\"']$this->extKey[\"']\s*,\s*[\"']";
+            $pattern .= preg_quote($dir, '#');
+            $pattern .= '["\'][^\)]+\)\s*;\s*(//.*$)?#m';
             preg_replace($pattern, '', $content);
             $row = $this->rows[$uid];
             $title = $row->title;
@@ -238,31 +247,6 @@ class tx_t3build_provider_tr2tf extends tx_t3build_provider_abstract
         return $this->rootlines[$id];
     }
 
-    protected function getPath($vars)
-    {
-        $replace = array();
-        foreach ($vars as $key => $value) {
-            $replace[] = '${'.$key.'}';
-        }
-        $path = str_replace($replace, $vars, $this->pathMask);
-        if (preg_match('/\$\{([^\}]*)\}/', $path, $res)) {
-            $this->_die('Unknown var "'.$res[1].'" in path mask');
-        }
-        $path = strtolower($path);
-        $path = preg_replace('#[^a-z0-9/-_\.]+#i', ' ', $path);
-        $path = preg_replace('#\s*/+\s*#', '/', $path);
-        $parts = explode(' ', $path);
-        if ($this->renameMode == 'underscore') {
-            $path = implode('_', $parts);
-        } else {
-            $path = array_shift($parts);
-            foreach ($parts as $part) {
-                $path .= ucfirst($part);
-            }
-        }
-        return $path;
-    }
-
     protected function _collect($row)
     {
         $rootline = $this->getRootline($row->pid);
@@ -293,7 +277,7 @@ class tx_t3build_provider_tr2tf extends tx_t3build_provider_abstract
             $count++;
             $vars['type'] = $type;
             $vars['column'] = $column;
-            $path = $this->getPath($vars);
+            $path = $this->getPath($this->pathMask, $vars, $this->renameMode);
             if (in_array($path, $this->templateFiles)) {
                 $msg = 'Path "'.$path.'" already in use for template record '.$this->templateFiles['uid'].' - ';
                 $msg .= 'try renaming one of those records';
diff --git a/provider/class.tvstaticds.php b/provider/class.tvstaticds.php
new file mode 100644 (file)
index 0000000..3b55199
--- /dev/null
@@ -0,0 +1,161 @@
+<?php
+/**
+ * Provider to copy templavoila datastructure to files and link
+ * them with the template objects
+ *
+ * @package t3build
+ * @author Christian Opitz <co@netzelf.de>
+ */
+class tx_t3build_provider_tvstaticds extends tx_t3build_provider_abstract
+{
+    /**
+     * The extension to which the files should be written
+     * @arg
+     * @var string
+     */
+    protected $extKey = 'template';
+
+    /**
+     * Export only records with this pid
+     * @arg
+     * @required
+     * @var int
+     */
+    protected $pid = null;
+
+    /**
+     * The mask of the path within the extension the files will be
+     * exportet to. Following variables are available:
+     * ${scope} fce or page
+     * ${title} title of the ds record
+     *
+     * Add a feature request/patch at forge.typo3.org if you need more
+     *
+     * @arg
+     * @var string
+     */
+    protected $pathMask = 'ds/${scope}/${title}.xml';
+
+    /**
+     * Whether to include deleted ds
+     * @arg
+     * @var boolean
+     */
+    protected $includeDeletedRecords = false;
+
+    /**
+     * If the content should be appended to eventually existing files
+     * @arg
+     * @var boolean
+     */
+    protected $append = true;
+
+    /**
+     * If existing files should be overwritten
+     * @var boolean
+     */
+    protected $overwrite = false;
+
+    /**
+     * If the tmplobj records should be updated after export
+     * (set datastructure to file)
+     * @arg
+     * @var boolean
+     */
+    protected $update = true;
+
+    /**
+     * If the datastructure records should be deleted after export
+     * @arg
+     * @var boolean
+     */
+    protected $delete = true;
+
+    /**
+     * Renaming mode: 'camelCase', 'CamelCase' or 'under_scored'
+     * @arg
+     * @var string
+     */
+    protected $renameMode = 'camelCase';
+
+    /**
+     * @var t3lib_DB
+     */
+    protected $db;
+
+    protected $columns = array(
+        'setup' => 'config',
+        'constants' => 'constants'
+    );
+
+    protected $rows = array();
+
+    protected $rootlines = array();
+
+    protected $templateFiles = array();
+
+    protected $staticDirs = array();
+
+    public function tvstaticdsAction()
+    {
+        if (!t3lib_extMgm::isLoaded('templavoila')) {
+            $this->_die('templavoila is not loaded');
+        }
+        $this->db = $GLOBALS['TYPO3_DB'];
+
+        $extPath = PATH_typo3conf.'ext/'.$this->extKey.'/';
+        $extRelPath = 'typo3conf/ext/'.$this->extKey.'/';
+
+        if (!file_exists($extPath)) {
+            t3lib_div::mkdir($extPath);
+        }
+
+        $paths = array();
+
+        $where = 'pid='.$this->pid.($this->includeDeletedRecords ? '' : ' AND deleted = 0');
+        $rows = $this->db->exec_SELECTgetRows('*', 'tx_templavoila_datastructure', $where);
+
+        if (!count($rows)) {
+            $this->_die('No records found on page '.$this->pid);
+        }
+
+        foreach ($rows as $row) {
+            $file = $this->getPath($this->pathMask, array(
+               'scope' => $row['scope'] === '1' ? 'page' : 'fce',
+                'title' => str_replace(array('/', '\\'), '-', $row['title'])
+            ), $this->renameMode);
+            $path = $extPath.$file;
+            if (!file_exists(dirname($path))) {
+                if (t3lib_div::mkdir_deep($extPath, dirname($file))) {
+                    $this->_die('Could not make directory '.$path);
+                }
+            }
+            if (!file_exists($path) || $this->overwrite) {
+                file_put_contents($path, $row['dataprot']);
+            }
+            if ($this->update) {
+                $this->db->exec_UPDATEquery(
+                       'tx_templavoila_tmplobj',
+                       'datastructure='.$row['uid'],
+                    array('datastructure' => $extRelPath.$file)
+                );
+            }
+            if ($this->delete) {
+                $this->db->exec_UPDATEquery(
+                       'tx_templavoila_datastructure',
+                       'uid='.$row['uid'],
+                    array('deleted' => 1)
+                );
+            }
+        }
+
+
+        $conf = array('enable' => 1);
+        foreach (array('page', 'fce') as $scope) {
+            $file = $this->getPath($this->pathMask, array('scope' => $scope, 'title' => 'foo'), $this->renameMode);
+            $conf['path_'.$scope] = $extRelPath.dirname($file).'/';
+        }
+
+        $this->writeExtConf('templavoila', array('staticDS.' => $conf));
+    }
+}
\ No newline at end of file