Пример #1
0
 public function update($id, $request)
 {
     $req = $request->getParameters();
     $data = $req;
     $data['current'] = 'channels';
     $name = @$req['name'];
     $descr = @$req['description'];
     $admins = @json_decode($req['_admins']);
     if (isset($req['editChannelSubmit']) && Session::isActive()) {
         $channel = UserChannel::exists($id) ? UserChannel::find($id) : UserChannel::find_by_name($id);
         if (!is_object($channel)) {
             return Utils::getNotFoundResponse();
         }
         if (!$channel->belongToUser(Session::get()->id)) {
             return Utils::getForbiddenResponse();
         }
         $data['mainChannel'] = $channel->isUsersMainChannel(Session::get()->id);
         $data['name'] = $channel->name;
         $data['description'] = $channel->description;
         $data['currentPageTitle'] = $channel->name . ' - Edition';
         $data['owner_id'] = $channel->owner_id;
         $admins_array_ids = explode(';', trim($channel->admins_ids, ';'));
         $data['admins_ids'] = $admins_array_ids;
         $data['admins'] = array();
         foreach ($admins_array_ids as $adm) {
             $data['admins'][] = User::find_by_id($adm)->getMainChannel();
         }
         if (isset($req['name'], $req['description'])) {
             if (strlen($name) >= 3 && strlen($name) <= 40) {
                 if (preg_match("#^[a-zA-Z0-9\\_\\-\\.]+\$#", $name)) {
                     if ($channel->isUsersMainChannel(Session::get()->id)) {
                         if ($channel->name != $req['name']) {
                             $data['name'] = $channel->name;
                             $response = new ViewResponse('channel/edit', $data);
                             $response->addMessage(ViewMessage::error('Vous ne pouvez pas changer le nom de votre chaîne principale !'));
                             return $response;
                         }
                     } else {
                         $adm = trim($channel->admins_ids, ';');
                         $adm = explode(';', $adm);
                         foreach ($admins as $admin) {
                             if ($admin > 0) {
                                 if (!in_array($admin, $adm)) {
                                     $adm[] = $admin;
                                     ChannelAction::create(array('id' => ChannelAction::generateId(6), 'channel_id' => $channel->id, 'recipients_ids' => ';' . $admin . ';', 'type' => 'admin', 'target' => $channel->id, 'timestamp' => Utils::tps()));
                                 }
                             } else {
                                 $value = -1 * $admin;
                                 if (in_array($value, $adm) && $channel->owner_id != $value) {
                                     $id = array_keys($adm, $value);
                                     unset($adm[$id[0]]);
                                     ChannelAction::create(array('id' => ChannelAction::generateId(6), 'channel_id' => $channel->id, 'recipients_ids' => ';' . $admin . ';', 'type' => 'unadmin', 'target' => $channel->id, 'timestamp' => Utils::tps()));
                                 }
                             }
                         }
                         $adm = ';' . implode(';', $adm) . ';';
                     }
                     UserChannel::edit($channel->id, $name, $descr, $adm, $req['_FILES_']['avatar'], $req['_FILES_']['background']);
                     //TODO: Support logo/background
                     $data['channels'] = Session::get()->getOwnedChannels();
                     $data['currentPageTitle'] = 'Mes chaines';
                     $response = new ViewResponse('account/channels', $data);
                     $response->addMessage(ViewMessage::success('Votre chaîne ' . $name . ' a bien été modifiée !'));
                     return $response;
                 } else {
                     $response = new ViewResponse('channel/edit', $data);
                     $response->addMessage(ViewMessage::error('Le nom de la chaîne doit contenir uniquement des lettres (majuscules et minuscules), des traits-d\'union, des _ et des points.'));
                     return $response;
                 }
             } else {
                 $response = new ViewResponse('channel/edit', $data);
                 $response->addMessage(ViewMessage::error('Le nom de la chaîne doit être compris entre 3 et 40 caractères.'));
                 return $response;
             }
         } else {
             $response = new ViewResponse('channel/edit', $data);
             $response->addMessage(ViewMessage::error('Tous les champs doivent être remplis.'));
             return $response;
         }
     } else {
         if (isset($req['subscribe'])) {
             if (Session::isActive()) {
                 $channel = UserChannel::exists($id) ? UserChannel::find($id) : UserChannel::find_by_name($id);
                 if (is_object($channel) && !$channel->belongToUser(Session::get()->id)) {
                     $channel->subscribe(Session::get()->id);
                     $response = new Response(200);
                     return $response;
                 }
             } else {
                 return new Response(500);
             }
         } else {
             if (isset($req['unsubscribe'])) {
                 if (Session::isActive()) {
                     $channel = UserChannel::exists($id) ? UserChannel::find($id) : UserChannel::find_by_name($id);
                     if (is_object($channel) && !$channel->belongToUser(Session::get()->id)) {
                         $channel->unsubscribe(Session::get()->id);
                         $response = new Response(200);
                         return $response;
                     }
                 } else {
                     return new Response(500);
                 }
             } else {
                 if (isset($req['admin_edit'])) {
                     if (Session::isActive()) {
                         $channel = UserChannel::exists($id) ? UserChannel::find($id) : UserChannel::find_by_name($id);
                         if (!$channel) {
                             return Utils::getNotFoundResponse();
                         }
                         if (!$channel->isUsersMainChannel(Session::get()->id) && $channel->owner_id != Session::get()->id) {
                             if (in_array($channel, Session::get()->getOwnedChannels())) {
                                 $current_admins = $channel->admins_ids;
                                 $current_admins = trim($current_admins, ";");
                                 $current_admins = explode(";", $current_admins);
                                 foreach ($current_admins as $k => $admin) {
                                     if ($admin == Session::get()->id) {
                                         unset($current_admins[$k]);
                                         $channel->admins_ids = ";" . implode($current_admins, ";") . ";";
                                         $channel->save();
                                         return new RedirectResponse(WEBROOT . "channel/{$id}");
                                     }
                                 }
                             }
                         }
                     }
                     return Utils::getForbiddenResponse();
                 }
             }
         }
     }
 }