Example #1
0
 public function select_items()
 {
     $flash = Flash::Instance();
     $s_data = array();
     $params['options']['parent_id'] = $this->_templateobject->getDisplayFieldNames();
     // so set context from calling module
     if (isset($this->_data['parent_id'])) {
         $s_data['parent_id'] = $this->_data['parent_id'];
     } elseif (!isset($this->_data['Search']['parent_id'])) {
         $s_data['parent_id'] = NULL;
     }
     $params['type'] = $this->module;
     parent::setSearch('SelectorItemSearch', 'useDefault', $s_data, $params);
     // load the default display fields into the session, we need these as we cannot rely on getting the headings from the data itself
     $_SESSION['selected_items']['headings'] = $this->_templateobject->getDisplayFieldNames();
     $this->_templateobject->idField = 'id';
     $this->_templateobject->orderby = $this->itemFields;
     $collection = new SelectorCollection($this->_templateobject);
     $collection->setTableName($collection->setOverview());
     $sh = $this->setSearchHandler($collection);
     if (!isset($this->_data['orderby']) && !isset($this->_data['page'])) {
         $sh->addConstraint(SelectorCollection::getItemHierarchy($this->module, $this->search->getValue('parent_id')));
     }
     // get list of items matching search criteria
     parent::index($collection, $sh);
     // construct and set link
     foreach ($this->_modules as $key => $value) {
         $modules[] = $key . '=' . $value;
     }
     $link = implode('&', $modules) . '&controller=' . $this->name . '&action=selected_items';
     $this->view->set('link', $link);
     // get the list of selected targets
     // - if target supplied on input, use that
     // - otherwise use the saved session
     if (isset($this->_data['target_id']) && !isset($this->_data['page'])) {
         $this->_data['selected_targets'][] = $this->_data['target_id'];
         $_SESSION['selected_items']['data'] = $_SESSION['selected_targets']['data'] = $selected_targets = '';
     } else {
         $selected_targets = empty($_SESSION['selected_targets']['data']) ? array() : $_SESSION['selected_targets']['data'];
     }
     if (empty($selected_targets)) {
         if (!isset($this->_data['selected_targets']) || count($this->_data['selected_targets']) <= 0) {
             // no target set on input or in saved session - so start from scratch!
             $selected_targets = $_SESSION['selected_targets']['data'] = array();
         } else {
             // target must have been set on input so get the target details and save to session
             $_SESSION['selected_targets'] = array();
             $selected_target_name = $this->targetModel . 'Collection';
             $target = new $this->targetModel();
             $idField = $target->idField;
             $targets = new $selected_target_name($target);
             $sh = new SearchHandler($targets);
             $sh->addConstraint(new Constraint($idField, 'IN', '(' . implode(',', $this->_data['selected_targets']) . ')'));
             $rows = $targets->load($sh, null, RETURN_ROWS);
             foreach ($rows as $data) {
                 $selected_targets[$data[$idField]] = $data;
             }
             $_SESSION['selected_targets']['data'] = $selected_targets;
             $target_headings = $target->getDisplayFieldNames();
             foreach ($this->targetFields as $fieldname) {
                 $selected_target_headings[$fieldname] = $target_headings[$fieldname];
             }
             $_SESSION['selected_targets']['headings'] = $selected_target_headings;
         }
     }
     $this->view->set('selected_targets', $selected_targets);
     $this->view->set('current_targets_headings', $_SESSION['selected_targets']['headings']);
     // get list of selected items
     if (!empty($selected_targets) && empty($_SESSION['selected_items']['data'])) {
         // targets are defined but no items, so get the items associated with the targets
         $item_link = new DataObject($this->linkTableName);
         $item_link->idField = 'item_id';
         $cc = new ConstraintChain();
         $cc->add(new Constraint('target_id', 'IN', '(' . implode(',', array_keys($selected_targets)) . ')'));
         $item_ids = $item_link->getAll($cc);
         $item = new SelectorObject($this->itemTableName);
         $item->setDefaultDisplayFields($this->itemFields);
         $item->idField = 'id';
         $item->orderby = $this->itemFields;
         $items = new SelectorCollection($item);
         $items->setTableName($items->setOverview());
         $sh = new SearchHandler($items);
         if (count($item_ids) > 0) {
             $sh->addConstraint(new Constraint($item->idField, 'IN', '(' . implode(',', array_keys($item_ids)) . ')'));
         } else {
             $sh->addConstraint(new Constraint($item->idField, '=', -1));
         }
         $rows = $items->load($sh, null, RETURN_ROWS);
         if (count($rows) > 0) {
             foreach ($rows as $data) {
                 $_SESSION['selected_items']['data'][$data['id']] = $data;
             }
         } else {
             $_SESSION['selected_items']['data'] = array();
         }
     }
     // get and set variables
     $selected_items = $_SESSION['selected_items']['data'];
     $selected_item_headings = empty($_SESSION['selected_items']['headings']) ? array() : $_SESSION['selected_items']['headings'];
     $this->view->set('selected_items', $selected_items);
     $this->view->set('selected_item_headings', $selected_item_headings);
     $this->view->set('selected_item_headings_count', count($selected_item_headings) + 1);
     $this->view->set('title', $this->title);
     $this->view->set('page_title', $this->getPageName('Select Items'));
     $this->printaction = '';
 }