* refactored Repository (moved methods from blogexample to extmvc)
authorJochen Rau <j.rau@web.de>
Sun, 1 Feb 2009 09:23:05 +0000 (09:23 +0000)
committerJochen Rau <j.rau@web.de>
Sun, 1 Feb 2009 09:23:05 +0000 (09:23 +0000)
* first sketch of LLL handling (not finished yet)

typo3/sysext/extbase/Classes/Persistence/TX_EXTMVC_Persistence_Repository.php
typo3/sysext/extbase/Classes/View/TX_EXTMVC_View_AbstractView.php
typo3/sysext/extbase/Classes/View/TX_EXTMVC_View_TemplateView.php

index f36d87c..34b3771 100644 (file)
@@ -70,7 +70,7 @@ class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_Reposito
         * @var array
         */
        protected $findBy = array();
-
+       
        /**
         * The content object
         *
@@ -87,10 +87,11 @@ class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_Reposito
                $this->objects = new TX_EXTMVC_Persistence_ObjectStorage();
                $this->cObj = t3lib_div::makeInstance('tslib_cObj');
                $repositoryClassName = get_class($this);
+               // the session object is a singleton
                $this->session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session');
                $this->session->registerRepository($repositoryClassName);
-               if (substr($repositoryClassName,-10) == 'Repository' && substr($repositoryClassName,-11,1) != '_') {
-                       $this->aggregateRootClassName = substr($repositoryClassName,0,-10);
+               if (substr($repositoryClassName, -10) == 'Repository' && substr($repositoryClassName, -11, 1) != '_') {
+                       $this->aggregateRootClassName = substr($repositoryClassName, 0, -10);
                }
                // TODO check if the table exists in the database
                $this->tableName = strtolower($this->aggregateRootClassName);
@@ -191,7 +192,7 @@ class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_Reposito
         * @author Jochen Rau <jochen.rau@typoplanet.de>
         */
        public function findAll() {
-               return $this->reconstituteObjects($this->fetchFromDatabase());
+               return $this->reconstituteObjects($this->fetch($this->getTableName()));
        }
        
        /**
@@ -204,7 +205,7 @@ class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_Reposito
         */
        private function findByProperty($propertyName, $arguments) {
                $where = $propertyName . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($arguments[0], $this->tableName);
-               return $this->reconstituteObjects($this->fetch($this->tableName, $where));
+               return $this->reconstituteObjects($this->fetch($this->getTableName(), $where));
        }
        
        /**
@@ -257,7 +258,7 @@ class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_Reposito
                        );
                // TODO language overlay; workspace overlay
                return $rows ? $rows : array();         
-       }       
+       }
        
        /**
         * Dispatches the reconstitution of a domain object to an appropriate method
@@ -326,35 +327,49 @@ class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_Reposito
        }
        
        /**
-        * Deletes all removed objects from the database
-        * This is only a template method to be overwritten in extending classes
+        * Deletes all removed objects from the database.
         *
         * @return void
         * @author Jochen Rau <jochen.rau@typoplanet.de>
         */
        protected function deleteRemoved() {
+               $removedObjects = $this->session->getRemovedObjects($this->getAggregateRootClassName());
+               // FIXME remove debug code
+               // debug($removedObjects, 'removed objects');
+
+               foreach ($removedObjects as $object) {
+                       $res = $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->getTableName(), 'uid=' . $object->getUid());
+               }
        }
        
        /**
-        * Inserts all newly created objects to the database
-        * This is only a template method to be overwritten in extending classes
+        * Inserts all added objects in the database.
         *
         * @return void
         * @author Jochen Rau <jochen.rau@typoplanet.de>
         */
        protected function insertAdded() {
+               $addedObjects = $this->session->getAddedObjects($this->getAggregateRootClassName());
+
+               // FIXME remove debug code
+               // debug($addedObjects, 'added objects');
+               
+               foreach ($addedObjects as $object) {
+                       $row = array(
+                               'pid' => 0, // FIXME
+                               'tstamp' => time(),
+                               'crdate' => time(),
+                               // FIXME static fields
+                               'name' => $object->getName(),
+                               'description' => $object->getDescription()
+                               // 'logo' => $object->getLogo(),
+                               );
+                       $res = $GLOBALS['TYPO3_DB']->exec_INSERTquery(
+                               $this->getTableName(),
+                               $row
+                               );
+               }
        }
        
