DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d5c3254b3e56)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
#!/usr/bin/perl -w
# -*- mode: cperl; c-basic-offset: 8; indent-tabs-mode: nil; -*-

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Litmus.
#
# The Initial Developer of the Original Code is
# the Mozilla Corporation.
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Chris Cooper <ccooper@deadsquid.com>
#   Zach Lipton <zach@zachlipton.com>
#
# ***** END LICENSE BLOCK *****

use strict;
$|++;

#use Time::HiRes qw( gettimeofday tv_interval );
#my $t0 = [gettimeofday];

use Litmus;
use Litmus::Auth;
use Litmus::Cache;
use Litmus::Error;
use Litmus::FormWidget;
use Litmus::Utils qw(sanitize);

use CGI;
use Date::Manip;
use JSON -convert_blessed_universally;

Litmus->init();
Litmus::Auth::requireProductAdmin("manage_categories.cgi");

my $c = Litmus->cgi();
print $c->header();

my $message;
my $status;
my $rv;
my $defaults;

if ($c->param) {
  # auth:
  # must be a super user for all changes but branch changes:
  if ((!Litmus::Auth::getCurrentUser()->isSuperUser()) && 
  	($c->param("product_id") || $c->param("edit_product_form_mode") ||
  	$c->param("platform_id") || $c->param("edit_platform_form_mode") ||
  	$c->param("opsys_id") || $c->param("edit_opsys_form_mode") || 
  	$c->param("edit_opsys_form_opsys_id"))) {
  		Litmus::Auth::requireAdmin("manage_categories.cgi");
  	}

  my $now = &Date::Manip::UnixDate("now","%q");
  my $user_id = Litmus::Auth::getCurrentUser();

  # This next section allows us to display the right section by default,
  # facilitating navigation/management.
  # It's at the start of the param checking so that it can be overriden
  # if we actually create/modify/delete anything on the page further on.
  if ($c->param('goto_product')) {
    $defaults->{'product_id'} = 1;
  } elsif ($c->param('goto_platform')) {
    $defaults->{'platform_id'} = 1;
  } elsif ($c->param('goto_opsys')) {
    $defaults->{'opsys_id'} = 1;
  } elsif ($c->param('goto_branch')) {
    $defaults->{'branch_id'} = 1;
  }

  # Process product changes.
  if ($c->param("delete_product_button") and 
      $c->param("product_id")) {
    my $product_id = $c->param("product_id");
    my $product = Litmus::DB::Product->retrieve($product_id);
    if ($product) {
      $rv = $product->delete_with_refs();
      if ($rv) {
        $status = "success";
        $message = "Product ID# $product_id deleted successfully.";
      } else {
        $status = "failure";
        $message = "Failed to delete Product ID# $product_id.";
      }
    } else { 
      $status = "failure";
      $message = "Product ID# $product_id does not exist. (Already deleted?)";
    }
  } elsif ($c->param("edit_product_form_mode")) {
    my $enabled = $c->param('edit_product_form_enabled') ? 1 : 0;
    if ($c->param("edit_product_form_mode") eq "add") {
      my %hash = ( 
                  name => $c->param('edit_product_form_name'),
                  iconpath => $c->param('edit_product_form_iconpath'),
                  enabled => $enabled,
                  creation_date => $now,
                  last_updated => $now,
                  creator_id => $c->param('edit_product_form_created_by'),
                 );
      my $new_product = 
        Litmus::DB::Product->create(\%hash);
      if ($new_product) {
        # add a new security group by default for admins of the new product
        my $secgroup = Litmus::DB::SecurityGroup->create({
        	name => $new_product->name()." Administrators",
        	description => "Administrators of the ".$new_product->name()." product",
        	grouptype => 3,
        	isactive => 1,
        });
        Litmus::DB::GroupProductMap->create({group=>$secgroup, product=>$new_product});
      
        $status = "success";
        $message = "Product added successfully. New product ID# is " . $new_product->product_id;
        $defaults->{'product_id'} = $new_product->product_id;
      } else {
        $status = "failure";
        $message = "Failed to add product.";        
      }
    } elsif ($c->param("edit_product_form_mode") eq "edit") {
      my $product_id = $c->param("edit_product_form_product_id");
      my $product = Litmus::DB::Product->retrieve($product_id);
      if ($product) {
        $product->name($c->param('edit_product_form_name'));
        $product->iconpath($c->param('edit_product_form_iconpath') ? $c->param('edit_product_form_iconpath') : '');
        $product->enabled($enabled);
        $product->creator_id($c->param('edit_product_form_created_by'));
        $rv = $product->update();
        if ($rv) {
          $status = "success";
          $message = "Product ID# $product_id updated successfully.";
          $defaults->{'product_id'} = $product_id;
        } else {
          $status = "failure";
          $message = "Failed to update product ID# $product_id.";        
        }
      } else {
        $status = "failure";
        $message = "Product ID# $product_id not found.";        
      }
    }
  }

  # Process platform changes.
  if ($c->param("delete_platform_button") and 
      $c->param("platform_id")) {
    my $platform_id = $c->param("platform_id");
    my $platform = Litmus::DB::Platform->retrieve($platform_id);
    if ($platform) {
      $rv = $platform->delete_with_refs();
      if ($rv) {
        $status = "success";
        $message = "Platform ID# $platform_id deleted successfully.";
      } else {
        $status = "failure";
        $message = "Failed to delete Platform ID# $platform_id.";
      }
    } else { 
      $status = "failure";
      $message = "Platform ID# $platform_id does not exist. (Already deleted?)";
    }
  } elsif ($c->param("edit_platform_form_mode")) {
    if ($c->param("edit_platform_form_mode") eq "add") {
      my %hash = ( 
                  name => $c->param('edit_platform_form_name'),
                  iconpath => $c->param('edit_platform_form_iconpath'),
                  detect_regexp => $c->param('edit_platform_form_detect_regexp'),
                  creation_date => $now,
                  last_updated => $now,
                  creator_id => $c->param('edit_platform_form_created_by'),
                 );
      my $new_platform = 
        Litmus::DB::Platform->create(\%hash);
      if ($new_platform) {
        my @selected_products = $c->param("edit_platform_form_platform_products");
        $new_platform->update_products(\@selected_products);        
        $status = "success";
        $message = "Platform added successfully. New platform ID# is " . $new_platform->platform_id;
        $defaults->{'platform_id'} = $new_platform->platform_id;
      } else {
        $status = "failure";
        $message = "Failed to add platform.";        
      }
    } elsif ($c->param("edit_platform_form_mode") eq "edit") {
      my $platform_id = $c->param("edit_platform_form_platform_id");
      my $platform = Litmus::DB::Platform->retrieve($platform_id);
      if ($platform) {
        $platform->name($c->param('edit_platform_form_name'));
        $platform->iconpath($c->param('edit_platform_form_iconpath') ? $c->param('edit_platform_form_iconpath') : '');
        $platform->detect_regexp($c->param('edit_platform_form_detect_regexp') ? $c->param('edit_platform_form_detect_regexp') : '');
        $platform->creator_id($c->param('edit_platform_form_created_by'));
        $rv = $platform->update();
        if ($rv) {
          my @selected_products = $c->param("edit_platform_form_platform_products");
          $platform->update_products(\@selected_products);        
          $status = "success";
          $message = "Platform ID# $platform_id updated successfully.";
          $defaults->{'platform_id'} = $platform_id;
        } else {
          $status = "failure";
          $message = "Failed to update platform ID# $platform_id.";        
        }
      } else {
        $status = "failure";
        $message = "Platform ID# $platform_id not found.";        
      }
    }
  }

  # Process opsys changes.
  if ($c->param("delete_opsys_button") and 
      $c->param("opsys_id")) {
    my $opsys_id = $c->param("opsys_id");
    my $opsys = Litmus::DB::Opsys->retrieve($opsys_id);
    if ($opsys) {
      $rv = $opsys->delete;
      if ($rv) {
        $status = "success";
        $message = "Operating system ID# $opsys_id deleted successfully.";
      } else {
        $status = "failure";
        $message = "Failed to delete Operating system ID# $opsys_id.";
      }
    } else { 
      $status = "failure";
      $message = "Operating system ID# $opsys_id does not exist. (Already deleted?)";
    }
  } elsif ($c->param("edit_opsys_form_mode")) {
    if ($c->param("edit_opsys_form_mode") eq "add") {
      my %hash = ( 
                  name => $c->param('edit_opsys_form_name'),
                  platform_id => $c->param('edit_opsys_form_platform_id'),
                  detect_regexp => $c->param('edit_opsys_form_detect_regexp'),
                  creation_date => $now,
                  last_updated => $now,
                  creator_id => $c->param('edit_opsys_form_created_by'),
                 );
      my $new_opsys = 
        Litmus::DB::Opsys->create(\%hash);
      if ($new_opsys) {
        $status = "success";
        $message = "Operating system added successfully. New operating system ID# is " . $new_opsys->opsys_id;
        $defaults->{'opsys_id'} = $new_opsys->opsys_id;
      } else {
        $status = "failure";
        $message = "Failed to add operating system.";        
      }
    } elsif ($c->param("edit_opsys_form_mode") eq "edit") {
      my $opsys_id = $c->param("edit_opsys_form_opsys_id");
      my $opsys = Litmus::DB::Opsys->retrieve($opsys_id);
      if ($opsys) {
        $opsys->name($c->param('edit_opsys_form_name'));
        $opsys->platform_id($c->param('edit_opsys_form_platform_id'));
        $opsys->detect_regexp($c->param('edit_opsys_form_detect_regexp') ? $c->param('edit_opsys_form_detect_regexp') : '');
        $opsys->creator_id($c->param('edit_opsys_form_created_by'));
        $rv = $opsys->update();
        if ($rv) {
          $status = "success";
          $message = "Operating system ID# $opsys_id updated successfully.";
          $defaults->{'opsys_id'} = $opsys_id;
        } else {
          $status = "failure";
          $message = "Failed to update operating system ID# $opsys_id.";
        }
      } else {
        $status = "failure";
        $message = "Operating systen ID# $opsys_id not found.";        
      }
    }
  }

  # Process branch changes.
  if ($c->param("delete_branch_button") and 
      $c->param("branch_id")) {
    my $branch_id = $c->param("branch_id");
    my $branch = Litmus::DB::Branch->retrieve($branch_id);
    if ($branch) {
      # must be a product admin
      Litmus::Auth::requireProductAdmin("manage_categories.cgi", $branch->product());
      $rv = $branch->delete;
      if ($rv) {
        $status = "success";
        $message = "Branch ID# $branch_id deleted successfully.";
      } else {
        $status = "failure";
        $message = "Failed to delete branch ID# $branch_id.";
      }
    } else { 
      $status = "failure";
      $message = "Branch ID# $branch_id does not exist. (Already deleted?)";
    }

  } elsif ($c->param("clone_branch_button") and 
           $c->param("branch_id")) {

    my $branch_id = $c->param("branch_id");
    my $branch = Litmus::DB::Branch->retrieve($branch_id);
    if ($branch) {
      # must be a product admin to edit the branch:
      Litmus::Auth::requireProductAdmin('manage_categories.cgi',
                                        $branch->product());

      # Do we already have our cloning info? Then get cloning!
      if ($c->param("clone_type")) {
        my $clone_type = sanitize($c->param("clone_type"));
        my $new_name = $c->param("new_branch_name");
        my $new_regexp = sanitize($c->param("new_branch_detect_regexp"));
        my $new_branch;
        if ($clone_type eq "simple") {
          $new_branch = $branch->clone($new_name,$new_regexp);
        } elsif ($clone_type eq "recursive") {
          my $old_name_regexp = sanitize($c->param("old_name_regexp"));
          my $new_name_regexp = sanitize($c->param("new_name_regexp"));
          $new_branch = $branch->clone_recursive($new_name,
                                                 $new_regexp,
                                                 $old_name_regexp,
                                                 $new_name_regexp
                                                 );
        } 
     
        if ($new_branch) {
          $status = "success";
          $message = "Branch ID# $branch_id cloned successfully as branch ID# " . $new_branch->branch_id . ".";
          $defaults->{'branch_id'} = $new_branch->branch_id;
        } else {
          $status = "failure";
          $message = "Failed to clone branch ID# $branch_id.";
        }

      } else {
        my $vars = {
                    title => 'Manage Categories - Clone Branch',
                    branch => $branch,
                   };

        Litmus->template()->process("admin/clone_branch.tmpl", $vars) ||
          internalError(Litmus->template()->error());

        exit;
      }
    } else {
      $status = "failure";
      $message = "Failed to lookup branch ID# $branch_id.";
    }

  } elsif ($c->param("edit_branch_form_mode")) {
  	# need to be a product admin for the branch's product and for any
  	# product the branch may be moved into:
  	Litmus::Auth::requireProductAdmin("manage_categories", 
  		$c->param('edit_branch_form_product_id'));
  
    my $enabled = $c->param('edit_branch_form_enabled') ? 1 : 0;
    if ($c->param("edit_branch_form_mode") eq "add") {
      my %hash = ( 
                  name => $c->param('edit_branch_form_name'),
                  product_id => $c->param('edit_branch_form_product_id'),
                  detect_regexp => $c->param('edit_branch_form_detect_regexp'),
                  enabled => $enabled,
                  creation_date => $now,
                  last_updated => $now,
                  creator_id => $c->param('edit_branch_form_created_by'),
                 );
      my $new_branch = 
        Litmus::DB::Branch->create(\%hash);
      if ($new_branch) {
        $status = "success";
        $message = "Branch added successfully. New branch ID# is " . $new_branch->branch_id;
        $defaults->{'branch_id'} = $new_branch->branch_id;
      } else {
        $status = "failure";
        $message = "Failed to add branch.";
      }
    } elsif ($c->param("edit_branch_form_mode") eq "edit") {
      my $branch_id = $c->param("edit_branch_form_branch_id");
      my $branch = Litmus::DB::Branch->retrieve($branch_id);
      if ($branch) {
      	# must be a product admin to edit the branch:
      	Litmus::Auth::requireProductAdmin('manage_categories.cgi', 
      		$branch->product());
      	
        $branch->name($c->param('edit_branch_form_name'));
        $branch->product_id($c->param('edit_branch_form_product_id'));
        $branch->detect_regexp($c->param('edit_branch_form_detect_regexp') ? $c->param('edit_branch_form_detect_regexp') : '');
        $branch->enabled($enabled);
        $branch->creator_id($c->param('edit_branch_form_created_by'));
        $rv = $branch->update();
        if ($rv) {
          $status = "success";
          $message = "Branch ID# $branch_id updated successfully.";
          $defaults->{'branch_id'} = $branch_id;
        } else {
          $status = "failure";
          $message = "Failed to update branch ID# $branch_id.";
        }
      } else {
        $status = "failure";
        $message = "Branch ID# $branch_id not found.";        
      }
    }
  }

  }
  

