| Conditions | 8 |
| Paths | 7 |
| Total Lines | 103 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 109 | public function approve($data) { |
||
| 110 | // Approve items for inclusion |
||
| 111 | // Create new epobject and update the editqueue |
||
| 112 | |||
| 113 | global $THEUSER; |
||
| 114 | |||
| 115 | // We need a list of editqueue items to play with |
||
| 116 | $this->get_pending(); |
||
| 117 | if (!isset($this->pending)) { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | $timestamp = date('Y-m-d H:i:s', time()); |
||
| 121 | |||
| 122 | foreach ($data['approvals'] as $approval_id) { |
||
| 123 | // create a new epobject |
||
| 124 | // title VARCHAR(255), |
||
| 125 | // body TEXT, |
||
| 126 | // type INTEGER, |
||
| 127 | // created DATETIME, |
||
| 128 | // modified DATETIME, |
||
| 129 | /*print "<pre>"; |
||
| 130 | print_r($data); |
||
| 131 | print "</pre>";*/ |
||
| 132 | // Check to see that we actually have something to approve |
||
| 133 | if (!isset($this->pending[$approval_id])) { |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | $q = $this->db->query("INSERT INTO epobject |
||
| 137 | (title, body, type, created) |
||
| 138 | VALUES |
||
| 139 | ('" . addslashes($this->pending[$approval_id]['title']) . "', |
||
| 140 | '" . addslashes($this->pending[$approval_id]['body']) . "', |
||
| 141 | '" . $data['epobject_type'] . "', |
||
| 142 | '" . $timestamp . "');"); |
||
| 143 | |||
| 144 | // If that didn't work we can't go any further... |
||
| 145 | if (!$q->success()) { |
||
| 146 | print "epobject trouble"; |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | $this->current_epobject_id = $q->insert_id(); |
||
| 150 | |||
| 151 | // depending on the epobject type, we'll need to make |
||
| 152 | // entries in different tables. |
||
| 153 | switch ($data['epobject_type']) { |
||
| 154 | |||
| 155 | case EPTYPE_GLOSSARY: |
||
| 156 | $previous_insert_id = $q->insert_id(); |
||
| 157 | $q = $this->db->query( |
||
| 158 | 'INSERT INTO glossary |
||
| 159 | (epobject_id, type, visible) |
||
| 160 | VALUES |
||
| 161 | (:id, :type, :enabled);', |
||
| 162 | [ |
||
| 163 | ':id' => $q->insert_id(), |
||
| 164 | ':type' => EPTYPE_GLOSSARY, |
||
| 165 | ':enabled' => 1, |
||
| 166 | ] |
||
| 167 | ); |
||
| 168 | // Again, no point carrying on if this fails, |
||
| 169 | // so remove the previous entry |
||
| 170 | if (!$q->success()) { |
||
| 171 | print "glossary trouble!"; |
||
| 172 | $this->db->query('DELETE FROM epobject WHERE epobject_id=:id ;', [':id' => $previous_insert_id]); |
||
| 173 | return false; |
||
| 174 | } |
||
| 175 | break; |
||
| 176 | |||
| 177 | } |
||
| 178 | $this->current_subclass_id = $q->insert_id(); |
||
| 179 | |||
| 180 | // Then finally update the editqueue with |
||
| 181 | // the new epobject id and approval details. |
||
| 182 | $q = $this->db->query( |
||
| 183 | 'UPDATE editqueue |
||
| 184 | SET |
||
| 185 | epobject_id_l=:current, |
||
| 186 | editor_id=:editor, |
||
| 187 | approved=:approved, |
||
| 188 | decided=:timestamp |
||
| 189 | WHERE edit_id=:approval_id ;', |
||
| 190 | [ |
||
| 191 | ':current' => $this->current_epobject_id, |
||
| 192 | ':editor' => addslashes($THEUSER->user_id()), |
||
| 193 | ':approved' => '1', |
||
| 194 | ':timestamp' => $timestamp, |
||
| 195 | ':approval_id' => $approval_id, |
||
| 196 | ] |
||
| 197 | ); |
||
| 198 | if (!$q->success()) { |
||
| 199 | break; |
||
| 200 | } else { |
||
| 201 | // Now send them an email telling them they've been approved |
||
| 202 | |||
| 203 | |||
| 204 | // Scrub that one from the list of pending items |
||
| 205 | unset($this->pending[$approval_id]); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | $this->update_pending_count(); |
||
| 210 | |||
| 211 | return true; |
||
| 212 | } |
||
| 318 |