-       /**
-        * Updates all modified objects
-        * This is only a template method to be overwritten in extending classes
-        *
-        * @return void
-        * @author Jochen Rau <jochen.rau@typoplanet.de>
-        */
-       protected function updateDirty() {
-       }
-       
-       
 }
 ?>
\ No newline at end of file
index 97309e4..b1e080a 100755 (executable)
@@ -47,7 +47,43 @@ abstract class TX_EXTMVC_View_AbstractView implements TX_EXTMVC_View_ViewInterfa
        protected $contextVariables = array();
 
        /**
-        * Constructs the view.
+        * @var string
+        */
+       protected $languagePath = 'Resources/Language/';
+
+       /**
+        * Local Language content
+        *
+        * @var string
+        **/
+       protected $LOCAL_LANG = array();
+
+       /**
+        * Local Language content charset for individual labels (overriding)
+        *
+        * @var string
+        **/
+       protected $LOCAL_LANG_charset = array();
+
+       /**
+        * Key of the language to use
+        *
+        * @var string
+        **/
+       protected $languageKey = 'default';
+
+       /**
+        * Pointer to alternative fall-back language to use
+        *
+        * @var string
+        **/
+       protected $LLkey = 'default';
+       var $altLLkey='';                       // .
+       var $LLtestPrefix='';                   // You can set this during development to some value that makes it easy for you to spot all labels that ARe delivered by the getLL function.
+       var $LLtestPrefixAlt='';                // Save as LLtestPrefix, but additional prefix for the alternative value in getLL() function calls
+
+       /**
+        * Constructs the view
         */
        public function __construct() {
        }
@@ -60,6 +96,7 @@ abstract class TX_EXTMVC_View_AbstractView implements TX_EXTMVC_View_ViewInterfa
         */
        public function initializeObject() {
                $this->initializeView();
+               $this->initializeLocalization();
        }
 
        /**
@@ -102,6 +139,72 @@ abstract class TX_EXTMVC_View_AbstractView implements TX_EXTMVC_View_ViewInterfa
         */
        protected function initializeView() {
        }
