public function testSetGetReplaceDeleteCycle()
 {
     $vm_ret = CompositeCache::save('foo', array('foo' => 'bar'));
     $this->assertTrue($vm_ret, 'Setting item in cache should return true');
     $vm_ret = CompositeCache::contains('foo');
     $this->assertTrue($vm_ret, 'Checking for existence of a key we just set should return true');
     $vm_ret = CompositeCache::fetch('foo');
     $this->assertArrayHasKey('foo', $vm_ret, 'Returned array should have key');
     $this->assertEquals(array('foo' => 'bar'), $vm_ret, 'Cache item should not change');
     $vm_ret = CompositeCache::save('foo', array('bar' => 'foo'));
     $this->assertTrue($vm_ret, 'Replacing item in cache should return true');
     $vm_ret = CompositeCache::fetch('foo');
     $this->assertArrayHasKey('bar', $vm_ret, 'Returned array should have key');
     $this->assertArrayNotHasKey('foo', $vm_ret, 'Returned array should not have replaced key');
     $vm_ret = CompositeCache::delete('foo');
     $this->assertTrue($vm_ret, 'Removing an existing key should return true');
     $vm_ret = CompositeCache::fetch('foo');
     $this->assertFalse($vm_ret, 'Should not return anything after deleting');
     $vm_ret = CompositeCache::contains('foo');
     $this->assertFalse($vm_ret, 'Should not return anything after deleting');
 }
 public function Save($pa_values = null)
 {
     $t_element = $this->getElementObject(false);
     $t_element->setMode(ACCESS_WRITE);
     $va_request = $_REQUEST;
     /* we don't want to modify $_REQUEST since this may cause ugly side-effects */
     foreach ($t_element->getFormFields() as $vs_f => $va_field_info) {
         $t_element->set($vs_f, $_REQUEST[$vs_f]);
         unset($va_request[$vs_f]);
         if ($t_element->numErrors()) {
             foreach ($t_element->errors() as $o_e) {
                 $this->request->addActionError($o_e, 'general');
                 $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__);
             }
         }
     }
     if ($vn_parent_id = $this->request->getParameter('parent_id', pInteger)) {
         $t_element->set('parent_id', $vn_parent_id);
     }
     if (!$t_element->getPrimaryKey()) {
         $vb_new = true;
         $vo_db = $t_element->getDb();
         if ($vn_parent_id) {
             $qr_tmp = $vo_db->query("\n\t\t\t\t\tSELECT MAX(rank) AS rank\n\t\t\t\t\tFROM ca_metadata_elements\n\t\t\t\t\tWHERE parent_id=?\n\t\t\t\t", $vn_parent_id);
             if (!$qr_tmp->nextRow()) {
                 $t_element->set('rank', 1);
             } else {
                 $t_element->set('rank', intval($qr_tmp->get('rank')) + 1);
             }
         }
         $t_element->insert();
         $vs_message = _t("Added metadata element");
         $this->request->setParameter('element_id', $t_element->getPrimaryKey());
     } else {
         $t_element->update();
         $vb_new = false;
         $vs_message = _t("Saved changes to metadata element");
     }
     if ($t_element->numErrors()) {
         foreach ($t_element->errors() as $o_e) {
             $this->request->addActionError($o_e, 'general');
             $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__);
         }
     } else {
         $this->notification->addNotification($vs_message, __NOTIFICATION_TYPE_INFO__);
     }
     if ($t_element->getPrimaryKey()) {
         $va_new_labels = array();
         $va_old_labels = array();
         $va_delete_labels = array();
         foreach ($va_request as $vs_key => $vs_val) {
             if (!(strpos($vs_key, 'element_labels_Pref') === false)) {
                 /* label field */
                 $va_matches = array();
                 if (!(strpos($vs_key, '_new') === false)) {
                     /* new label field */
                     preg_match('/element_labels_Pref(.*)_new_([0-9]+)/', $vs_key, $va_matches);
                     $va_new_labels[$va_matches[2]][$va_matches[1]] = $vs_val;
                 } else {
                     if (!(strpos($vs_key, '_delete') === false)) {
                         /* delete label */
                         preg_match('/element_labels_PrefLabel_([0-9]+)_delete/', $vs_key, $va_matches);
                         $va_delete_labels[] = $va_matches[1];
                     } else {
                         /* existing label field */
                         preg_match('/element_labels_Pref(.*)_([0-9]+)/', $vs_key, $va_matches);
                         $va_old_labels[$va_matches[2]][$va_matches[1]] = $vs_val;
                     }
                 }
                 unset($va_request[$vs_key]);
             }
         }
         /* insert new labels */
         $t_element_label = new ca_metadata_element_labels();
         foreach ($va_new_labels as $va_label) {
             $t_element_label->clear();
             foreach ($va_label as $vs_f => $vs_val) {
                 $t_element_label->set($vs_f, $vs_val);
             }
             $t_element_label->set('element_id', $t_element->getPrimaryKey());
             $t_element_label->setMode(ACCESS_WRITE);
             $t_element_label->insert();
             if ($t_element_label->numErrors()) {
                 foreach ($t_element_label->errors() as $o_e) {
                     $this->request->addActionError($o_e, 'general');
                     $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__);
                 }
             }
         }
         /* delete labels */
         foreach ($va_delete_labels as $vn_label) {
             $t_element_label->load($vn_label);
             $t_element_label->setMode(ACCESS_WRITE);
             $t_element_label->delete(false);
         }
         /* process old labels */
         foreach ($va_old_labels as $vn_key => $va_label) {
             $t_element_label->load($vn_key);
             foreach ($va_label as $vs_f => $vs_val) {
                 $t_element_label->set($vs_f, $vs_val);
             }
             $t_element_label->set('element_id', $t_element->getPrimaryKey());
             $t_element_label->setMode(ACCESS_WRITE);
             if ($vb_new) {
                 $t_element_label->insert();
             } else {
                 $t_element_label->update();
             }
             if ($t_element_label->numErrors()) {
                 foreach ($t_element_label->errors() as $o_e) {
                     $this->request->addActionError($o_e, 'general');
                     $this->notification->addNotification($o_e->getErrorDescription(), __NOTIFICATION_TYPE_ERROR__);
                 }
             }
         }
         /* process settings */
         if (is_array($va_settings = $t_element->getAvailableSettings())) {
             $vb_need_to_update = false;
             foreach ($va_settings as $vs_setting_key => $va_setting_info) {
                 if (isset($va_setting_info['refreshOnChange']) && (bool) $va_setting_info['refreshOnChange']) {
                     $t_element->setSetting($vs_setting_key, $va_request['setting_' . $vs_setting_key]);
                     $vb_need_to_update = true;
                 }
             }
             if ($vb_need_to_update) {
                 $t_element->update();
                 $va_settings = $t_element->getAvailableSettings();
             }
             // we need to unset the form timestamp to disable the 'Changes have been made since you loaded this data' warning
             // when we update() below. the warning makes sense because an update() is called before we get here, but if there
             // was an actual concurrent save problem , that very update above would have triggered the warning already
             $vn_timestamp = $_REQUEST['form_timestamp'];
             unset($_REQUEST['form_timestamp']);
             foreach ($va_settings as $vs_setting_key => $va_setting_info) {
                 if (isset($va_request['setting_' . $vs_setting_key . '[]'])) {
                     $vs_val = $va_request['setting_' . $vs_setting_key . '[]'];
                 } else {
                     $vs_val = $va_request['setting_' . $vs_setting_key];
                 }
                 $vs_error = null;
                 if (!$t_element->setSetting($vs_setting_key, $vs_val, $vs_error)) {
                     $this->notification->addNotification(_t("Setting %2 is not valid: %1", $vs_error, $vs_setting_key), __NOTIFICATION_TYPE_ERROR__);
                     continue;
                 }
                 $t_element->update();
             }
             $_REQUEST['form_timestamp'] = $vn_timestamp;
         }
         /* process type restrictions */
         $t_restriction = new ca_metadata_type_restrictions(null, true);
         $va_settings = array_keys($t_restriction->getAvailableSettings());
         foreach ($_REQUEST as $vs_key => $vs_value) {
             if (preg_match('!^type_restrictions_table_num_([\\d]+)$!', $vs_key, $va_matches)) {
                 // got one to update
                 if ($t_restriction->load($va_matches[1])) {
                     $t_restriction->setMode(ACCESS_WRITE);
                     $t_restriction->set('table_num', $this->request->getParameter('type_restrictions_table_num_' . $va_matches[1], pInteger));
                     $t_restriction->set('type_id', ($vn_type_id = $this->request->getParameter('type_restrictions_type_id_' . $va_matches[1], pInteger)) ? $vn_type_id : null);
                     $t_restriction->set('include_subtypes', ($vn_include_subtypes = $this->request->getParameter('type_restrictions_include_subtypes_' . $va_matches[1], pInteger)) ? $vn_include_subtypes : null);
                     foreach ($va_settings as $vs_setting) {
                         $t_restriction->setSetting($vs_setting, $this->request->getParameter('type_restrictions_setting_' . $vs_setting . '_' . $va_matches[1], pString));
                     }
                     $t_restriction->update();
                 }
                 continue;
             }
             if (preg_match('!^type_restrictions_table_num_new_([\\d]+)$!', $vs_key, $va_matches)) {
                 // got one to create
                 $t_restriction->setMode(ACCESS_WRITE);
                 $t_restriction->set('element_id', $t_element->getPrimaryKey());
                 $t_restriction->set('table_num', $this->request->getParameter('type_restrictions_table_num_new_' . $va_matches[1], pInteger));
                 $t_restriction->set('type_id', ($vn_type_id = $this->request->getParameter('type_restrictions_type_id_new_' . $va_matches[1], pInteger)) ? $vn_type_id : null);
                 $t_restriction->set('include_subtypes', ($vn_include_subtypes = $this->request->getParameter('type_restrictions_include_subtypes_new_' . $va_matches[1], pInteger)) ? $vn_include_subtypes : null);
                 foreach ($va_settings as $vs_setting) {
                     $t_restriction->setSetting($vs_setting, $this->request->getParameter('type_restrictions_setting_' . $vs_setting . '_new_' . $va_matches[1], pString));
                 }
                 $t_restriction->insert();
                 continue;
             }
             if (preg_match('!^type_restrictions_([\\d]+)_delete$!', $vs_key, $va_matches)) {
                 // got one to delete
                 if ($t_restriction->load($va_matches[1])) {
                     $t_restriction->setMode(ACCESS_WRITE);
                     $t_restriction->delete();
                 }
                 continue;
             }
         }
         CompositeCache::delete($t_element->getPrimaryKey(), 'ElementSets');
         CompositeCache::delete($t_element->getPrimaryKey(), 'ElementSetIds');
     }
     $this->Edit();
     return;
 }
