Example #1
0
 /**
  * Parses raw data array from db and replace keys with matching values (select
  * fields) like country in address data
  *
  * @param array $data Address data
  * @param array $typoScript TypoScript for addresshandling for this type
  *
  * @return array Address data
  * @throws Exception If configuration is not correct
  */
 public function parseRawData(array $data = array(), array $typoScript)
 {
     if (!is_array($data)) {
         return array();
     }
     $database = $this->getDatabaseConnection();
     $this->debug($typoScript, '$typoScript', __FILE__ . ' ' . __LINE__);
     $newdata = array();
     foreach ($data as $key => $value) {
         $newdata[$key] = $value;
         $fieldConfig = $typoScript[$key . '.'];
         // Get the value from database if the field is a select box
         if (in_array($fieldConfig['type'], array('select', 'static_info_country')) && strlen($fieldConfig['table'])) {
             $table = $fieldConfig['table'];
             $select = $fieldConfig['value'] . ' = ' . $database->fullQuoteStr($value, $table) . $this->cObj->enableFields($table);
             $fields = $fieldConfig['label'] . ' AS label,';
             $fields .= $fieldConfig['value'] . ' AS value';
             $res = $database->exec_SELECTquery($fields, $table, $select);
             $value = $database->sql_fetch_assoc($res);
             $newdata[$key] = $value['label'];
         } elseif ($fieldConfig['type'] == 'select' && is_array($fieldConfig['values.'])) {
             $newdata[$key] = $fieldConfig['values.'][$value];
         } elseif ($fieldConfig['type'] == 'select') {
             throw new Exception('Neither table nor value-list defined for select field ' . $key, 1304333953);
         }
         if ($fieldConfig['type'] == 'static_info_tables') {
             $field = $fieldConfig['field'];
             $valueHidden = $this->staticInfo->getStaticInfoName($field, $value);
             $newdata[$key] = $valueHidden;
         }
     }
     return $newdata;
 }
    /**
     * Returns the listing HTML of addresses.
     *
     * @param int $addressType Type of addresses that should be returned.
     * 		If this is 0 all types will be returned
     * @param bool $createHiddenFields Create hidden fields
     * @param string $hiddenFieldPrefix Prefix for field names
     * @param bool $selectAddressId Adress ID which should be selected by default
     *
     * @throws \Exception Thrown if form fields are undefined
     * @return string HTML with addresses
     */
    public function getListing($addressType = 0, $createHiddenFields = FALSE, $hiddenFieldPrefix = '', $selectAddressId = FALSE)
    {
        $hookObjectsArr = array();
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/pi4/class.tx_commerce_pi4.php']['getListing'])) {
            \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('
				hook
				$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'commerce/pi4/class.tx_commerce_pi4.php\'][\'getListing\']
				is deprecated since commerce 1.0.0, it will be removed in commerce 1.4.0, please use instead
				$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'commerce/Classes/Controller/AddressesController.php\'][\'getListing\']
			');
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/pi4/class.tx_commerce_pi4.php']['getListing'] as $classRef) {
                $hookObjectsArr[] = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($classRef);
            }
        }
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/Classes/Controller/AddressesController.php']['getListing'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/Classes/Controller/AddressesController.php']['getListing'] as $classRef) {
                $hookObjectsArr[] = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($classRef);
            }
        }
        if ($this->conf[$addressType . '.']['subpartMarker.']['listWrap']) {
            $tplBase = $this->cObj->getSubpart($this->templateCode, strtoupper($this->conf[$addressType . '.']['subpartMarker.']['listWrap']));
        } else {
            $tplBase = $this->cObj->getSubpart($this->templateCode, '###ADDRESS_LISTING###');
        }
        if ($this->conf[$addressType . '.']['subpartMarker.']['listItem']) {
            $tplItem = $this->cObj->getSubpart($this->templateCode, strtoupper($this->conf[$addressType . '.']['subpartMarker.']['listItem']));
        } else {
            $tplItem = $this->cObj->getSubpart($this->templateCode, '###ADDRESS_ITEM###');
        }
        if (!is_array($this->conf['formFields.'])) {
            if (is_array($this->conf[$addressType . '.']['formFields.'])) {
                $this->conf['formFields.'] = $this->conf[$addressType . '.']['formFields.'];
            }
        }
        // Set prefix if not set
        if (empty($hiddenFieldPrefix)) {
            $hiddenFieldPrefix = $this->prefixId;
        }
        $editAddressId = 0;
        if ($this->piVars['addressid']) {
            // Set var editAddressId for checked
            $editAddressId = (int) $this->piVars['addressid'];
        } elseif ($selectAddressId) {
            $editAddressId = (int) $selectAddressId;
        }
        // Unset some piVars we don't need here
        unset($this->piVars['check']);
        unset($this->piVars['addressid']);
        unset($this->piVars['ismainaddress']);
        foreach ($this->fieldList as $name) {
            unset($this->piVars[$name]);
        }
        // Get all addresses for the desired address types
        $addressTypes = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['selectAddressTypes']);
        // Count different address types
        $addressTypeCounter = array();
        foreach ($this->addresses as $address) {
            $addressTypeCounter[$address['tx_commerce_address_type_id']]++;
        }
        $addressItems = '';
        $addressFound = FALSE;
        foreach ($this->addresses as $address) {
            if ($addressType > 0 && $address['tx_commerce_address_type_id'] != $addressType) {
                continue;
            }
            $itemMarkerArray = array();
            $linkMarkerArray = array();
            // Fill marker array
            $address = Tx_Commerce_Utility_GeneralUtility::removeXSSStripTagsArray($address);
            foreach ($address as $key => $value) {
                $valueHidden = '';
                $upperKey = strtoupper($key);
                if ($this->conf['hideEmptyFields'] && empty($value)) {
                    continue;
                }
                if ($value === '') {
                    $value = $this->conf['emptyFieldSign'];
                }
                // Get value from database if the field is a select box
                if ($this->conf['formFields.'][$key . '.']['type'] == 'select' && strlen($this->conf['formFields.'][$key . '.']['table']) > 0) {
                    $fieldConfig = $this->conf['formFields.'][$key . '.'];
                    $table = $fieldConfig['table'];
                    $select = $fieldConfig['value'] . '=\'' . $value . '\'' . $this->cObj->enableFields($fieldConfig['table']);
                    $fields = $fieldConfig['label'] . ' AS label,';
                    $fields .= $fieldConfig['value'] . ' AS value';
                    $value = $this->getDatabaseConnection()->exec_SELECTgetSingleRow($fields, $table, $select);
                    $valueHidden = $value['value'];
                    $value = $value['label'];
                } elseif ($this->conf['formFields.'][$key . '.']['type'] == 'select' && is_array($this->conf['formFields.'][$key . '.']['values.'])) {
                    $valueHidden = $value;
                    $value = $this->conf['formFields.'][$key . '.']['values.'][$value];
                } elseif ($this->conf['formFields.'][$key . '.']['type'] == 'select') {
                    throw new \Exception('Neither table nor value-list defined for select field ' . $key, 1304333953);
                }
                if ($this->conf['formFields.'][$key . '.']['type'] == 'static_info_tables') {
                    $fieldConfig = $this->conf['formFields.'][$key . '.'];
                    $field = $fieldConfig['field'];
                    $valueHidden = $value;
                    $value = $this->staticInfo->getStaticInfoName($field, $value);
                }
                $hidden = '';
                if ($createHiddenFields) {
                    $hidden = '<input type="hidden" name="' . $hiddenFieldPrefix . '[' . $address['uid'] . '][' . $key . ']" value="' . ($valueHidden ? $valueHidden : $value) . '" />';
                }
                $itemMarkerArray['###LABEL_' . $upperKey . '###'] = $this->pi_getLL('label_' . $key);
                $itemMarkerArray['###' . $upperKey . '###'] = $value . $hidden;
            }
            // Create a pivars array for merging with link to edit page
            if ($this->conf['editAddressPid'] > 0) {
                $piArray = array('backpid' => $GLOBALS['TSFE']->id);
                $linkTarget = $this->conf['editAddressPid'];
            } else {
                $piArray = array('backpid' => $GLOBALS['TSFE']->id);
                $linkTarget = $this->conf['addressMgmPid'];
            }
            // Set delete link only if addresses may be deleted, otherwise set it empty
            if ((int) $addressTypeCounter[$address['tx_commerce_address_type_id']] > (int) $this->conf['minAddressCount']) {
                $linkMarkerArray['###LINK_DELETE###'] = explode('|', $this->pi_linkTP_keepPIvars('|', array('action' => 'delete', 'addressid' => $address['uid'])));
                $itemMarkerArray['###LABEL_LINK_DELETE###'] = $this->cObj->stdWrap($this->pi_getLL('label_link_delete'), $this->conf['deleteLinkWrap.']);
            } else {
                $linkMarkerArray['###LINK_DELETE###'][0] = '';
                $linkMarkerArray['###LINK_DELETE###'][1] = '';
                $itemMarkerArray['###LABEL_LINK_DELETE###'] = '';
            }
            $linkMarkerArray['###LINK_EDIT###'] = explode('|', $this->pi_linkTP_keepPIvars('|', array_merge($piArray, array('action' => 'edit', 'addressid' => $address['uid'], 'addressType' => $address['tx_commerce_address_type_id'])), FALSE, FALSE, $linkTarget));
            $itemMarkerArray['###LABEL_LINK_EDIT###'] = $this->cObj->stdWrap($this->pi_getLL('label_link_edit'), $this->conf['editLinkWrap.']);
            // add an edit radio button, checked selected previously
            $itemMarkerArray['###SELECT###'] = '<input type="radio" ';
            if ($editAddressId == $address['uid'] || empty($editAddressId) && $address['tx_commerce_is_main_address']) {
                $itemMarkerArray['###SELECT###'] .= 'checked="checked" ';
            }
            $itemMarkerArray['###SELECT###'] .= 'name="' . $hiddenFieldPrefix . '[address_uid]" value="' . $address['uid'] . '" />';
            foreach ($hookObjectsArr as $hookObj) {
                if (method_exists($hookObj, 'processAddressMarker')) {
                    $itemMarkerArray = $hookObj->processAddressMarker($itemMarkerArray, $address, $piArray, $this);
                }
            }
            $addressFound = TRUE;
            $addressItems[$address['tx_commerce_address_type_id']] .= $this->substituteMarkerArrayNoCached($tplItem, $itemMarkerArray, array(), $linkMarkerArray);
        }
        $linkMarkerArray = array();
        // Create a pivars array for merging with link to edit page
        if ($this->conf['editAddressPid'] > 0) {
            $piArray = array('backpid' => $GLOBALS['TSFE']->id);
            $linkTarget = $this->conf['editAddressPid'];
        } else {
            $piArray = array();
            $linkTarget = $this->conf['addressMgmPid'];
        }
        // Create links and labels for every address type
        if ($addressType == 0) {
            foreach ($addressTypes as $addressType) {
                $baseMarkerArray['###ADDRESS_ITEMS_OF_TYPE_' . $addressType . '###'] = $addressItems[$addressType];
                $baseMarkerArray['###LABEL_ADDRESSES_OF_TYPE_' . $addressType . '###'] = $this->pi_getLL('label_addresses_of_type_' . $addressType);
                $linkMarkerArray['###LINK_NEW_TYPE_' . $addressType . '###'] = explode('|', $this->pi_linkTP_keepPIvars('|', array_merge($piArray, array('action' => 'new', 'addressType' => $addressType)), FALSE, FALSE, $linkTarget));
                $baseMarkerArray['###LABEL_LINK_NEW_TYPE_' . $addressType . '###'] = $this->cObj->stdWrap($this->pi_getLL('label_link_new_type_' . $addressType), $this->conf['newLinkWrap.']);
            }
        } else {
            $baseMarkerArray['###ADDRESS_ITEMS###'] = $addressItems[$addressType];
            $linkMarkerArray['###LINK_NEW###'] = explode('|', $this->pi_linkTP_keepPIvars('|', array_merge($piArray, array('action' => 'new', 'addressType' => $addressType)), FALSE, FALSE, $linkTarget));
            $baseMarkerArray['###LABEL_LINK_NEW###'] = $this->cObj->stdWrap($this->pi_getLL('label_link_new'), $this->conf['newLinkWrap.']);
        }
        if (!$addressFound) {
            $baseMarkerArray['###NO_ADDRESS###'] = $this->cObj->stdWrap($this->pi_getLL('label_no_address'), $this->conf['noAddressWrap.']);
        } else {
            $baseMarkerArray['###NO_ADDRESS###'] = '';
        }
        // Fill sysMessage marker if set
        if (!empty($this->sysMessage)) {
            $baseMarkerArray['###SYS_MESSAGE###'] = $this->cObj->stdWrap($this->sysMessage, $this->conf['sysMessageWrap.']);
        } else {
            $baseMarkerArray['###SYS_MESSAGE###'] = '';
        }
        foreach ($hookObjectsArr as $hookObj) {
            if (method_exists($hookObj, 'processListingMarker')) {
                $hookObj->processListingMarker($baseMarkerArray, $linkMarkerArray, $addressItems, $addressType, $piArray, $this);
            }
        }
        // Replace markers and return content
        return $this->substituteMarkerArrayNoCached($tplBase, $baseMarkerArray, array(), $linkMarkerArray);
    }