my $products = Litmus::FormWidget->getProducts();
my $platforms = Litmus::FormWidget->getPlatforms();
my $branches = Litmus::FormWidget->getBranches();
my $testgroups = Litmus::FormWidget->getTestgroups();
my $opsyses = Litmus::FormWidget->getOpsyses();
my $locales = Litmus::FormWidget->getLocales;
my $authors = Litmus::FormWidget->getAuthors();

# if the user is not a superuser, only allow them access to the 
# branches they are product admins for
my $authorized_branches;
if (Litmus::Auth::getCurrentUser()->isSuperUser()) {
	$authorized_branches = $branches;
} else {
	my @tmp;
	foreach my $b (@{$branches}) {
		my %cur = %{$b};
		if (Litmus::Auth::getCurrentUser()->isProductAdmin($cur{product_id})) {
			push(@tmp, $b);
		}
	}
	$authorized_branches = \@tmp;
}
$branches = $authorized_branches;

my $json = new JSON;
my $products_js = $json->allow_unknown->convert_blessed->encode($products);
my $branches_js = $json->allow_unknown->convert_blessed->encode($branches);
my $testgroups_js = $json->allow_unknown->convert_blessed->encode($testgroups);

my $vars = {
            title => 'Manage Categories',
            products => $products,
            platforms => $platforms,
            branches => $branches,
            opsyses => $opsyses,
            locales => $locales,
           };
  
$vars->{'products_js'} = $products_js;
$vars->{'branches_js'} = $branches_js;
$vars->{'testgroups_js'} = $testgroups_js;
$vars->{'authors'} = $authors;
$vars->{'user'} = Litmus::Auth::getCurrentUser();

if ($status and $message) {
  $vars->{'onload'} = "toggleMessage('$status','$message');";
}

my $cookie =  Litmus::Auth::getCookie();
$vars->{"defaultemail"} = $cookie;
$vars->{"show_admin"} = Litmus::Auth::istrusted($cookie);

if ($defaults) {
  $vars->{'defaults'} = $defaults;
}

Litmus->template()->process("admin/edit_categories.tmpl", $vars) ||
  internalError(Litmus->template()->error());

#my $elapsed = tv_interval ( $t0 );
#printf  "<div id='pageload'>Page took %f seconds to load.</div>", $elapsed;

exit 0;