1<?php
2
3/**
4 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
5 * all the essential functionalities required for any enterprise.
6 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
7 *
8 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
9 * the GNU General Public License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with this program;
17 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA  02110-1301, USA
19 */
20
21/**
22 * Description of getSharedEmployeeListAction
23 *
24 * @author aruna
25 */
26class getSharedEmployeeListAction extends BaseBuzzAction {
27
28    /**
29     * @param sfForm $form
30     * @return
31     */
32    protected function setForm(sfForm $form) {
33        if (is_null($this->form)) {
34            $this->form = $form;
35        }
36    }
37
38    public function execute($request) {
39        try {
40
41            $this->setForm(new LikedOrSharedEmployeeForm());
42
43            if ($request->isMethod('post')) {
44                $this->form->bind($request->getParameter($this->form->getName()));
45                if ($this->form->isValid()) {
46                    $formValues = $this->form->getValues();
47                    $this->id = $formValues['id'];
48                    $this->event = $formValues['event'];
49                    $this->loggedInUser = $this->getLogedInEmployeeNumber();
50                    $this->buzzService = $this->getBuzzService();
51                    $this->loggedInEmployeeId = $this->getLogedInEmployeeNumber();
52                    $this->post = $this->buzzService->getShareById($this->id)->getPostShared();
53                    $this->sharedEmployeeDetailsList = $this->getPostSharedEmployeeNameList($this->post);
54
55                    if ($this->event === "click") {
56
57                    } else {
58                        foreach ($this->sharedEmployeeDetailsList as $employee) {
59                            if ($employee['empNumber'] !== $this->loggedInEmployeeId) {
60                                echo $employee['empName'];
61                                echo "\n";
62                            }
63                        }
64                        sfView::NONE;
65                        die;
66                    }
67                }
68            }
69        } catch (Exception $ex) {
70            $this->redirect('auth/login');
71        }
72    }
73
74    /**
75     * get post shared employee list
76     * @param type $post
77     * @return array EployeeList
78     */
79    private function getPostSharedEmployeeList($post) {
80        $sharedEmployeeList = array();
81        $isAdminShare = false;
82        foreach ($post->getShare() as $share) {
83            if ($share->getEmployeeNumber() == null) {
84                $isAdminShare = true;
85            } else {
86                $employee = $share->getEmployeePostShared();
87                array_push($sharedEmployeeList, $employee);
88            }
89        }
90        $sharedUniqueEmployeeList = $this->generateUniqueEmployeeList($sharedEmployeeList);
91        if ($isAdminShare) {
92            array_push($sharedUniqueEmployeeList, new Employee());
93        }
94        return $sharedUniqueEmployeeList;
95    }
96
97    /**
98     * Returns a unique list of employees from the $employeeList
99     * @param array $employeeList
100     * @return array
101     */
102    private function generateUniqueEmployeeList($employeeList) {
103        $uniqueEmployeeList = array();
104        foreach ($employeeList as $employee) {
105            $id = $employee->getEmpNumber();
106            isset($uniqueEmployeeList[$id]) or $uniqueEmployeeList[$id] = $employee;
107        }
108        return $uniqueEmployeeList;
109    }
110
111    /**
112     * get post shared employee name list;
113     * @param type $post
114     * @return array employee name list
115     */
116    private function getPostSharedEmployeeNameList($post) {
117        $sharedEmployeeDetailsList = array();
118        foreach ($post->getShare() as $share) {
119
120            if ($share->getEmployeeNumber() == null) {
121                $empName = 'Admin';
122            } else {
123                $employee = $share->getEmployeePostShared();
124                if ($employee instanceof Employee) {
125                    $empName = $employee->getFirstAndLastNames();
126                    $jobTitle = $employee->getJobTitleName();
127                } else {
128                    $empName = '(' . __(self::LABEL_EMPLOYEE_DELETED) . ')';
129                    $jobTitle = '';
130                    $employeeDeleted = true;
131                }
132            }
133
134            $employeeDetails = array(self::EMP_DELETED => $employeeDeleted, self::EMP_NUMBER => $share->getEmployeeNumber(), self::EMP_NAME => $empName, self::EMP_JOB_TITLE => $jobTitle);
135            $sharedEmployeeDetailsList[$share->getEmployeeNumber()] = $employeeDetails;
136        }
137        return $sharedEmployeeDetailsList;
138    }
139
140}
141