function processView()
 {
     $GLOBALS['system']->includeDBClass('service_component_category');
     $this->category = new Service_Component_Category($_REQUEST['categoryid']);
     if (!empty($_FILES['datafile']) && !empty($_FILES['datafile']['tmp_name'])) {
         $GLOBALS['system']->doTransaction('BEGIN');
         $GLOBALS['system']->includeDBClass('service_component');
         $comp = new Service_Component();
         $fp = fopen($_FILES['datafile']['tmp_name'], 'r');
         if (!$fp) {
             trigger_error("Your data file could not be read.  Please check the file and try again");
             return;
         }
         $toprow = fgetcsv($fp, 0, ",", '"');
         $rowNum = 1;
         $all_ccli = Service_Component::getAllByCCLINumber();
         while ($row = fgetcsv($fp, 0, ",", '"')) {
             $comp->populate(0, array());
             $this->_captureErrors();
             $data = array();
             foreach ($row as $k => $v) {
                 $data[strtolower($toprow[$k])] = $v;
             }
             if (isset($data['content'])) {
                 $c = trim($data['content']);
                 $c = str_replace("\r", "", $c);
                 $c = str_replace("\n\n", "</p><p>", $c);
                 $c = str_replace("\n", "<br />", $c);
                 $data['content_html'] = '<p>' . $c . '</p>';
                 unset($data['content']);
             }
             if (isset($data['show_in_handout'])) {
                 $val = $data['show_in_handout'];
                 $map = array('y' => 'full', 'n' => 0, 'yes' => 'full', 'no' => 0);
                 $val = array_get($map, strtolower($val), $val);
                 if (!in_array($val, array('0', 'title', 'full'))) {
                     $val = '0';
                 }
                 $data['show_in_handout'] = $val;
             }
             if (!empty($_REQUEST['dupe-match']) && !empty($data['ccli_number']) && isset($all_ccli[$data['ccli_number']])) {
                 $comp->load($all_ccli[$data['ccli_number']]);
                 $comp->fromCSVRow($data);
                 foreach ($_REQUEST['congregationids'] as $congid) {
                     $comp->addCongregation($congid);
                 }
                 $comp->save();
             } else {
                 $comp->fromCSVRow($data);
                 $comp->setValue('categoryid', $_REQUEST['categoryid']);
                 if ($errors = $this->_getErrors()) {
                     $this->errors[$rowNum] = $errors;
                 } else {
                     foreach ($_REQUEST['congregationids'] as $congid) {
                         $comp->addCongregation($congid);
                     }
                     $comp->create();
                 }
             }
             $rowNum++;
         }
         if (empty($this->errors)) {
             $GLOBALS['system']->doTransaction('COMMIT');
             add_message($rowNum - 1 . ' rows imported successfully');
             redirect('services__component_library');
             // exits
         } else {
             add_message("Errors were found in the CSV file.  Import has not been performed.  Please correct the errors and try again", 'error');
             $GLOBALS['system']->doTransaction('ROLLBACK');
         }
         fclose($fp);
     }
 }
    function printView()
    {
        $congs = $GLOBALS['system']->getDBObjectData('congregation', array('!meeting_time' => ''), 'AND', 'name');
        if (empty($congs)) {
            print_message("To edit services you must first go to admin > congregations and set the 'code name' for the relevant congregations", 'failure');
            return;
        }
        ?>
		<form class="well well-small form-inline" style="line-height: 35px">
			<input type="hidden" name="view" value="<?php 
        echo ents($_REQUEST['view']);
        ?>
" />
			Show components containing
			<input type="text" name="search" placeholder="Enter search terms" value="<?php 
        echo ents(array_get($_REQUEST, 'search'));
        ?>
">
			<span style="white-space: nowrap">
				tagged with&nbsp;
				<?php 
        print_widget('tagid', array('type' => 'reference', 'references' => 'service_component_tag', 'allow_empty' => TRUE, 'empty_text' => '-- Choose Tag --'), array_get($_REQUEST, 'tagid'));
        ?>
			</span>
			 used by 
			<?php 
        $options = array('' => 'Any Congregation');
        foreach ($congs as $id => $cong) {
            $options[$id] = $cong['name'];
        }
        $options['-'] = 'No congregation';
        print_widget('congregationid', array('type' => 'select', 'options' => $options), array_get($_REQUEST, 'congregationid'));
        ?>
			<button type="submit" class="btn">Go</button>
			<a href="?view=<?php 
        echo ents($_REQUEST['view']);
        ?>
" class="btn">Clear</a>
		</form>

		<div class="row-fluid">
			<div class="span6" id="service-comp-manager">
				<?php 
        $cats = $GLOBALS['system']->getDBObjectdata('service_component_category');
        $congRestriction = array();
        if (!empty($_REQUEST['congregationid'])) {
            $congRestriction['congregationid'] = (int) $_REQUEST['congregationid'];
        }
        ?>
				<ul class="nav nav-tabs">
					<?php 
        $c = ' class="active"';
        foreach ($cats as $catid => $cat) {
            ?>
						<li<?php 
            echo $c;
            ?>
><a data-toggle="tab" href="#cat<?php 
            echo $catid;
            ?>
"><?php 
            echo ents($cat['category_name']);
            ?>
</a></li>
						<?php 
            $c = '';
        }
        ?>
				</ul>
				<div class="tab-content anchor-bottom">
					<?php 
        $c = ' active';
        foreach ($cats as $catid => $cat) {
            ?>
						<div class="tab-pane<?php 
            echo $c;
            ?>
" id="cat<?php 
            echo $catid;
            ?>
">
							<p class="pull-right">
								<a href="?view=_import_service_components&categoryid=<?php 
            echo (int) $catid;
            ?>
"><i class="icon-upload"></i>Import</a> &nbsp;
								<a href="?view=_add_service_component&categoryid=<?php 
            echo (int) $catid;
            ?>
"><i class="icon-plus-sign"></i>Add</a>
							</p>

							<table style="width: 100%;" class="table table-bordered service-comps clickable-rows">
								<thead>
									<tr>
										<th>Title</th>
									<?php 
            if (empty($_REQUEST['congregationid'])) {
                ?>
										<th>Used By</th>
										<?php 
            }
            ?>
									</tr>
								</thead>
								<tbody>
								<?php 
            $GLOBALS['system']->includeDBClass('service_component');
            $comps = Service_Component::search(array_get($_REQUEST, 'search'), array_get($_REQUEST, 'tagid'), array_get($_REQUEST, 'congregationid'), $catid);
            foreach ($comps as $compid => $comp) {
                ?>
									<tr data-id="<?php 
                echo (int) $compid;
                ?>
">
										<td>
											<?php 
                echo ents($comp['title']);
                if ($comp['alt_title']) {
                    echo ' <span class="alt-title">' . ents($comp['alt_title']) . '</span>';
                }
                ?>
										</td>
									<?php 
                if (empty($_REQUEST['congregationid'])) {
                    ?>
										<td><?php 
                    echo ents($comp['congregations']);
                    ?>
</td>
										<?php 
                }
                ?>
									</tr>
									<?php 
            }
            ?>
								</tbody>
							</table>
						</div>
						<?php 
            $c = '';
        }
        ?>
				</div>
			</div>
			
			<div class="span6 well well-small preview-pane anchor-bottom" id="preview">
				<p class="center"><i><br /><br />Select a component to view its details here</i></p>
			</div>
			
			<script>
				
				$(document).ready(function() {
					$('.service-comps tr').click(function() {
						$('#selected').attr('id', '');
						$(this).attr('id', 'selected');
						$('#preview').load("?call=service_comp_detail&id="+$(this).attr('data-id'));
					})
				})
			</script>

			<?php 
    }
 function run()
 {
     $GLOBALS['system']->includeDBClass('service_component');
     $results = Service_Component::search(array_get($_REQUEST, 'search'), array_get($_REQUEST, 'tagid'), array_get($_REQUEST, 'congregationid'));
     echo '[' . implode(',', array_keys($results)) . ']';
 }