PowerKiKi /
mqueue
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | use mQueue\Form\Filters; |
||||||
| 4 | use mQueue\Form\Import; |
||||||
| 5 | use mQueue\Model\Movie; |
||||||
| 6 | use mQueue\Model\MovieMapper; |
||||||
| 7 | use mQueue\Model\Status; |
||||||
| 8 | use mQueue\Model\StatusMapper; |
||||||
| 9 | use mQueue\Model\User; |
||||||
| 10 | use mQueue\Model\UserMapper; |
||||||
| 11 | |||||||
| 12 | class MovieController extends Zend_Controller_Action |
||||||
| 13 | { |
||||||
| 14 | 3 | public function init(): void |
|||||
| 15 | { |
||||||
| 16 | // Init the Context Switch Action helper |
||||||
| 17 | 3 | $contextSwitch = $this->_helper->contextSwitch(); |
|||||
| 18 | |||||||
| 19 | // Add the new context |
||||||
| 20 | 3 | $contextSwitch->setContexts([ |
|||||
| 21 | 3 | 'csv' => ['suffix' => 'csv'], |
|||||
| 22 | 'rss' => ['suffix' => 'rss'], |
||||||
| 23 | ]); |
||||||
| 24 | |||||||
| 25 | 3 | $contextSwitch->addActionContext('index', 'csv')->addActionContext('index', 'rss')->initContext(); |
|||||
| 26 | 3 | } |
|||||
| 27 | |||||||
| 28 | 1 | public function indexAction(): void |
|||||
| 29 | { |
||||||
| 30 | // Check there is at least one user, otherwise the whole page will crash |
||||||
| 31 | 1 | if (!User::getCurrent() && !UserMapper::getDbTable()->fetchRow()) { |
|||||
| 32 | throw new Exception('At least one user must exist to access this page'); |
||||||
| 33 | } |
||||||
| 34 | |||||||
| 35 | 1 | $form = new Filters(); |
|||||
| 36 | 1 | $this->view->formFilter = $form; |
|||||
| 37 | |||||||
| 38 | // Detect if at least one filter was submitted |
||||||
| 39 | 1 | $submitted = false; |
|||||
| 40 | 1 | foreach ($this->getRequest()->getParams() as $key => $filter) { |
|||||
| 41 | 1 | if (preg_match('/^filter\d+$/', $key)) { |
|||||
| 42 | $submitted = true; |
||||||
| 43 | } |
||||||
| 44 | } |
||||||
| 45 | |||||||
| 46 | // If was submitted, try to validate values |
||||||
| 47 | 1 | if ($submitted) { |
|||||
| 48 | if (!$form->isValid($this->getRequest()->getParams())) { |
||||||
| 49 | $this->_helper->FlashMessenger(['warning' => _tr('Filter is invalid.')]); |
||||||
| 50 | $form->setDefaults([]); |
||||||
| 51 | } |
||||||
| 52 | } // If we submitted a quicksearch, set default values to search with any status |
||||||
| 53 | 1 | elseif ($this->_getParam('search')) { |
|||||
| 54 | $form->setDefaults([ |
||||||
| 55 | 'filter1' => [ |
||||||
| 56 | 'user' => User::getCurrent() ? 0 : UserMapper::fetchAll()->current()->id, |
||||||
| 57 | 'condition' => 'is', |
||||||
| 58 | 'status' => array_merge([0], array_keys(mQueue\Model\Status::$ratings)), |
||||||
| 59 | 'title' => $this->_getParam('search'), |
||||||
| 60 | ], |
||||||
| 61 | ]); |
||||||
| 62 | } // Otherwise clear the filter |
||||||
| 63 | else { |
||||||
| 64 | 1 | $form->setDefaults([]); |
|||||
| 65 | } |
||||||
| 66 | |||||||
| 67 | // Gather users selected in filters |
||||||
| 68 | 1 | $this->view->users = []; |
|||||
| 69 | 1 | $filters = $form->getValues(); |
|||||
| 70 | 1 | foreach ($filters as $key => $filter) { |
|||||
| 71 | 1 | if (!preg_match('/^filter\d+$/', $key)) { |
|||||
| 72 | 1 | continue; |
|||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | 1 | $this->view->users[$filter['user']] = UserMapper::find($filter['user']); |
|||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 76 | } |
||||||
| 77 | |||||||
| 78 | // If we ouput rss, we force sorting by date |
||||||
| 79 | 1 | if ($this->_helper->contextSwitch()->getCurrentContext() == 'rss') { |
|||||
| 80 | $this->getRequest()->setParam('sort', $filters['filter1']['withSource'] ? 'dateSearch' : 'date'); |
||||||
| 81 | $this->getRequest()->setParam('sortOrder', 'desc'); |
||||||
| 82 | } |
||||||
| 83 | 1 | $this->view->permanentParams = $form->getValues(); |
|||||
| 84 | 1 | $this->view->filterName = $form->getValuesText(); |
|||||
| 85 | 1 | unset($this->view->permanentParams['addFilter']); |
|||||
|
0 ignored issues
–
show
|
|||||||
| 86 | |||||||
| 87 | 1 | $allowedSortingKey = ['title', 'date', 'dateSearch']; |
|||||
| 88 | 1 | $usersCount = count($this->view->users); |
|||||
| 89 | 1 | for ($i = 0; $i < $usersCount; ++$i) { |
|||||
| 90 | 1 | $allowedSortingKey[] = 'status' . $i; |
|||||
| 91 | } |
||||||
| 92 | 1 | $sort = $this->_helper->createSorting('sort', $allowedSortingKey); |
|||||
| 93 | |||||||
| 94 | // Set up the paginator: Apply pagination only if there is no special context (so it is normal html rendering) |
||||||
| 95 | 1 | $this->view->paginator = $this->_helper->createPaginator(MovieMapper::getFilteredQuery($filters, $sort)); |
|||||
| 96 | 1 | } |
|||||
| 97 | |||||||
| 98 | public function viewAction(): void |
||||||
| 99 | { |
||||||
| 100 | if ($this->getRequest()->getParam('id')) { |
||||||
| 101 | $this->view->movie = MovieMapper::find($this->getRequest()->getParam('id')); |
||||||
| 102 | } |
||||||
| 103 | |||||||
| 104 | if (!$this->view->movie) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 105 | throw new Exception($this->view->translate('Movie not found')); |
||||||
| 106 | } |
||||||
| 107 | |||||||
| 108 | $this->view->users = UserMapper::fetchAll(); |
||||||
| 109 | $this->view->movieActivity = $this->_helper->createPaginator(StatusMapper::getActivityQuery($this->view->movie)); |
||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | 1 | public function addAction(): void |
|||||
| 113 | { |
||||||
| 114 | 1 | $request = $this->getRequest(); |
|||||
| 115 | 1 | $form = new \mQueue\Form\Movie(); |
|||||
| 116 | |||||||
| 117 | 1 | if ($this->_getParam('id')) { |
|||||
| 118 | if ($form->isValid($request->getParams())) { |
||||||
| 119 | $values = $form->getValues(); |
||||||
| 120 | $movie = MovieMapper::find(Movie::extractId($values['id'])); |
||||||
|
0 ignored issues
–
show
It seems like
mQueue\Model\Movie::extractId($values['id']) can also be of type string; however, parameter $id of mQueue\Model\MovieMapper::find() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 121 | if (!$movie) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 122 | $movie = MovieMapper::getDbTable()->createRow(); |
||||||
| 123 | $movie->setId($values['id']); |
||||||
| 124 | $movie->save(); |
||||||
| 125 | $this->_helper->FlashMessenger(_tr('A movie was added.')); |
||||||
| 126 | } |
||||||
| 127 | |||||||
| 128 | $this->view->movies = [$movie]; |
||||||
| 129 | } |
||||||
| 130 | } |
||||||
| 131 | |||||||
| 132 | 1 | $this->view->form = $form; |
|||||
| 133 | 1 | } |
|||||
| 134 | |||||||
| 135 | 1 | public function importAction(): void |
|||||
| 136 | { |
||||||
| 137 | 1 | $request = $this->getRequest(); |
|||||
| 138 | 1 | $form = new Import(); |
|||||
| 139 | 1 | $form->setDefaults(['favoriteMinimum' => 9, 'excellentMinimum' => 7, 'okMinimum' => 5]); |
|||||
| 140 | 1 | $this->view->form = $form; |
|||||
| 141 | |||||||
| 142 | 1 | if ($this->getRequest()->isPost() && $form->isValid($request->getPost())) { |
|||||
|
0 ignored issues
–
show
The method
isPost() does not exist on Zend_Controller_Request_Abstract. It seems like you code against a sub-type of Zend_Controller_Request_Abstract such as Zend_Controller_Request_Http.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
getPost() does not exist on Zend_Controller_Request_Abstract. It seems like you code against a sub-type of Zend_Controller_Request_Abstract such as Zend_Controller_Request_Http.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 143 | if (User::getCurrent() == null) { |
||||||
| 144 | $this->_helper->FlashMessenger(['error' => _tr('You must be logged in.')]); |
||||||
| 145 | |||||||
| 146 | return; |
||||||
| 147 | } |
||||||
| 148 | |||||||
| 149 | $values = $form->getValues(); |
||||||
| 150 | $page = file_get_contents($values['url']); |
||||||
| 151 | |||||||
| 152 | $pattern = '|<a href="/title/tt(\d{7,})/">.*</td>\s*<td.*>(\d+(\.\d)*)</td>|U'; |
||||||
| 153 | preg_match_all($pattern, $page, $matches); |
||||||
| 154 | |||||||
| 155 | $movies = []; |
||||||
| 156 | $matchesCount = count($matches[1]); |
||||||
| 157 | for ($i = 0; $i < $matchesCount; ++$i) { |
||||||
| 158 | $id = $matches[1][$i]; |
||||||
| 159 | $imdbRating = $matches[2][$i]; |
||||||
| 160 | |||||||
| 161 | $movie = MovieMapper::find($id); |
||||||
| 162 | if (!$movie) { |
||||||
| 163 | $movie = MovieMapper::getDbTable()->createRow(); |
||||||
| 164 | $movie->setId($id); |
||||||
| 165 | $movie->save(); |
||||||
| 166 | } |
||||||
| 167 | |||||||
| 168 | if ($imdbRating >= $values['favoriteMinimum']) { |
||||||
| 169 | $rating = Status::Favorite; |
||||||
| 170 | } elseif ($imdbRating >= $values['excellentMinimum']) { |
||||||
| 171 | $rating = Status::Excellent; |
||||||
| 172 | } elseif ($imdbRating >= $values['okMinimum']) { |
||||||
| 173 | $rating = Status::Ok; |
||||||
| 174 | } else { |
||||||
| 175 | $rating = Status::Bad; |
||||||
| 176 | } |
||||||
| 177 | |||||||
| 178 | $movie->setStatus(User::getCurrent(), $rating); |
||||||
| 179 | $movies[] = $movie; |
||||||
| 180 | } |
||||||
| 181 | |||||||
| 182 | $count = count($movies); |
||||||
| 183 | if ($count) { |
||||||
| 184 | $this->_helper->FlashMessenger(_tr('Movies imported.')); |
||||||
| 185 | $this->view->movies = $movies; |
||||||
| 186 | } else { |
||||||
| 187 | $this->_helper->FlashMessenger(['warning' => _tr('No movies found for import.')]); |
||||||
| 188 | } |
||||||
| 189 | } |
||||||
| 190 | 1 | } |
|||||
| 191 | } |
||||||
| 192 |