Ejemplo n.º 1
0
 /**
  * Merge SQL
  */
 public static function merge($table, $table_dependent, array $ids, $id_primary)
 {
     $topicTable = new EditTable(array('name' => $table));
     $dependTable = new EditTable(array('name' => $table_dependent));
     $depend_col = "{$table}_id";
     // remove primary id from list of ids
     foreach ($ids as $key => $id) {
         if ($id == $id_primary) {
             unset($ids[$key]);
         }
     }
     $ids_sql = implode(',', $ids);
     // update dependent table
     $dependTable->update(array($depend_col => $id_primary), "{$depend_col} IN ({$ids_sql})");
     // delete from option table
     $topicTable->delete("id IN ({$ids_sql})");
 }
 public function execute()
 {
     $params = $this->_getAllParams();
     if (isset($params['merge']) && $this->allowMerge) {
         $this->merge();
         return;
     }
     if (isset($params['default']) && $this->allowDefault) {
         $this->setDefault();
     }
     if (isset($params['redirect']) and $params['redirect']) {
         // editTable is part of a "wizard" (redirect to the next step)
         header("Location: {$params['redirect']}");
         exit;
     } elseif (isset($params['saveonly'])) {
         $status = ValidationContainer::instance();
         $status->setStatusMessage('Your settings have been updated.');
     }
     require_once 'models/table/EditTable.php';
     $editTable = new EditTable(array('name' => $this->table));
     $request = $this->controller->getRequest();
     $validateOnly = $request->isXmlHttpRequest();
     // Delete, insert, or update?
     if ($validateOnly) {
         //$id = $params['id'];
         $id = isset($_POST['id']) && is_numeric($_POST['id']) ? $_POST['id'] : null;
         // Get field to update
         foreach ($this->fields as $key => $devnull) {
             if (isset($params[$key])) {
                 $fieldEdit = $key;
                 $fieldValue = $params[$key];
                 break;
             }
         }
         if ($id == 0 && isset($params['undelete'])) {
             // undelete record
             try {
                 $row = $editTable->undelete($fieldEdit, $fieldValue);
                 $sendRay['insert'] = $row->id;
                 $sendRay['undelete'] = $row->{$fieldEdit};
                 $this->sendData($sendRay);
             } catch (Zend_Exception $e) {
                 $this->sendData(array("insert" => 0, 'error' => $e->getMessage()));
             }
         } elseif ($id == 0) {
             // user added new record
             try {
                 if (!$this->insertExtra) {
                     $insert = $editTable->insertUnique($fieldEdit, $fieldValue);
                 } else {
                     $data = array($fieldEdit => $fieldValue);
                     $insert = $editTable->insert(array_merge($data, $this->insertExtra));
                 }
                 $sendRay['insert'] = "{$insert}";
                 if ($insert == -1) {
                     $sendRay['error'] = 'A record already exists with this value.';
                 }
                 if ($insert == -2) {
                     $sendRay['error'] = '"%s" already exists, but was deleted.  Would you like to undelete?';
                 }
                 $this->sendData($sendRay);
             } catch (Zend_Exception $e) {
                 $this->sendData(array("insert" => 0, 'error' => $e->getMessage()));
             }
         } elseif ($id > 0) {
             // update or delete
             if (isset($params['delete'])) {
                 try {
                     $delete = $editTable->delete("id={$id}", true);
                     //force the delete, changed 06/16/08 Todd W
                     $this->sendData(array("delete" => $delete));
                 } catch (Zend_Exception $e) {
                     $this->sendData(array("delete" => 0, 'error' => $e->getMessage()));
                 }
             } elseif (isset($fieldEdit)) {
                 // update
                 try {
                     $update = $editTable->update(array($fieldEdit => $fieldValue), "id={$id}");
                     $this->sendData(array("update" => $id));
                 } catch (Zend_Exception $e) {
                     if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
                         $this->sendData(array("update" => 0, 'error' => t('A record already exists with this value.')));
                     } else {
                         $this->sendData(array("update" => 0, 'error' => $e->getMessage()));
                     }
                 }
             }
         }
     } else {
         // view
         $selectFields = array_keys($this->fields);
         if ($this->allowDefault) {
             $selectFields[] = 'is_default';
         }
         require_once 'views/helpers/EditTableHelper.php';
         $rowRay = $editTable->getRowsSingle($this->table, $selectFields, $this->where);
         foreach ($rowRay as $key => $row) {
             foreach ($selectFields as $field) {
                 if ($field != 'id') {
                     $rowRay[$key][$field] = htmlspecialchars($row[$field]);
                 }
             }
         }
         // Modify rows
         if ($this->rowHook) {
             $func_name = $this->rowHook;
             eval('$rowRay = ' . $func_name . "(unserialize('" . serialize($rowRay) . "'));");
         }
         $noDelete = array();
         // look up dependencies
         if (!empty($this->dependencies)) {
             foreach ($this->dependencies as $colDependent => $tableDependent) {
                 if (is_numeric($colDependent)) {
                     $colDependent = $this->table . '_id';
                 }
                 if (is_array($tableDependent)) {
                     // in case multiple tables use the same field name
                     $colDependent = key($tableDependent);
                     $tableDependent = current($tableDependent);
                 }
                 $ray = $editTable->getDependencies($this->table, $tableDependent, $colDependent);
                 $noDelete += array_merge($noDelete, $ray);
             }
             $noDelete = array_unique($noDelete);
         }
         // disable delete on all rows
         if ($this->noDelete) {
             foreach ($rowRay as $key => $row) {
                 $noDelete[$row['id']] = $row['id'];
             }
         }
         // merge checkbox
         if ($this->allowMerge) {
             foreach ($rowRay as $key => $row) {
                 $rowRay[$key]['merge'] = '
       <input type="checkbox" name="merge[]" value="' . $row['id'] . '" id="merge' . $row['id'] . '">';
             }
             $this->customColDef['merge'] = 'editor:false';
             $this->fields['merge'] = 'Merge?';
         }
         // default radio
         if ($this->allowDefault) {
             foreach ($rowRay as $key => $row) {
                 $isChecked = $row['is_default'] ? ' checked="checked"' : '';
                 $rowRay[$key]['default'] = '
       <input type="radio" name="default" value="' . $row['id'] . '" id="merge' . $row['id'] . '"' . $isChecked . '>';
             }
             $this->customColDef['default'] = 'editor:false';
             $this->fields['default'] = 'Default?';
         }
         $html = '';
         if ($this->allowMerge) {
             $mergehtml = '
     <input type="hidden" name="table_option" value="' . $this->table . '">
     <input type="hidden" name="table_dependent" value="' . implode(',', $this->dependencies) . '">
     <input type="submit" name="mergesubmit" value="Merge Selected" class="submitArrow">';
             $html .= $mergehtml;
         }
         if ($html) {
             $html .= '<div class="clear"></div><br>';
         }
         $html .= EditTableHelper::generateHtml($this->label, $rowRay, $this->fields, $this->customColDef, $noDelete, $this->noEdit);
         // merge form
         if ($this->allowMerge) {
             $html .= $mergehtml;
         }
         $this->controller->view->assign($this->viewVar, $html);
     }
 }