function json_update($j, $updates) { if ($j && is_array($j)) { $j = (object) $j; } else { if (!$j || !is_object($j)) { $j = (object) array(); } } foreach ($updates as $k => $v) { if ((string) $k === "") { global $Me; error_log(json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)) . ", user {$Me->email}"); continue; } if ($v === null) { unset($j->{$k}); } else { if (is_object($v) || is_array($v)) { $v = json_update(isset($j->{$k}) ? $j->{$k} : null, $v); if ($v !== null) { $j->{$k} = $v; } else { unset($j->{$k}); } } else { $j->{$k} = $v; } } } $n = count(get_object_vars($j)); if ($n == 0) { return null; } for ($i = 0; $i != $n; ++$i) { if (!property_exists($j, $i)) { return $j; } } $j = get_object_vars($j); ksort($j, SORT_NUMERIC); return array_values($j); }
function update_contact_grade_info($updates, $reset_keys = false) { assert($this->pset->gitless); // find original $record = $this->grade; // compare-and-swap loop while (1) { // change notes $new_notes = json_update($record ? $record->notes : null, $updates); self::clean_notes($new_notes); // update database $notes = json_encode($new_notes); $hasactiveflags = self::notes_hasactiveflags($new_notes); if (!$record) { $result = $this->conf->qx("insert into ContactGrade set cid=?, pset=?, notes=?, hasactiveflags=?", $this->user->contactId, $this->pset->psetid, $notes, $hasactiveflags); } else { $result = $this->conf->qe("update ContactGrade set notes=?, hasactiveflags=?, notesversion=? where cid=? and pset=? and notesversion=?", $notes, $hasactiveflags, $record->notesversion + 1, $this->user->contactId, $this->pset->psetid, $record->notesversion); } if ($result && $result->affected_rows) { break; } // reload record $record = $this->pset->contact_grade_for($this->user); } if (!$record) { $record = (object) ["cid" => $this->user->contactId, "pset" => $this->pset->psetid, "gradercid" => null, "hidegrade" => 0, "notesversion" => 0]; } $record->notes = $new_notes; $record->hasactiveflags = $hasactiveflags; $record->notesversion = $record->notesversion + 1; $this->grade = $record; $this->grade_notes = $record->notes; }
function json_update($j, $updates) { if ($j && is_object($j)) { $j = (array) $j; } else { if (!is_array($j)) { $j = []; } } foreach ($updates as $k => $v) { if ((string) $k === "") { global $Me; error_log(json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)) . ", user {$Me->email}"); continue; } if ($v === null) { unset($j[$k]); } else { if (is_object($v) || is_associative_array($v)) { $v = json_update(isset($j[$k]) ? $j[$k] : null, $v); if ($v !== null) { $j[$k] = $v; } else { unset($j[$k]); } } else { $j[$k] = $v; } } } $n = count($j); if ($n == 0) { return null; } for ($i = 0; $i != $n; ++$i) { if (!array_key_exists($i, $j)) { return (object) $j; } } ksort($j, SORT_NUMERIC); return array_values($j); }