]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/UserFlag/flagprofile.php
* i18n/L10n review.
[quix0rs-gnu-social.git] / plugins / UserFlag / flagprofile.php
index f128b9c40e32a7ed5ada00e0ca5ecc8b3cd067bc..283eea40ce157289d811ebc55f5c5071ff0631c7 100644 (file)
@@ -40,12 +40,8 @@ if (!defined('STATUSNET')) {
  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  * @link     http://status.net/
  */
-
-class FlagprofileAction extends Action
+class FlagprofileAction extends ProfileFormAction
 {
-    var $profile = null;
-    var $flag    = null;
-
     /**
      * Take arguments for running
      *
@@ -53,49 +49,21 @@ class FlagprofileAction extends Action
      *
      * @return boolean success flag
      */
-
     function prepare($args)
     {
-        parent::prepare($args);
-
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
-            throw new ClientException(_('Action only accepts POST'));
-        }
-
-        if (!common_logged_in()) {
-            $this->clientError(_('Not logged in.'));
+        if (!parent::prepare($args)) {
             return false;
         }
 
-        $id = $this->trimmed('flagprofileto');
-
-        if (!$id) {
-            $this->clientError(_('No profile specified.'));
-            return false;
-        }
-
-        $this->profile = Profile::staticGet('id', $id);
-
-        if (empty($this->profile)) {
-            $this->clientError(_('No profile with that ID.'));
-            return false;
-        }
-
-        $this->flag = $this->trimmed('flag');
-
-        if (empty($this->flag)) {
-            $this->flag = Profile_flag::DEFAULTFLAG;
-        }
-
         $user = common_current_user();
 
         assert(!empty($user)); // checked above
+        assert(!empty($this->profile)); // checked above
 
         if (User_flag_profile::exists($this->profile->id,
-                                      $user->id,
-                                      $this->flag))
-        {
-            $this->clientError(_('Flag already exists.'));
+                                      $user->id)) {
+            // TRANS: Client error when setting flag that has already been set for a profile.
+            $this->clientError(_m('Flag already exists.'));
             return false;
         }
 
@@ -105,64 +73,62 @@ class FlagprofileAction extends Action
     /**
      * Handle request
      *
+     * Overriding the base Action's handle() here to deal check
+     * for Ajax and return an HXR response if necessary
+     *
      * @param array $args $_REQUEST args; handled in prepare()
      *
      * @return void
      */
-
     function handle($args)
     {
-        parent::handle($args);
-
-        $this->flagProfile();
-        $this->returnTo();
-    }
-
-    function title() {
-        return _('Flag profile');
+        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+            $this->handlePost();
+            if (!$this->boolean('ajax')) {
+                $this->returnToPrevious();
+            }
+        }
     }
 
     /**
-     * save the profile flag
+     * Handle POST
      *
      * @return void
      */
-
-    function flagProfile()
+    function handlePost()
     {
         $user = common_current_user();
 
         assert(!empty($user));
         assert(!empty($this->profile));
-        assert(!empty($this->flag));
 
-        $ufp = new User_flag_profile();
+        // throws an exception on error
 
-        $ufp->profile_id = $this->profile->id;
-        $ufp->user_id    = $user->id;
-        $ufp->flag       = $this->flag;
-        $ufp->created    = common_sql_now();
+        User_flag_profile::create($user->id, $this->profile->id);
 
-        if (!$ufp->insert()) {
-            throw new ServerException(sprintf(_("Couldn't flag profile '%s' with flag '%s'."),
-                                              $this->profile->nickname, $this->flag));
+        if ($this->boolean('ajax')) {
+            $this->ajaxResults();
         }
-
-        $ufp->free();
     }
 
-    function returnTo()
+    /**
+     * Return results as AJAX message
+     *
+     * @return void
+     */
+    function ajaxResults()
     {
-        // Now, gotta figure where we go back to
-        foreach ($this->args as $k => $v) {
-            if ($k == 'returnto-action') {
-                $action = $v;
-            } elseif (substr($k, 0, 9) == 'returnto-') {
-                $args[substr($k, 9)] = $v;
-            }
-        }
-
-        common_redirect(common_local_url($action, $args), 303);
+        header('Content-Type: text/xml;charset=utf-8');
+        $this->xw->startDocument('1.0', 'UTF-8');
+        $this->elementStart('html');
+        $this->elementStart('head');
+        // TRANS: AJAX form title for a flagged profile.
+        $this->element('title', null, _m('Flagged for review'));
+        $this->elementEnd('head');
+        $this->elementStart('body');
+        // TRANS: Body text for AJAX form when a profile has been flagged for review.
+        $this->element('p', 'flagged', _m('Flagged'));
+        $this->elementEnd('body');
+        $this->elementEnd('html');
     }
 }
-