Example #1
0
/**
 * Internal recursive function - finds all settings from submitted form
 *
 * @param object $node Instance of admin_category, or admin_settingpage
 * @param array $data
 * @return array
 */
function admin_find_write_settings($node, $data)
{
    $return = array();
    if (empty($data)) {
        return $return;
    }
    if ($node instanceof admin_category) {
        $entries = array_keys($node->children);
        foreach ($entries as $entry) {
            $return = array_merge($return, admin_find_write_settings($node->children[$entry], $data));
        }
    } else {
        if ($node instanceof admin_settingpage) {
            foreach ($node->settings as $setting) {
                $fullname = $setting->get_full_name();
                if (array_key_exists($fullname, $data)) {
                    $return[$fullname] = $setting;
                }
            }
        }
    }
    return $return;
}
 protected function save_config_data(admin_root $adminroot, array $data)
 {
     $adminroot->errors = array();
     $settings = admin_find_write_settings($adminroot, $data);
     $count = 0;
     foreach ($settings as $fullname => $setting) {
         /** @var $setting admin_setting */
         $original = $setting->get_setting();
         $error = $setting->write_setting($data[$fullname]);
         if ($error !== '') {
             $adminroot->errors[$fullname] = new stdClass();
             $adminroot->errors[$fullname]->data = $data[$fullname];
             $adminroot->errors[$fullname]->id = $setting->get_id();
             $adminroot->errors[$fullname]->error = $error;
         }
         $count++;
     }
     return $count;
 }