<?php

class Plugin_Stories {
  
  /**
   * add a profile based story
   * @param array $userData
   * @param array $storyData
   * @param array $imageFile
   * @return stdClass 
   */
  public function addProfileStory(Array $userData, Array $storyData, Array $imageFile){
    $return = new stdClass;
    $return->error = FALSE;
    $return->message = '';
    
    // change database to transaction
    $db = Zend_Db_Table::getDefaultAdapter();
    $db->beginTransaction();
    
    try {

      // treat the region and country
//      $countryParams = explode('-', $userData['country_id']);
//      if(count($countryParams) > 1){
//        $userData['country_region_id'] = $countryParams[0];
//        $userData['country_id'] = $countryParams[1];
//      } else {
//        $userData['country_region_id'] = $userData['country_id'];
//        unset($userData['country_id']);
//      }
      
      // 1. add user
      $modelUsers = new Model_Users;
      $user = $modelUsers->add($userData);
      if($user->error){
        throw new Exception($user->message);
      }
      
      // 2. add story with image and issues
      $storyData['user_id'] = $user->id;
      $storyData['type'] = 'P';
      $storyData['layout'] = 'L';
      $storyData['approved'] = '0';
      
      $modelStories = new Model_Stories;
      $story = $modelStories->add($storyData, $imageFile);
      if($story->error){
        throw new Exception($story->message);
      }
      
      // 3. login user
      $login = Plugin_User::singleton()->login($userData);
      if($login->error){
        throw new Exception('Problem with login:'.$login->message);
      }

      // 4. send email
      $mail = Plugin_Mailer::sendProfileStoryAdded($userData, $storyData);
      if($mail->error){
        throw new Exception($mail->message);
      }
      
      // attach story id
      $return->storyId = $story->id;
      
      // commit transactions
      $db->commit();
      
    } catch (Exception $exc){
      
      // rollback database changes
      $db->rollBack();
      
      $return->error = TRUE;
      $return->message = $exc->getMessage();
    }
    
    return $return;
  }
  
  /**
   * add a profile based story
   * @param array $userData
   * @param array $storyData
   * @param array $organizationData
   * @param array $imageFile
   * @return stdClass 
   */
  public function addOrganizationStory(Array $userData, Array $storyData, Array $organizationData, Array $imageFile){
    $return = new stdClass;
    $return->error = FALSE;
    $return->message = '';
    
    // change database to transaction
    $db = Zend_Db_Table::getDefaultAdapter();
    $db->beginTransaction();
    
    try {
      
      // 1. add user
      $modelUsers = new Model_Users;
      $user = $modelUsers->add($userData);
      if($user->error){
        throw new Exception($user->message);
      }
      
      // 2. add story with image and issues
      $storyData['user_id'] = $user->id;
      $storyData['type'] = 'O';
      $storyData['layout'] = 'L';
      $storyData['approved'] = '0';
      
      $modelStories = new Model_Stories;
      $story = $modelStories->add($storyData, $imageFile);
      if($story->error){
        throw new Exception($story->message);
      }
      
      // 3. add organization
      $organizationData['story_id'] = $story->id;
      
      $modelOrganizations = new Model_Stories_Organizations;
      $organization = $modelOrganizations->add($organizationData);
      if($organization->error){
        throw new Exception($organization->message);
      }
      
      // 4. login user
      $login = Plugin_User::singleton()->login($userData);
      if($login->error){
        throw new Exception('Problem with login:'.$login->message);
      }
      
      // 5. send email
      $mail = Plugin_Mailer::sendOrganizationStoryAdded($userData, $storyData);
      if($mail->error){
        throw new Exception($mail->message);
      }
      
      // attach story id
      $return->storyId = $story->id;
      
      // commit transactions
      $db->commit();
      
    } catch (Exception $exc){
      
      // rollback database changes
      $db->rollBack();
      
      $return->error = TRUE;
      $return->message = $exc->getMessage();
    }
    
    return $return;
  }
  
  public function updateProfileStory($storyData){
    $return = new stdClass;
    $return->error = FALSE;
    $return->message = '';

    try {
      // check for login
      $pluginUser = Plugin_User::singleton();
      
      if(!$pluginUser->isLogged()){
        throw new Exception("Forbidden action");
      }
      
      $user = $pluginUser->getUser();
      
      $modelStories = new Model_Stories;
      $story = $modelStories->findByUser($user['id']);
      $story->title = $storyData['title'];
      $story->text = $storyData['text'];

      $modelStories->updateRow($story->toArray());
      
    } catch (Exception $e) {
      $return->error = TRUE;
      $return->message = $e->getMessage();
    }
    return $return;
    
  }

  public function updateOrganizationStory($storyData, $organizationData){
    $return = new stdClass;
    $return->error = FALSE;
    $return->message = '';

    try {
      // check for login
      $pluginUser = Plugin_User::singleton();

      if(!$pluginUser->isLogged()){
        throw new Exception("Forbidden action");
      }

      $user = $pluginUser->getUser();

      $modelStories = new Model_Stories;
      $story = $modelStories->findByUser($user['id']);
      $story->title = $storyData['title'];
      $story->text = $storyData['text'];

      if(!empty($organizationData['name'])){
        $organization = $story->Organization()->current();
        $organization->name = $organizationData['name'];
        $modelOrganizations = new Model_Stories_Organizations();
        $modelOrganizations->updateRow($organization->toArray());
      }

      $modelStories->updateRow($story->toArray());

    } catch (Exception $e) {
      $return->error = TRUE;
      $return->message = $e->getMessage();
    }
    return $return;

  }

  public function updateStoryIssues($storyData, $issuesData){
    $return = new stdClass;
    $return->error = FALSE;
    $return->message = '';

    try {
      // check for login
      $pluginUser = Plugin_User::singleton();

      if(!$pluginUser->isLogged()){
        throw new Exception("Forbidden action");
      }

      $user = $pluginUser->getUser();

      $modelStories = new Model_Stories;
      $story = $modelStories->findByUser($user['id']);

      $modelStoryIssues = new Model_Stories_Issues;
      $modelStoryIssues->setFromStory($story, $issuesData);

      $story->other_issues = $storyData['other_issues'];
      $modelStories->updateRow($story->toArray());

    } catch (Exception $e) {
      $return->error = TRUE;
      $return->message = $e->getMessage();
    }
    return $return;

  }
  
}