Beispiel #3
0
 /**
  * Remove this result from cache
  * @return bool
  */
 public function remove()
 {
     $this->opa_search = array();
     $vm_ret = CompositeCache::delete($this->ops_cache_key, 'SearchCache');
     $this->ops_cache_key = null;
     return $vm_ret;
 }
 /**
  * Helper to flush the specific possible cache keys for the getLocaleList function below. We don't want
  * to flush the whole cache because that would nuke the session too. After the initial setup, locales change
  * very, very rarely anyway.
  */
 private function flushLocaleListCache()
 {
     foreach ($this->getFields() as $vs_field) {
         foreach (array('asc', 'desc') as $vs_sort_direction) {
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/0/0/0", 'LocaleList');
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/0/0/1", 'LocaleList');
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/0/1/0", 'LocaleList');
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/0/1/1", 'LocaleList');
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/1/0/0", 'LocaleList');
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/1/0/1", 'LocaleList');
             CompositeCache::delete("{$vs_field}/{$vs_sort_direction}/1/1/1", 'LocaleList');
         }
     }
 }
 /**
  * Flushes the element set cache for current record, its parent and the whole element set
  */
 private function flushElementSetCache()
 {
     if (!$this->getPrimaryKey()) {
         return;
     }
     if ($vn_parent_id = $this->get('parent_id')) {
         CompositeCache::delete($vn_parent_id, 'ElementSetIds');
         CompositeCache::delete($vn_parent_id, 'ElementSets');
     }
     if ($vn_hier_element_id = $this->get('hier_element_id')) {
         CompositeCache::delete($vn_hier_element_id, 'ElementSetIds');
         CompositeCache::delete($vn_hier_element_id, 'ElementSets');
     }
     CompositeCache::delete($this->getPrimaryKey(), 'ElementSetIds');
     CompositeCache::delete($this->getPrimaryKey(), 'ElementSets');
 }