1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_Block_Urlrewrite_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
35: {
36: 37: 38: 39:
40: public function __construct()
41: {
42: parent::__construct();
43: $this->setId('urlrewrite_form');
44: $this->setTitle(Mage::helper('adminhtml')->__('Block Information'));
45: }
46:
47: 48: 49: 50: 51:
52: protected function _prepareForm()
53: {
54: $model = Mage::registry('current_urlrewrite');
55: $product = Mage::registry('current_product');
56: $category = Mage::registry('current_category');
57:
58: $form = new Varien_Data_Form(
59: array(
60: 'id' => 'edit_form',
61: 'action' => $this->getData('action'),
62: 'method' => 'post'
63: )
64: );
65:
66:
67: $formValues = array(
68: 'store_id' => $model->getStoreId(),
69: 'id_path' => $model->getIdPath(),
70: 'request_path' => $model->getRequestPath(),
71: 'target_path' => $model->getTargetPath(),
72: 'options' => $model->getOptions(),
73: 'description' => $model->getDescription(),
74: );
75: if ($sessionData = Mage::getSingleton('adminhtml/session')->getData('urlrewrite_data', true)) {
76: foreach ($formValues as $key => $value) {
77: if (isset($sessionData[$key])) {
78: $formValues[$key] = $sessionData[$key];
79: }
80: }
81: }
82:
83: $fieldset = $form->addFieldset('base_fieldset', array(
84: 'legend' => Mage::helper('adminhtml')->__('URL Rewrite Information')
85: ));
86:
87: $fieldset->addField('is_system', 'select', array(
88: 'label' => Mage::helper('adminhtml')->__('Type'),
89: 'title' => Mage::helper('adminhtml')->__('Type'),
90: 'name' => 'is_system',
91: 'required' => true,
92: 'options' => array(
93: 1 => Mage::helper('adminhtml')->__('System'),
94: 0 => Mage::helper('adminhtml')->__('Custom')
95: ),
96: 'disabled' => true,
97: 'value' => $model->getIsSystem()
98: ));
99:
100: $isFilterAllowed = false;
101:
102: if (!Mage::app()->isSingleStoreMode()) {
103: $stores = Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm();
104: $entityStores = array();
105: $noStoreError = false;
106:
107:
108: if ($product && $product->getId()) {
109: $entityStores = $product->getStoreIds() ? $product->getStoreIds() : array();
110: if (!$entityStores) {
111: $stores = array();
112: $noStoreError = $this->__('Chosen product does not associated with any website, so url rewrite is not possible.');
113: }
114:
115: if ($category && $category->getId()) {
116: $categoryStores = $category->getStoreIds() ? $category->getStoreIds() : array();
117: $entityStores = array_intersect($entityStores, $categoryStores);
118:
119: }
120: $isFilterAllowed = true;
121: } elseif ($category && $category->getId()) {
122: $entityStores = $category->getStoreIds() ? $category->getStoreIds() : array();
123: if (!$entityStores) {
124: $stores = array();
125: $noStoreError = $this->__('Chosen category does not associated with any website, so url rewrite is not possible.');
126: }
127: $isFilterAllowed = true;
128: }
129:
130: 131: 132: 133:
134: if ($stores && $isFilterAllowed) {
135: foreach ($stores as $i => $store) {
136: if (isset($store['value']) && $store['value']) {
137: $found = false;
138: foreach ($store['value'] as $_k => $_v) {
139: if (isset($_v['value']) && in_array($_v['value'], $entityStores)) {
140: $found = true;
141: } else {
142: unset($stores[$i]['value'][$_k]);
143: }
144: }
145: if (!$found) {
146: unset($stores[$i]);
147: }
148: }
149: }
150: }
151:
152: $element = $fieldset->addField('store_id', 'select', array(
153: 'label' => Mage::helper('adminhtml')->__('Store'),
154: 'title' => Mage::helper('adminhtml')->__('Store'),
155: 'name' => 'store_id',
156: 'required' => true,
157: 'values' => $stores,
158: 'disabled' => true,
159: 'value' => $formValues['store_id'],
160: ));
161: $renderer = $this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element');
162: $element->setRenderer($renderer);
163: if ($noStoreError) {
164: $element->setAfterElementHtml($noStoreError);
165: }
166: if (!$model->getIsSystem()) {
167: $element->unsetData('disabled');
168: }
169: } else {
170: $fieldset->addField('store_id', 'hidden', array(
171: 'name' => 'store_id',
172: 'value' => Mage::app()->getStore(true)->getId()
173: ));
174: }
175:
176: $idPath = $fieldset->addField('id_path', 'text', array(
177: 'label' => Mage::helper('adminhtml')->__('ID Path'),
178: 'title' => Mage::helper('adminhtml')->__('ID Path'),
179: 'name' => 'id_path',
180: 'required' => true,
181: 'disabled' => true,
182: 'value' => $formValues['id_path']
183: ));
184:
185: $requestPath = $fieldset->addField('request_path', 'text', array(
186: 'label' => Mage::helper('adminhtml')->__('Request Path'),
187: 'title' => Mage::helper('adminhtml')->__('Request Path'),
188: 'name' => 'request_path',
189: 'required' => true,
190: 'value' => $formValues['request_path']
191: ));
192:
193: $targetPath = $fieldset->addField('target_path', 'text', array(
194: 'label' => Mage::helper('adminhtml')->__('Target Path'),
195: 'title' => Mage::helper('adminhtml')->__('Target Path'),
196: 'name' => 'target_path',
197: 'required' => true,
198: 'disabled' => true,
199: 'value' => $formValues['target_path'],
200: ));
201:
202:
203: if (!$model->getId()) {
204: $_product = null;
205: $_category = null;
206: if ($category->getId() || $product->getId()) {
207: $_category = $category;
208: }
209:
210: if ($product->getId()) {
211: $_product = $product;
212: }
213:
214: if ($_category || $_product) {
215: $catalogUrlModel = Mage::getSingleton('catalog/url');
216: $idPath->setValue($catalogUrlModel->generatePath('id', $_product, $_category));
217: if (!isset($sessionData['request_path'])) {
218: $requestPath->setValue($catalogUrlModel->generatePath('request', $_product, $_category, ''));
219: }
220: $targetPath->setValue($catalogUrlModel->generatePath('target', $_product, $_category));
221: } else {
222: $idPath->unsetData('disabled');
223: $targetPath->unsetData('disabled');
224: }
225: } else {
226: if (!$model->getProductId() && !$model->getCategoryId()) {
227: $idPath->unsetData('disabled');
228: $targetPath->unsetData('disabled');
229: }
230: }
231:
232: $fieldset->addField('options', 'select', array(
233: 'label' => Mage::helper('adminhtml')->__('Redirect'),
234: 'title' => Mage::helper('adminhtml')->__('Redirect'),
235: 'name' => 'options',
236: 'options' => array(
237: '' => Mage::helper('adminhtml')->__('No'),
238: 'R' => Mage::helper('adminhtml')->__('Temporary (302)'),
239: 'RP' => Mage::helper('adminhtml')->__('Permanent (301)'),
240: ),
241: 'value' => $formValues['options']
242: ));
243:
244: $fieldset->addField('description', 'textarea', array(
245: 'label' => Mage::helper('adminhtml')->__('Description'),
246: 'title' => Mage::helper('adminhtml')->__('Description'),
247: 'name' => 'description',
248: 'cols' => 20,
249: 'rows' => 5,
250: 'value' => $formValues['description'],
251: 'wrap' => 'soft'
252: ));
253:
254: $form->setUseContainer(true);
255: $form->setAction(Mage::helper('adminhtml')->getUrl('*/*/save', array(
256: 'id' => $model->getId(),
257: 'product' => $product->getId(),
258: 'category' => $category->getId(),
259: )));
260: $this->setForm($form);
261:
262: return parent::_prepareForm();
263: }
264: }
265: