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: 35: 36: 37: 38: 39:
40: class Eabi_Livehandler_Model_System_Config_Backend_Button extends Mage_Adminhtml_Model_System_Config_Backend_Serialized_Array {
41: protected $_eventPrefix = 'eabi_livehandler_system_config_backend_button';
42:
43: 44: 45: 46: 47: 48: 49: 50:
51: protected function _afterLoad() {
52: parent::_afterLoad();
53:
54: $dirnames = array(
55: Mage::getBaseDir('code').'/community/Eabi/Livehandler/Model/Action',
56: Mage::getBaseDir('code').'/local/Eabi/Livehandler/Model/Action',
57: );
58: $foundNames = array();
59: foreach ($dirnames as $dirname) {
60: if (is_dir($dirname) && !$this->getEabi()->getConfigData('eabi_livehandler/admintools/disable_actions_read')) {
61: $directoryLister = new Eabi_Livehandler_Model_Directory_Collection($dirname, true);
62: $filenames = $directoryLister->filesPaths();
63: foreach ($filenames as $filename) {
64: $className = str_replace(array($dirname, '\\'), array('', '/'), $filename);
65: if (strpos($className, '.php') !== false) {
66: $className = ltrim(strtolower(str_replace(array('.php', '/'), array('', '_'), $className)), '_');
67: if ($className != 'abstract') {
68: $foundNames[$className] = $className;
69: }
70: }
71: }
72:
73: }
74: }
75: $values = $this->getValue();
76: $existingValues = array();
77: $valuesToRemove = array();
78: if (is_array($values)) {
79: uasort($values, array(__CLASS__, '_sortAction'));
80:
81: foreach ($values as $key => $value) {
82: $existingValues[$value['button_name']] = $value['button_name'];
83: $modelName = 'eabi_livehandler/action_'.$value['button_name'];
84: if (strpos($value['button_name'], '/')) {
85: $modelName = $value['button_name'];
86: }
87: $testModel = Mage::getModel($modelName);
88: if (!$testModel || !($testModel instanceof Eabi_Livehandler_Model_Action_Abstract)) {
89: $valuesToRemove[] = $key;
90: }
91:
92: }
93: }
94: if (count($foundNames) && !is_array($values)) {
95: $values = array();
96: }
97: $valueAdded = false;
98: foreach ($foundNames as $foundName) {
99: if (!isset($existingValues[$foundName])) {
100: $testModel = Mage::getModel('eabi_livehandler/action_'.$foundName);
101: if ($testModel && $testModel instanceof Eabi_Livehandler_Model_Action_Abstract) {
102: $values['_' . time() . '_' . mt_rand(0, 999)] = array(
103: 'button_name' => $foundName,
104: 'sort_order' => "0",
105: 'disabled' => "0"
106: );
107: $valueAdded = true;
108: }
109:
110: }
111: }
112: foreach ($valuesToRemove as $valueToRemove) {
113: unset($values[$valueToRemove]);
114: }
115: if ($valueAdded || count($valuesToRemove) || true) {
116: $this->setValue($values);
117: }
118:
119:
120: }
121:
122: public function isValueChanged() {
123: $oldValue = @unserialize($this->getOldValue());
124: if (is_array($oldValue)) {
125: return $oldValue != $this->getValue();
126: }
127: return parent::isValueChanged();
128: }
129:
130:
131: 132: 133: 134: 135: 136:
137: public static function _sortAction($a, $b) {
138: if ((bool)$a['disabled'] && !(bool)$b['disabled']) {
139: return 1;
140: }
141: if (!(bool)$a['disabled'] && (bool)$b['disabled']) {
142: return -1;
143: }
144: if ((int)$a['sort_order'] == (int)$b['sort_order']) {
145: return 0;
146: }
147: return (int)$a['sort_order'] < (int)$b['sort_order']?-1:1;
148: }
149:
150: 151: 152: 153:
154: protected function getEabi() {
155: return Mage::helper('eabi');
156: }
157:
158:
159:
160: }
161:
162: