<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Network_Plugin_User extends Core_Model_Abstract
{
  public function onUserCreateAfter($event)
  {
    $payload = $event->getPayload();
    Engine_Api::_()->getDbtable('networks', 'network')->recalculate($payload);
  }

  public function onFieldsValuesSave($event)
  {
    $payload = $event->getPayload();
    if( $payload['item'] instanceof User_Model_User ) {
      Engine_Api::_()->getDbtable('networks', 'network')->recalculate($payload['item'], $payload['values']);
    }
  }

  public function onUserDeleteBefore($event)
  {
    $payload = $event->getPayload();
    if( $payload instanceof User_Model_User ) {
      $networkApi = Engine_Api::_()->getDbtable('membership', 'network');
      $ids = $networkApi->getMembershipsOfIds($payload, true);
      if(!empty($ids)){
        foreach( $networkApi->getMembershipsOf($payload) as $network ) {
          $networkApi->removeMember($network, $payload);
        }
      }
    }
  }
}
