1<?php
2
3class ohrmListComponent extends sfComponent {
4
5    protected static $configurationFactory;
6    protected static $listData;
7    protected static $itemsPerPage;
8    protected static $headerPartial;
9    protected static $footerPartial;
10    protected static $numberOfRecords;
11    protected static $definitionsPath;
12    protected static $activePlugin;
13    protected static $listForm ;
14
15    public static $pageNumber = 0;
16
17    /**
18     *
19     * @param sfRequest $request
20     */
21    public function execute($request) {
22        $this->setTemplateVariables();
23
24        $this->params = array();
25
26        foreach( $this->getVarHolder()->getAll() as $param => $val) {
27                $this->params[$param] = $val;
28        }
29
30        $recordsLimit = self::$itemsPerPage;//sfConfig::get('app_items_per_page');
31        $pageNo = $request->getParameter('pageNo', 1);
32
33        if (self::$pageNumber) {
34            $pageNo = self::$pageNumber;
35        } else {
36            $pageNo = $request->getParameter('pageNo', 1);
37        }
38
39        $numberOfRecords = self::$numberOfRecords;//replace with the count of all the records(self::$listData instanceof Doctrine_Collection) ? self::$listData->count() : count(self::$listData); // TODO: Remove the dependancy of ORM here; Use a Countable interface and a Iterator interface
40
41        $pager = new SimplePager($this->className, $recordsLimit);
42        $pager->setPage($pageNo);
43        $pager->setNumResults($numberOfRecords);
44        $pager->init();
45
46        $offset = $pager->getOffset();
47        $offset = empty($offset) ? 0 : $offset;
48
49        $this->offset = $offset;
50        $this->pager = $pager;
51        $this->recordLimit = $recordsLimit;
52
53        $this->currentSortField = $request->getParameter('sortField', '');
54        $this->currentSortOrder = $request->getParameter('sortOrder', '');
55
56        $this->showGroupHeaders = self::$configurationFactory->showGroupHeaders();
57        $this->headerGroups = self::$configurationFactory->getHeaderGroups();
58        $this->columns = self::$configurationFactory->getHeaders();
59        $this->data = self::$listData;
60        $this->className = self::$configurationFactory->getClassName();
61        $this->partial = self::$headerPartial;
62        $this->footerPartial = self::$footerPartial;
63        $this->listForm = ( empty( self::$listForm) )? new DefaultListForm() :  self::$listForm ;
64
65
66        $this->applyRuntimeDefinitions();
67
68        $this->makePluginCalls();
69    }
70
71    /**
72     *
73     * @param ListConfigurationFactory $configurationFactory
74     */
75    public static function setConfigurationFactory(ListConfigurationFactory $configurationFactory) {
76        self::$configurationFactory = $configurationFactory;
77    }
78
79    /**
80     *
81     * @return mixed
82     */
83    public static function getListData() {
84        return self::$listData;
85    }
86
87    /**
88     *
89     * @param mixed $data
90     */
91    public static function setListData($data) {
92        self::$listData = $data;
93    }
94
95     /**
96     *
97     * @return mixed
98     */
99    public static function getListForm() {
100
101
102        return self::$listForm;
103    }
104
105    /**
106     *
107     * @param mixed $data
108     */
109    public static function setListForm($form) {
110        self::$listForm = $form;
111    }
112
113    /**
114     *
115     * @param string $partial
116     */
117    public static function setHeaderPartial($partial) {
118        self::$headerPartial = $partial;
119    }
120
121    /**
122     *
123     * @param string $partial
124     */
125    public static function setFooterPartial($partial) {
126        self::$footerPartial = $partial;
127    }
128    /**
129     *
130     * @param int $items
131     */
132    public static function setItemsPerPage($items) {
133        self::$itemsPerPage = $items;
134    }
135
136    /**
137     *
138     * @param int $count
139     */
140    public static function setNumberOfRecords($count) {
141        self::$numberOfRecords = $count;
142    }
143
144    /**
145     *
146     * @param int $pageNumber
147     */
148    public static function setPageNumber ($pageNumber) {
149        self::$pageNumber = $pageNumber;
150    }
151
152    /**
153     *
154     * @return string
155     */
156    public static function getDefinitionsPath() {
157        return self::$definitionsPath;
158    }
159
160    /**
161     *
162     * @param string $path
163     */
164    public static function setDefinitionsPath($path) {
165        self::$definitionsPath = $path;
166    }
167
168    /**
169     *
170     * @return string
171     */
172    public static function getActivePlugin() {
173        return self::$activePlugin;
174    }
175
176    /**
177     *
178     * @param string $pluginName
179     */
180    public static function setActivePlugin($pluginName) {
181        self::$activePlugin = $pluginName;
182    }
183
184    /**
185     *
186     * @return array
187     */
188    protected function getDefinitions() {
189        $className = self::$configurationFactory->getClassName();
190
191        $definitions = $this->loadDefinitions();
192        $definitionParams = array_key_exists($className, $definitions) ? $definitions[$className] : $definitions['Default'];
193        return $definitionParams;
194    }
195
196    /**
197     *
198     * @return array
199     */
200    protected function loadDefinitions() {
201
202        if (empty(self::$definitionsPath)) {
203            if (empty(self::$activePlugin)) {
204                self::$definitionsPath = sfConfig::get('sf_plugins_dir') . '/orangehrmCorePlugin/config/list_component.yml';
205            } else {
206                self::$definitionsPath = sfConfig::get('sf_plugins_dir') . '/' . self::$activePlugin . '/config/list_component.yml';;
207            }
208        }
209
210        return sfYaml::load(self::$definitionsPath);
211    }
212
213    /**
214     *
215     * @return array
216     */
217    protected function getDefinitionsFromPlugins() {
218        return PluginConfigurationManager::instance()->getExternalConfigurations('ohrmListComponent');
219    }
220
221    /**
222     * @return void
223     */
224    protected function setTemplateVariables() {
225        $definitions = $this->getDefinitions();
226        $definitionsFromPlugins = $this->getDefinitionsFromPlugins();
227
228        foreach ($definitions as $key => $value) {
229            $this->setVar($key, $value);
230        }
231
232        foreach ($definitionsFromPlugins as $key => $value) {
233            if ($key == 'calls') {
234                continue;
235            }
236            $this->setVar('extra' . ucfirst($key), $value);
237        }
238    }
239
240    /**
241     * @return void
242     */
243    protected function makePluginCalls() {
244        $definitionsFromPlugins = $this->getDefinitionsFromPlugins();
245        $calls = isset($definitionsFromPlugins['calls']) ? $definitionsFromPlugins['calls'] : '';
246
247        if (!empty($calls)) {
248            foreach ($calls as $subjectClass => $methodCalls) {
249                foreach ($methodCalls as $staticMethod => $param) {
250                    forward_static_call(array($subjectClass, $staticMethod), $param);
251                }
252            }
253        }
254    }
255
256    /**
257     * @return void
258     */
259    protected function applyRuntimeDefinitions() {
260        $runtimeDefinitions = self::$configurationFactory->getRuntimeDefinitions();
261        foreach ($runtimeDefinitions as $key => $value) {
262            $this->setVar($key, $value);
263        }
264    }
265
266}
267