예제 #1
0
 function editItem($noDelete = false)
 {
     /* here REQUEST is fine */
     if (!isset($_REQUEST['page'])) {
         $page = 0;
     } else {
         if ($this->active) {
             $page = $_REQUEST['page'];
         } else {
             $page = 1;
         }
     }
     switch ($page) {
         case 0:
             $entity = $this->entity;
             if (!isset($this->pager)) {
                 /* the pager is going to instantiate the template object as well */
                 $this->pager = new beContentPager();
             }
             if (!isset($_REQUEST['mode']) or $_REQUEST['mode'] != AJAX) {
                 /*  
                 					
                 						FORM - EDIT ITEM - PAGE 0 
                 					
                 						The following is executed as the first step in the editItem() procedure.
                 */
                 $this->editItem_preSelection();
                 if (isset($_REQUEST['msgCode'])) {
                     $GLOBALS['main']->setContent("message", $GLOBALS['message']->getMessage($_REQUEST['msgCode']));
                 }
                 $body = new Template("{$this->reportTemplate}");
                 $body->setContent("page", 1);
                 $body->setContent("item", $this->entity->name, "length=\"{$this->pager->length}\"");
                 $content = "<div id=\"becontent\">\n\n" . $body->get() . "\n</div>\n";
                 $this->editItem_postSelection();
             } else {
                 /* 	
                 						22.01.2008
                 	FORM - EDIT ITEM - PAGE 0 - AJAX:
                 					
                 						The following is executed by an AJAX request, it is intended to 
                 						replace the the externa ajax-report.php script, which caused lots
                 						of loosy coupling with the rest.
                 	It is invoked by the code which is generated by 
                 		FORM - EDIT FORM - PAGE 0 (ie the previous sub-step)
                 */
                 /* 
                 					
                 						Warning: in the following stripslashes is necessary because of the 
                 						folloing directive in the PHP.INI file
                 		magic_quotes_sybase = Off
                 							
                 						it should not cause problems also in the case is On
                 */
                 $form = unserialize(stripslashes($_REQUEST['form']));
                 if ($form['update'] != "") {
                     /* Look for eventual checkboxes (and others) which have clicked */
                     foreach ($this->entity->fields as $k => $v) {
                         if (array_key_exists($v['name'], $form)) {
                             $fields[] = $this->entity->fields[$k]['name'];
                         }
                     }
                     $query = "UPDATE {$this->entity->name} SET ";
                     if (is_array($fields)) {
                         foreach ($fields as $field) {
                             $query .= aux::first_comma("UPDATE AJAX", ", ") . "{$field} = '{$form[$field]}'";
                         }
                     }
                     $query .= " WHERE {$this->entity->fields[0]['name']} = '{$form['value']}'";
                     $oid = mysql_query($query);
                 }
                 $_REQUEST['pagelength'] = $this->pager->length;
                 $startIndex = ($_REQUEST['currentpage'] - 1) * $_REQUEST['pagelength'];
                 if ($startIndex < 0) {
                     $startIndex = 1;
                 }
                 $length = $_REQUEST['pagelength'];
                 if ($_REQUEST['currentpage'] * $_REQUEST['pagelength'] > $_REQUEST['totallength']) {
                     $length = $_REQUEST['pagelength'] - ($_REQUEST['currentpage'] * $_REQUEST['pagelength'] - $_REQUEST['totallength']);
                 }
                 $endIndex = $startIndex + $length;
                 if ($this->pager->query != "") {
                     if ($form['operation'] == "search" and $form['search'] != "") {
                         $query = aux::refineQuery($this->pager->query, aux::evaluate($this->pager->filter, $form));
                         $startIndex = ($_REQUEST['currentpage'] - 1) * $_REQUEST['pagelength'];
                         $length = $_REQUEST['pagelength'];
                         if ($_REQUEST['currentpage'] * $_REQUEST['pagelength'] > $_REQUEST['totallength']) {
                             $length = $_REQUEST['pagelength'] - ($_REQUEST['currentpage'] * $_REQUEST['pagelength'] - $_REQUEST['totallength']);
                         }
                         $endIndex = $startIndex + $length;
                     } else {
                         $query = aux::getResult($this->pager->getQuery(), PARSE);
                     }
                     $oid = mysql_query($query);
                     $_REQUEST['totallength'] = mysql_num_rows($oid);
                     if ($endIndex > $_REQUEST['totallength']) {
                         $endIndex = $_REQUEST['totallength'];
                     }
                     $query .= " LIMIT {$startIndex}, {$length}";
                     $data = aux::getResult($query);
                 } else {
                     if ($form['operation'] == "search" and $form['search'] != "") {
                         $condition = "";
                         foreach ($this->entity->presentation as $v) {
                             $condition .= aux::first_comma("condition", " OR ");
                             $condition .= " {$v} LIKE '%{$form['search']}%' ";
                         }
                         if ($this->entity->owner) {
                             $condition .= aux::first_comma("condition", " OR ");
                             $condition .= " username LIKE '%{$form['search']}%' ";
                         }
                         $condition .= aux::first_comma("condition", " OR ");
                         $condition .= " {$this->entity->fields[0]['name']} LIKE '%{$form['search']}%' ";
                         $data = $entity->getReference(LIMIT, $startIndex, $length, $condition);
                     } else {
                         $data = $entity->getReference(LIMIT, $startIndex, $length);
                     }
                 }
                 $totalPages = ceil($_REQUEST['totallength'] / $_REQUEST['pagelength']);
                 if ($_REQUEST['totallength'] > 0) {
                     $content['content'] = $this->pager->get($data);
                     $content['content'] = ereg_replace("\n", "", $content['content']);
                 } else {
                     $empty = new Template("dtml/empty-report.html");
                     $content['content'] = ereg_replace("\n", "", $empty->get());
                 }
                 $content['startIndex'] = $startIndex + 1;
                 $content['endIndex'] = $endIndex;
                 $content['currentPage'] = $_REQUEST['currentpage'];
                 $content['totalPages'] = $totalPages . "";
                 $content['totallength'] = $_REQUEST['totallength'];
                 echo aux::AjaxEncode($content);
                 exit;
             }
             break;
         case 1:
             /* FORM FEED */
             $oid = mysql_query("SELECT * FROM {$this->entity->name}\n\t\t\t    \t                WHERE {$this->entity->fields[0]['name']}='{$_REQUEST['value']}'");
             if (!$oid) {
                 echo $GLOBALS['message']->getMessage(MSG_ERROR_DATABASE_GENERIC) . " (" . basename(__FILE__) . ":" . __LINE__ . ")";
                 exit;
             }
             $data = mysql_fetch_assoc($oid);
             /* here we have a problem with multiple forms:
              * 
              * the values to be fed into the form are coming from the db, ie
              * in case of multiple forms few values can be overwritten. 
              *  
              *  */
             foreach ($this->elements as $element) {
                 if (isset($data[$element['name']])) {
                     $_REQUEST[$element['name']] = $data[$element['name']];
                     if ($element['type'] == 'FILE' or $element['type'] == 'IMAGE') {
                         $_REQUEST[$element['name'] . "_type"] = $data[$element['name'] . "_type"];
                         $_REQUEST[$element['name'] . "_size"] = $data[$element['name'] . "_size"];
                         $_REQUEST[$element['name'] . "_filename"] = $data[$element['name'] . "_filename"];
                     }
                 }
             }
             $_REQUEST[$this->elements[0]['name']] = $data[$this->elements[0]['name']];
             $this->editItem_preFormFeed();
             /* the following is about N-M relations */
             if (count($this->triggeredForms) > 0 and $this->triggeredForms != "") {
                 foreach ($this->triggeredForms as $k => $form) {
                     switch ($form->elements[0]['orientation']) {
                         case RIGHT:
                             $query = "SELECT * \n\t\t\t\t\t\t\t            FROM {$form->entity->name}\n\t\t\t                \t\t   WHERE {$form->entity->fields[0]['name']}='{$_REQUEST['value']}'";
                             break;
                         case LEFT:
                             $query = "SELECT * \n\t\t\t\t\t\t\t          FROM {$form->entity->name}\n\t\t\t                   \t\t  WHERE {$form->entity->fields[1]['name']}='{$_REQUEST['value']}'";
                             break;
                     }
                     $oid = mysql_query($query);
                     if (!$oid) {
                         echo $GLOBALS['message']->getMessage(MSG_ERROR_DATABASE_GENERIC) . " (" . basename(__FILE__) . ":" . __LINE__ . ")";
                         exit;
                     }
                     do {
                         $data = mysql_fetch_array($oid);
                         if ($data) {
                             switch ($form->elements[0]['orientation']) {
                                 case RIGHT:
                                     $_REQUEST["{$form->elements[0]['name']}_" . aux::encode_name($data[1])] = $data[1];
                                     #$_REQUEST["{$form->elements[0]['name']}_{$data[1]}"] = $data[1];
                                     break;
                                 case LEFT:
                                     $_REQUEST["{$form->elements[0]['name']}_" . aux::encode_name($data[0])] = $data[0];
                                     #$_REQUEST["{$form->elements[0]['name']}_{$data[0]}"] = $data[0];
                                     break;
                             }
                         }
                     } while ($data);
                 }
             }
             $this->noDelete = $noDelete;
             $content = $this->display(EDIT, 2, PRELOAD);
             $this->editItem_postFormFeed();
             break;
         case 2:
             /* UPDATE */
             foreach ($this->elements as $k => $v) {
                 if ($v['type'] == CHECKBOX) {
                     $token = explode(":", $v['values'][1]);
                     if (!isset($_REQUEST[$token[1]])) {
                         $_REQUEST[$token[1]] = '';
                     }
                 }
             }
             $this->editItem_preUpdate();
             $entity = $this->entity;
             if (!$entity->editItem($this)) {
                 /* An error occourred */
                 echo $GLOBALS['message']->getMessage(MSG_ERROR_DATABASE_GENERIC) . " (" . basename(__FILE__) . ":" . __LINE__ . ")";
             } else {
                 /* Pivotal entity has been succesfully updated */
                 if (!isset($_REQUEST['value'])) {
                     $_REQUEST['value'] = "";
                 }
                 $query = "SELECT * FROM {$this->entity->name} WHERE {$this->entity->fields[0]['name']}='{$_REQUEST['value']}'";
                 $oid = mysql_query($query);
                 if (!$oid) {
                     echo $GLOBALS['message']->getMessage(MSG_ERROR_DATABASE_GENERIC) . " (" . basename(__FILE__) . ":" . __LINE__ . ")";
                     exit;
                 }
                 $data = mysql_fetch_assoc($oid);
                 if (count($data) > 0 and $data != "") {
                     foreach ($data as $k => $v) {
                         $_REQUEST[$k] = $v;
                     }
                 }
                 /* RELATION MANAGER MANAGEMENT BELOW */
                 if (count($this->triggeredForms) > 0 and $this->triggeredForms != "") {
                     foreach ($this->triggeredForms as $k => $form) {
                         switch ($form->elements[0]['orientation']) {
                             case RIGHT:
                                 $query = "DELETE FROM {$form->entity->name} WHERE {$form->entity->fields[0]['name']}='{$_REQUEST['value']}'";
                                 break;
                             case LEFT:
                                 $query = "DELETE FROM {$form->entity->name} WHERE {$form->entity->fields[1]['name']}='{$_REQUEST['value']}'";
                                 break;
                         }
                         $oid = mysql_query($query);
                         if (!$oid) {
                             echo $GLOBALS['message']->getMessage(MSG_ERROR_DATABASE_GENERIC) . " (" . basename(__FILE__) . ":" . __LINE__ . ")";
                             exit;
                         }
                         if ($this->entity->standardKey and isset($_REQUEST[$form->entity->entity_1->fields[0]['name']])) {
                             $_REQUEST[$form->entity->fields[0]['name']] = $_REQUEST[$form->entity->entity_1->fields[0]['name']];
                         }
                         foreach ($_REQUEST as $key2 => $value2) {
                             if (ereg("{$form->elements[0]['name']}_", $key2)) {
                                 switch ($form->elements[0]['orientation']) {
                                     case RIGHT:
                                         $query = "INSERT INTO {$form->entity->name} VALUES('{$_REQUEST[$form->entity->fields[0]['name']]}','{$_REQUEST[$key2]}')";
                                         break;
                                     case LEFT:
                                         $query = "INSERT INTO {$form->entity->name} VALUES('{$_REQUEST[$key2]}','{$_REQUEST['value']}')";
                                         break;
                                 }
                                 $oid = mysql_query($query);
                                 if (!$oid) {
                                     echo $GLOBALS['message']->getMessage(MSG_ERROR_DATABASE_RELATION_INSERT) . " (" . basename(__FILE__) . ":" . __LINE__ . ")";
                                     exit;
                                 }
                             }
                         }
                     }
                 }
                 $this->noDelete = $noDelete;
                 $content = $this->display(EDIT, 2, PRELOAD);
                 if (!$this->moderationMode) {
                     $GLOBALS['main']->setContent("message", $GLOBALS['message']->getMessage(NOTIFY_ITEM_UPDATED));
                 }
             }
             $this->editItem_postUpdate();
             break;
         case 3:
             /* DELETION */
             $this->editItem_preDeletion();
             if ($this->entity->deleteItem()) {
                 $msgCode = NOTIFY_ITEM_DELETED;
             } else {
                 $msgCode = NOTIFY_ITEM_INTEGRITY_VIOLATION;
             }
             $this->editItem_postDeletion();
             Header("Location: " . basename($_SERVER['SCRIPT_NAME']) . "?action=edit&page=0&msgCode={$msgCode}");
             exit;
             break;
     }
     return $content;
 }