| Conditions | 20 |
| Paths | 60 |
| Total Lines | 67 |
| 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 |
||
| 104 | public function updatefield($request = null) |
||
| 105 | { |
||
| 106 | Versioned::set_reading_mode('Stage.Stage'); |
||
| 107 | $updateMessage = ""; |
||
| 108 | $updateCount = 0; |
||
| 109 | $table = $request->param("ID"); |
||
| 110 | $field = $request->param("OtherID"); |
||
| 111 | $titleField = $request->getVar('titlefield'); |
||
| 112 | $ids = explode(",", $request->getVar("id")); |
||
| 113 | $newValue = $request->getVar("value"); |
||
| 114 | if ($memberID = Member::currentUserID()) { |
||
| 115 | if (class_exists($table) && count($ids) > 0 && ($newValue || $newValue == 0)) { |
||
| 116 | foreach ($ids as $id) { |
||
| 117 | if (intval($id)) { |
||
| 118 | if ($obj = $table::get()->byID($id)) { |
||
| 119 | if ($obj->hasDatabaseField($field)) { |
||
| 120 | if ($obj->canEdit()) { |
||
| 121 | $obj->$field = $newValue; |
||
| 122 | if ($obj instanceof SiteTree) { |
||
| 123 | $obj->writeToStage("Stage"); |
||
| 124 | $obj->publish("Stage", "Live"); |
||
| 125 | } else { |
||
| 126 | $obj->write(); |
||
| 127 | } |
||
| 128 | if ($titleField && $obj->hasDatabaseField($titleField)) { |
||
| 129 | $title = $obj->$titleField; |
||
| 130 | } elseif ($obj->hasMethod("Title")) { |
||
| 131 | $title = $obj->Title(); |
||
| 132 | } elseif ($obj->hasMethod("getTitle")) { |
||
| 133 | $title = $obj->getTitle(); |
||
| 134 | } elseif ($title = $obj->Title) { |
||
| 135 | //do nothing |
||
| 136 | } elseif ($title = $obj->Name) { |
||
| 137 | //do nothing |
||
| 138 | } else { |
||
| 139 | $title = $obj->ID; |
||
| 140 | } |
||
| 141 | $dbField = $obj->stat('db'); |
||
| 142 | $newValueObject = $obj->dbObject($field); |
||
| 143 | if ($newValueObject->hasMethod('Nice')) { |
||
| 144 | $newValueFancy = $newValueObject->Nice(); |
||
| 145 | } else { |
||
| 146 | $newValueFancy = $newValueObject->Raw(); |
||
| 147 | } |
||
| 148 | $updateCount++; |
||
| 149 | $updateMessage .= "Record updated: <i class=\"fieldTitle\">$field</i> for <i class=\"recordTitle\">".$title ."</i> updated to <i class=\"newValue\">".$newValueFancy."</i><br />"; |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | user_error("field does not exist", E_USER_ERROR); |
||
| 153 | } |
||
| 154 | } else { |
||
| 155 | user_error("could not find record: $table, $id ", E_USER_ERROR); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | if ($updateCount > 1) { |
||
| 160 | return "$updateCount records Updated"; |
||
| 161 | } else { |
||
| 162 | return $updateMessage; |
||
| 163 | } |
||
| 164 | } else { |
||
| 165 | user_error("data object specified: '$table' or id count: '".count($ids)."' or newValue: '$newValue' is not valid", E_USER_ERROR); |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | user_error("you need to be logged in to make the changes", E_USER_ERROR); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 242 |