function sync_from_API($mode = '') { global $beanFiles; require_once $beanFiles['TILKEE_PROJECTS']; $associated_project = new TILKEE_PROJECTS(); $projects_array = $this->get_linked_beans('tilkee_projects_tilkee_tilks', 'TILKEE_PROJECTS'); $tilkee_project_id = 0; foreach ($projects_array as $project) { $tilkee_project_id = $project->tilkee_id; } if (!empty($tilkee_project_id)) { require_once 'custom/include/externalAPI/Tilkee/ExtAPITilkee.php'; $tilkee = new ExtAPITilkee(); $result = $tilkee->infos_tilk($tilkee_project_id, $this->tilkee_id); if ($result != -1 && !empty($result)) { // project updated : init bean $this->name = $result->title; $this->tilk_url = $result->url; $this->won = $result->won == 1 ? 'true' : 'false'; $this->archived = !empty($result->archived_at) ? 'true' : 'false'; $this->created_at = !empty($result->created_at) ? date('Y-m-d H:i:s', strtotime($result->created_at)) : ''; $this->archived_at = !empty($result->archived_at) ? date('Y-m-d H:i:s', strtotime($result->archived_at)) : ''; $this->last_sign_in_at = !empty($result->last_sign_in_at) ? date('Y-m-d H:i:s', strtotime($result->last_sign_in_at)) : ''; $this->total_time = $this->convert_time($result->total_time); $this->total_connexion = $result->total_connexion; $this->save(); // UPDATE LINKED CONNEXIONS if ($mode == 'update_connexions') { global $beanFiles; require_once $beanFiles['TILKEE_CONNEXIONS']; $cur_connexions = new TILKEE_CONNEXIONS(); $cur_connexions->updateFromArray($this->id, $result->connexions); } } else { global $mod_strings; SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_SYNC_ON_TILKEE']); $GLOBALS['log']->fatal('TILKS sync_from_API -> ERROR RETRIEVE TILK INFOS FOR TILK : ' . $this->tilkee_id . ' - PROJECT : ' . $associated_project->tilkee_id); } } return 0; }
function updateFromArray($tilkee_tilk_id, $connexions_array) { global $beanFiles; require_once $beanFiles['TILKEE_PROJECTS']; require_once $beanFiles['TILKEE_CONNEXIONS']; if (!empty($tilkee_tilk_id)) { foreach ($connexions_array as $connexion) { $search_connexion = new TILKEE_CONNEXIONS(); $search_connexion->retrieve_by_string_fields(array('connexion_id' => $connexion->id)); if (!empty($search_connexion->id)) { // Update CONNEXION $search_connexion->name = "[CONNEXION] "; $search_connexion->device = $connexion->device; $search_connexion->browser = $connexion->browser; $search_connexion->plateform = $connexion->plateform; $search_connexion->os = $connexion->os; $search_connexion->ip_address = $connexion->ip_address; $search_connexion->access_date = !empty($connexion->access_date) ? date('Y-m-d H:i:s', strtotime($connexion->access_date)) : ''; $search_connexion->total_time = $this->convert_time($connexion->total_time); $search_connexion->downloads = $connexion->downloads; $search_connexion->save(); } else { // Create CONNEXION $new_connexion = new TILKEE_CONNEXIONS(); $new_connexion->name = "[CONNEXION] "; $new_connexion->connexion_id = $connexion->id; $new_connexion->device = $connexion->device; $new_connexion->browser = $connexion->browser; $new_connexion->plateform = $connexion->plateform; $new_connexion->os = $connexion->os; $new_connexion->ip_address = $connexion->ip_address; $new_connexion->access_date = !empty($connexion->access_date) ? date('Y-m-d H:i:s', strtotime($connexion->access_date)) : ''; $new_connexion->total_time = $this->convert_time($connexion->total_time); $new_connexion->downloads = $connexion->downloads; $new_connexion->tilkee_tilks_id = $tilkee_tilk_id; $new_connexion->save(); // Init relationship with Tilkee Tilk $new_connexion->load_relationship('tilkee_tilks_tilkee_connexions'); $new_connexion->tilkee_tilks_tilkee_connexions->add($tilkee_tilk_id); } unset($search_connexion); } // CONNEXIONS LOOP } // Sugar TILK ID is set }