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_Paypal_Block_Adminhtml_System_Config_Fieldset_Location
35: extends Mage_Adminhtml_Block_System_Config_Form_Fieldset
36: {
37: 38: 39: 40: 41: 42: 43:
44: protected function ($element, $tooltipsExist = false)
45: {
46: $js = '
47: document.observe("dom:loaded", function() {
48: $$(".with-button button.button").each(function(configureButton) {
49: togglePaypalSolutionConfigureButton(configureButton, true);
50: });
51: var paypalConflictsObject = {
52: "isConflict": false,
53: "ecMissed": false,
54: sharePayflowEnabling: function(enabler, isEvent) {
55: var ecPayflowEnabler = $$(".paypal-ec-payflow-enabler")[0];
56: if (typeof ecPayflowEnabler == "undefined") {
57: return;
58: }
59: var ecPayflowScopeElement = adminSystemConfig.getScopeElement(ecPayflowEnabler);
60:
61: if (!enabler.enablerObject.ecPayflow) {
62: if ((!ecPayflowScopeElement || !ecPayflowScopeElement.checked) && isEvent
63: && enabler.value == 1
64: ) {
65: ecPayflowEnabler.value = 0;
66: fireEvent(ecPayflowEnabler, "change");
67: }
68: return;
69: }
70:
71: var enablerScopeElement = adminSystemConfig.getScopeElement(enabler);
72: if (enablerScopeElement && ecPayflowScopeElement
73: && enablerScopeElement.checked != ecPayflowScopeElement.checked
74: && (isEvent || ecPayflowScopeElement.checked)
75: ) {
76: $(ecPayflowScopeElement).click();
77: }
78:
79: var ecEnabler = $$(".paypal-ec-enabler")[0];
80: if (ecPayflowEnabler.value != enabler.value && (isEvent || enabler.value == 1)) {
81: ecPayflowEnabler.value = enabler.value;
82: fireEvent(ecPayflowEnabler, "change");
83: if (ecPayflowEnabler.value == 1) {
84: if (typeof ecEnabler != "undefined") {
85: var ecEnablerScopeElement = adminSystemConfig.getScopeElement(ecEnabler);
86: ecEnabler.value = 1;
87: if (ecEnablerScopeElement && ecEnablerScopeElement.checked) {
88: paypalConflictsObject.checklessEventAction(ecEnablerScopeElement, false);
89: }
90: paypalConflictsObject.checklessEventAction(ecEnabler, true);
91: }
92: }
93: }
94: if (!isEvent && ecPayflowEnabler.value == 1 && typeof ecEnabler != "undefined") {
95: var ecSolution = $$(".pp-method-express")[0];
96: if (typeof ecSolution != "undefined" && !$(ecSolution).hasClassName("enabled")) {
97: ecSolution.addClassName("enabled");
98: }
99: }
100: },
101: onChangeEnabler: function(event) {
102: paypalConflictsObject.checkPaymentConflicts($(Event.element(event)), "change");
103: },
104: onClickEnablerScope: function(event) {
105: paypalConflictsObject.checkPaymentConflicts(
106: $(adminSystemConfig.getUpTr($(Event.element(event))).select(".paypal-enabler")[0]),
107: "click"
108: );
109: },
110: getSharedElements: function(element) {
111: var sharedElements = [];
112: adminSystemConfig.mapClasses(element, true, function(elementClassName) {
113: $$("." + elementClassName).each(function(sharedElement) {
114: if (sharedElements.indexOf(sharedElement) == -1) {
115: sharedElements.push(sharedElement);
116: }
117: });
118: });
119: if (sharedElements.length == 0) {
120: sharedElements.push(element);
121: }
122: return sharedElements;
123: },
124: checklessEventAction: function(element, isChange) {
125: var action = isChange ? "change" : "click";
126: var handler = isChange
127: ? paypalConflictsObject.onChangeEnabler
128: : paypalConflictsObject.onClickEnablerScope;
129: paypalConflictsObject.getSharedElements(element).each(function(sharedElement) {
130: Event.stopObserving(sharedElement, action, handler);
131: if (isChange) {
132: sharedElement.value = element.value;
133: if ($(sharedElement).requiresObj) {
134: $(sharedElement).requiresObj.indicateEnabled();
135: }
136: }
137: });
138: if (isChange) {
139: fireEvent(element, "change");
140: } else {
141: $(element).click();
142: }
143: paypalConflictsObject.getSharedElements(element).each(function(sharedElement) {
144: Event.observe(sharedElement, action, handler);
145: });
146: },
147: ecCheckAvailability: function() {
148: var ecButton = $$(".pp-method-express button.button")[0];
149: if (typeof ecButton == "undefined") {
150: return;
151: }
152: var couldBeConfigured = true;
153: $$(".paypal-enabler").each(function(enabler) {
154: if (enabler.enablerObject.ecEnabler || enabler.enablerObject.ecConflicts
155: || enabler.enablerObject.ecSeparate
156: ) {
157: return;
158: }
159: if (enabler.value == 1) {
160: couldBeConfigured = false;
161: }
162: });
163: if (couldBeConfigured) {
164: togglePaypalSolutionConfigureButton(ecButton, true);
165: } else {
166: togglePaypalSolutionConfigureButton(ecButton, false);
167: }
168: },
169: // type could be "initial", "change", "click"
170: checkPaymentConflicts: function(enabler, type) {
171: var isEvent = (type != "initial");
172: var ecEnabler = $$(".paypal-ec-enabler")[0];
173:
174: if (enabler.value == 0) {
175: if (!enabler.enablerObject.ecIndependent && type == "change") {
176: if (typeof ecEnabler != "undefined" && ecEnabler.value == 1) {
177: var ecEnablerScopeElement = adminSystemConfig.getScopeElement(ecEnabler);
178: if (!ecEnablerScopeElement || !ecEnablerScopeElement.checked) {
179: ecEnabler.value = 0;
180: paypalConflictsObject.checklessEventAction(ecEnabler, true);
181: }
182: }
183: }
184: paypalConflictsObject.ecCheckAvailability();
185: paypalConflictsObject.sharePayflowEnabling(enabler, isEvent);
186: return;
187: }
188:
189: var confirmationApproved = isEvent;
190: var confirmationShowed = false;
191: // check other solutions
192: $$(".paypal-enabler").each(function(anotherEnabler) {
193: var anotherEnablerScopeElement = adminSystemConfig.getScopeElement(anotherEnabler);
194: if (!confirmationApproved && isEvent || $(anotherEnabler) == enabler
195: || anotherEnabler.value == 0
196: && (!anotherEnablerScopeElement || !anotherEnablerScopeElement.checked)
197: ) {
198: return;
199: }
200: var conflict = enabler.enablerObject.ecConflicts && anotherEnabler.enablerObject.ecEnabler
201: || enabler.enablerObject.ecEnabler && anotherEnabler.enablerObject.ecConflicts
202: || !enabler.enablerObject.ecIndependent && anotherEnabler.enablerObject.ecConflicts
203: || !enabler.enablerObject.ecEnabler && !anotherEnabler.enablerObject.ecEnabler;
204:
205: if (conflict && !confirmationShowed && anotherEnabler.value == 1) {
206: if (isEvent) {
207: confirmationApproved = confirm(\'' . $this->helper('core')->jsQuoteEscape($this->__('There is already another PayPal solution enabled. Enable this solution instead?')) . '\');
208: } else {
209: paypalConflictsObject.isConflict = true;
210: }
211: confirmationShowed = true;
212: }
213: if (conflict && confirmationApproved) {
214: anotherEnabler.value = 0;
215: if (anotherEnablerScopeElement && anotherEnablerScopeElement.checked && isEvent) {
216: paypalConflictsObject.checklessEventAction(anotherEnablerScopeElement, false);
217: }
218: paypalConflictsObject.checklessEventAction(anotherEnabler, true);
219: }
220: });
221:
222: if (!enabler.enablerObject.ecIndependent) {
223: if (!isEvent && (typeof ecEnabler == "undefined" || ecEnabler.value == 0)) {
224: if (!enabler.enablerObject.ecPayflow) {
225: paypalConflictsObject.ecMissed = true;
226: }
227: } else if (isEvent && typeof ecEnabler != "undefined" && confirmationApproved) {
228: var ecEnablerScopeElement = adminSystemConfig.getScopeElement(ecEnabler);
229: if (ecEnablerScopeElement && ecEnablerScopeElement.checked) {
230: paypalConflictsObject.checklessEventAction(ecEnablerScopeElement, false);
231: }
232: if (ecEnabler.value == 0) {
233: ecEnabler.value = 1;
234: paypalConflictsObject.checklessEventAction(ecEnabler, true);
235: }
236: }
237: }
238:
239: if (!confirmationApproved && isEvent) {
240: enabler.value = 0;
241: paypalConflictsObject.checklessEventAction(enabler, true);
242: }
243: paypalConflictsObject.ecCheckAvailability();
244: paypalConflictsObject.sharePayflowEnabling(enabler, isEvent);
245: }
246: };
247:
248: // fill enablers with conflict data
249: $$(".paypal-enabler").each(function(enablerElement) {
250: var enablerObj = {
251: ecIndependent: false,
252: ecConflicts: false,
253: ecEnabler: false,
254: ecSeparate: false,
255: ecPayflow: false
256: };
257: $(enablerElement).classNames().each(function(className) {
258: switch (className) {
259: case "paypal-ec-conflicts":
260: enablerObj.ecConflicts = true;
261: case "paypal-ec-independent":
262: enablerObj.ecIndependent = true;
263: break;
264: case "paypal-ec-enabler":
265: enablerObj.ecEnabler = true;
266: enablerObj.ecIndependent = true;
267: break;
268: case "paypal-ec-separate":
269: enablerObj.ecSeparate = true;
270: enablerObj.ecIndependent = true;
271: break;
272: case "paypal-ec-pe":
273: enablerObj.ecPayflow = true;
274: break;
275: }
276: });
277: enablerElement.enablerObject = enablerObj;
278:
279: Event.observe(enablerElement, "change", paypalConflictsObject.onChangeEnabler);
280: var enablerScopeElement = adminSystemConfig.getScopeElement(enablerElement);
281: if (enablerScopeElement) {
282: Event.observe(enablerScopeElement, "click", paypalConflictsObject.onClickEnablerScope);
283: }
284: });
285:
286: // initially uncheck payflow
287: var ecPayflowEnabler = $$(".paypal-ec-payflow-enabler")[0];
288: if (typeof ecPayflowEnabler != "undefined") {
289: if (ecPayflowEnabler.value == 1) {
290: ecPayflowEnabler.value = 0;
291: fireEvent(ecPayflowEnabler, "change");
292: }
293:
294: var ecPayflowScopeElement = adminSystemConfig.getScopeElement(ecPayflowEnabler);
295: if (ecPayflowScopeElement && !ecPayflowScopeElement.checked) {
296: $(ecPayflowScopeElement).click();
297: }
298: }
299:
300: $$(".paypal-enabler").each(function(enablerElement) {
301: paypalConflictsObject.checkPaymentConflicts(enablerElement, "initial");
302: });
303: if (paypalConflictsObject.isConflict || paypalConflictsObject.ecMissed) {
304: var notification = \'' . $this->helper('core')->jsQuoteEscape($this->__('The following error(s) occured:')) . '\';
305: if (paypalConflictsObject.isConflict) {
306: notification += "\\n " + \'' . $this->helper('core')->jsQuoteEscape($this->__('Some PayPal solutions conflict.')) . '\';
307: }
308: if (paypalConflictsObject.ecMissed) {
309: notification += "\\n " + \'' . $this->helper('core')->jsQuoteEscape($this->__('PayPal Express Checkout is not enabled.')) . '\';
310: }
311: notification += "\\n" + \'' . $this->helper('core')->jsQuoteEscape($this->__('Please re-enable the previously enabled payment solutions.')) . '\';
312: setTimeout(function() {
313: alert(notification);
314: }, 1);
315: }
316:
317: $$(".requires").each(function(dependent) {
318: var $dependent = $(dependent);
319: if ($dependent.hasClassName("paypal-ec-enabler")) {
320: $dependent.requiresObj.callback = function(required) {
321: if ($(required).hasClassName("paypal-enabler") && required.value == 0) {
322: $dependent.disable();
323: }
324: }
325: $dependent.requiresObj.requires.each(function(required) {
326: $dependent.requiresObj.callback(required);
327: });
328: }
329: });
330:
331: var originalFormValidation = configForm.validator.options.onFormValidate;
332: configForm.validator.options.onFormValidate = function(result, form) {
333: originalFormValidation(result, form);
334: if (result) {
335: var ecPayflowEnabler = $$(".paypal-ec-payflow-enabler")[0];
336: if (typeof ecPayflowEnabler == "undefined") {
337: return;
338: }
339: var ecPayflowScopeElement = adminSystemConfig.getScopeElement(ecPayflowEnabler);
340: if ((typeof ecPayflowScopeElement == "undefined" || !ecPayflowScopeElement.checked)
341: && ecPayflowEnabler.value == 1
342: ) {
343: $$(".paypal-ec-enabler").each(function(ecEnabler) {
344: ecEnabler.value = 0;
345: });
346: }
347: }
348: }
349: });
350: ';
351: return parent::_getExtraJs($element, $tooltipsExist) . $this->helper('adminhtml/js')->getScript($js);
352: }
353: }
354: