function savePerms() { global $db, $row; if ($row->id == 1) { return; } $approlepermDAO = new ApprolepermDAO($db); $apppermDAO = new ApppermDAO($db); $perms = array(); foreach ($apppermDAO->findAll('perm_name') as $perm) { $valname = 'perm_nameSelected_' . $perm->perm_name; if (isset($_POST[$valname]) && (int) $_POST[$valname] != 0) { $obj = new stdClass(); $obj->role_name = $row->role_name; $obj->perm_name = $perm->perm_name; $perms[] = $obj; } } ChildRowUpdater::updateChildRows($db, 'Approleperm', $perms, array('role_name' => $row->role_name), array('role_name', 'perm_name')); }
public static function updateChildRows($db, $childDataClassName, $childRows, $commonIdentifiers, $uniqueIdentifyingColumnNames, $forcedChildRowValues = null, $neverUpdateChildColumnNames = null, $childRowInsertCallback = null, $childRowUpdateCallback = null, $childRowDeleteCallback = null) { $childDAOClassName = $childDataClassName . 'DAO'; $childTableName = strtolower(substr($childDataClassName, 0, 1)) . substr($childDataClassName, 1); $childDAO = new $childDAOClassName($db); if (!isset($childRows) || !is_array($childRows)) { $childRows = array(); } $commonIdWhereClause = ''; $whereAnd = ' where '; foreach (array_keys($commonIdentifiers) as $cn) { $commonIdWhereClause .= $whereAnd . $cn . ' = ?'; $whereAnd = ' and '; } $uniqueIdWhereClause = ''; $whereAnd = ' where '; foreach ($uniqueIdentifyingColumnNames as $cn) { $uniqueIdWhereClause .= $whereAnd . $cn . ' = ?'; $whereAnd = ' and '; } // Find existing child rows; load them into $oldRows. $ps = new PreparedStatement('select * from ' . $childTableName . $commonIdWhereClause); $row = new $childDataClassName(); $row->loadFromArray($commonIdentifiers); ChildRowUpdater::loadPS($ps, array_keys($commonIdentifiers), $row); $oldRows = $childDAO->findWithPreparedStatement($ps); // Delete rows which exist in $oldRows but don't exist in the $childRows array. $ps = new PreparedStatement('delete from ' . $childTableName . $uniqueIdWhereClause); for ($ori = 0; $ori < count($oldRows);) { $oldRow = $oldRows[$ori]; $found = false; foreach ($childRows as $cr) { $childRow = new $childDataClassName(); $childRow->loadFromArray((array) $cr); foreach ($commonIdentifiers as $key => $val) { $childRow->{$key} = $val; } $found = true; foreach ($uniqueIdentifyingColumnNames as $cn) { if ($childRow->{$cn} != $oldRow->{$cn}) { $found = false; break; } } if ($found) { break; } } if (!$found) { if (strlen($childRowDeleteCallback) > 0 || is_array($childRowDeleteCallback)) { call_user_func($childRowDeleteCallback, $oldRow); } ChildRowUpdater::loadPS($ps, $uniqueIdentifyingColumnNames, $oldRow); $db->executeUpdate($ps); unset($oldRows[$ori]); $oldRows = array_slice($oldRows, 0); } else { $ori++; } } // Update existing rows; insert missing rows. foreach ($childRows as $cr) { $childRow = new $childDataClassName(); $childRow->loadFromArray((array) $cr); foreach ($commonIdentifiers as $key => $val) { $childRow->{$key} = $val; } $found = false; foreach ($oldRows as $oldRow) { $found = true; foreach ($uniqueIdentifyingColumnNames as $cn) { if ($childRow->{$cn} != $oldRow->{$cn}) { $found = false; break; } } if ($found) { break; } } if ($found && is_array($neverUpdateChildColumnNames)) { foreach ($neverUpdateChildColumnNames as $cn) { $childRow->{$cn} = $oldRow->{$cn}; } } if (is_array($forcedChildRowValues)) { foreach ($forcedChildRowValues as $key => $val) { $childRow->{$key} = $val; } } if ($found) { $childDAO->update($childRow); if (strlen($childRowUpdateCallback) > 0 || is_array($childRowUpdateCallback)) { call_user_func($childRowUpdateCallback, $childRow, $cr); } } else { $childDAO->insert($childRow); if (strlen($childRowInsertCallback) > 0 || is_array($childRowInsertCallback)) { call_user_func($childRowInsertCallback, $childRow, $cr); } } } }
function saveRoles($user_id) { global $db, $row, $MAX_RESERVED_ID; if ($row->id > 0 && $row->id <= $MAX_RESERVED_ID) { return; } $appuserroleDAO = new AppuserroleDAO($db); $approleDAO = new ApproleDAO($db); $roles = array(); foreach ($approleDAO->findAll('sort_order, role_name') as $role) { $valname = 'role_nameSelected_' . $role->role_name; if (isset($_POST[$valname]) && (int) $_POST[$valname] != 0) { $obj = new stdClass(); $obj->user_id = $user_id; $obj->role_name = $role->role_name; $roles[] = $obj; } } ChildRowUpdater::updateChildRows($db, 'Appuserrole', $roles, array('user_id' => $user_id), array('user_id', 'role_name')); }