function syncLists(Am_Newsletter_Plugin $plugin, array $lists) { $existing = $disabled = array(); foreach ($this->findByPluginId($plugin->getId()) as $r) { if ($r->disabled) { $disabled[$r->plugin_list_id] = $r; } else { $existing[$r->plugin_list_id] = $r; } } // disable exising lists foreach (array_diff_key($existing, $lists, $disabled) as $list) { $list->disable(); // not available on service side anymore? } // foreach (array_diff_key($lists, $existing, $disabled) as $id => $list) { $r = $this->createRecord(array('plugin_id' => $plugin->getId(), 'plugin_list_id' => $id, 'title' => $list['title'])); $r->insert(); } // list is now enabled foreach (array_intersect_key($disabled, $lists) as $list) { $list->enable(); } //sync titles for existing lists foreach (array_intersect_key($existing, $lists) as $list) { if ($list->title != $lists[$list->plugin_list_id]['title']) { $list->updateQuick('title', $lists[$list->plugin_list_id]['title']); } } }