+       
+       /**
+        * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
+        * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
+        *
+        * @return      void
+        */
+       protected function initializeLocalization()     {
+                       $languageFilePath = t3lib_extMgm::extPath(strtolower($this->request->getControllerExtensionKey())) . $this->languagePath . 'locallang.php';
+
+                       if ($GLOBALS['TSFE']->config['config']['language'])     {
+                               $this->languageKey = $GLOBALS['TSFE']->config['config']['language'];
+                               if ($GLOBALS['TSFE']->config['config']['language_alt']) {
+                                       $this->alternativeLanguageKey = $GLOBALS['TSFE']->config['config']['language_alt'];
+                               }
+                       }
+
+                       // Read the strings in the required charset (since TYPO3 4.2)
+                       $this->LOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->languageKey, $GLOBALS['TSFE']->renderCharset);
+                       if ($this->alternativeLanguageKey)      {
+                               $tempLOCAL_LANG = t3lib_div::readLLfile($languageFilePath, $this->alternativeLanguageKey);
+                               $this->LOCAL_LANG = array_merge(is_array($this->LOCAL_LANG) ? $this->LOCAL_LANG : array(), $tempLOCAL_LANG);
+                       }
+
+                       // TODO Overlaying labels from TypoScript (including fictitious language keys for non-system languages!):
+                       if (is_array($this->conf['_LOCAL_LANG.']))      {
+                               reset($this->conf['_LOCAL_LANG.']);
+                               while(list($k,$lA)=each($this->conf['_LOCAL_LANG.']))   {
+                                       if (is_array($lA))      {
+                                               $k = substr($k,0,-1);
+                                               foreach($lA as $llK => $llV)    {
+                                                       if (!is_array($llV))    {
+                                                               $this->LOCAL_LANG[$k][$llK] = $llV;
+                                                                       // For labels coming from the TypoScript (database) the charset is assumed to be "forceCharset" and if that is not set, assumed to be that of the individual system languages
+                                                               $this->LOCAL_LANG_charset[$k][$llK] = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : $GLOBALS['TSFE']->csConvObj->charSetArray[$k];
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+       }
+       
+       /**
+        * Returns the localized label of the LOCAL_LANG key, $key
+        * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
+        *
+        * @param       string          The key from the LOCAL_LANG array for which to return the value.
+        * @param       string          Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
+        * @param       boolean         If true, the output label is passed through htmlspecialchars()
+        * @return      string          The value from LOCAL_LANG.
+        */
+       function translate($key, $default = '', $hsc=FALSE)     {
+               // The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
+               if (isset($this->LOCAL_LANG[$this->languageKey][$key])) {
+                       $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->languageKey][$key], $this->LOCAL_LANG_charset[$this->languageKey][$key]);
+               } elseif ($this->alternativeLanguageKey && isset($this->LOCAL_LANG[$this->alternativeLanguageKey][$key]))       {
+                       $translation = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->alternativeLanguageKey][$key], $this->LOCAL_LANG_charset[$this->alternativeLanguageKey][$key]);
+               } elseif (isset($this->LOCAL_LANG['default'][$key]))    {
+                       $translation = $this->LOCAL_LANG['default'][$key];      // No charset conversion because default is english and thereby ASCII
+               } else {
+                       $translation = $default;
+               }
+
+               return $hsc ? htmlspecialchars($translation) : $translation;
+       }
+       
 
        /**
         * Assigns domain models (single objects or aggregates) or values to the view
index 79aa9b2..b41320a 100644 (file)
@@ -207,21 +207,30 @@ class TX_EXTMVC_View_TemplateView extends TX_EXTMVC_View_AbstractView {
                return $markerArray;
        }
                
-       protected function getMarkerContent($markerName, $value) {
+       protected function getMarkerContent($marker, $value) {
+               $explodedMarker = explode(':', $marker);
+               if (count($explodedMarker) == 2) {
+                       $command = $explodedMarker[0];
+                       $markerName = $explodedMarker[1];
+               } else {
+                       $markerName = $marker;
+               }
+
+               if ($command === 'LLL') {
+                       $result = $this->translate(strtolower($markerContent));
+               }
+               // FIXME
                $explodedMarkerName = explode('.', $markerName);
-               $possibleMethodName = 'get' . TX_EXTMVC_Utility_Strings::underscoredToUpperCamelCase($explodedMarkerName[1]);
                if ($value === NULL) {
                        if (!empty($this->contextVariables[strtolower($markerName)])) {
                                $result = $this->contextVariables[strtolower($markerName)];
                        }
-               } elseif ($value instanceof TX_EXTMVC_DomainObject_AbstractDomainObject) {
-                       $explodedMarkerName = explode('.', $markerName);
+               } elseif (count($explodedMarkerName) == 2 && $value instanceof TX_EXTMVC_DomainObject_AbstractDomainObject) {
                        $possibleMethodName = 'get' . TX_EXTMVC_Utility_Strings::underscoredToUpperCamelCase($explodedMarkerName[1]);
                        if (method_exists($value, $possibleMethodName)) {
                                $result = $value->$possibleMethodName();
                        }
                } else {
-                       $result = $value;
                }
                return $this->convertValue($result);
        }