Create taxonomy 'add terms' form into block - drupal-7

I have two vocabularies named 'vocab_one' and 'vocab_two'. I create custom multiple blocks module named 'add library', in order to display the taxonomy 'add terms' form to each blocks.
Each block displayed on the same node in the content area. The position of 'Vocab Two Block' are above 'Vocab One Block'.
When I add new term on 'Vocab One Block', the new term is always stored in the 'vocab_two'. Then I switch position of the block. This time, the new term is always stored in the 'vocab_one'.
How to store a new term within selected vocabulary, without worrying about the block position?
here is my add_library.module:
function add_library_block_info() {
$blocks['Vocab Two Block'] = array(
'info' => t('Vocab Two'),
);
$blocks['Vocab Two Block'] = array(
'info' => t('Vocab One'),
);
return $blocks;
}
function add_library_block_view($delta='') {
$block = array();
switch ($delta) {
case 'Vocab Two Block':
$block['subject'] = t('Add Vocab Two');
$block['content'] = add_vocab_two();
break;
case 'Vocab Two Block':
$block['subject'] = t('Add Vocab One');
$block['content'] = add_vocab_one();
break;
}
return $block;
}
function add_vocab_two() {
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
$vocabulary = taxonomy_vocabulary_load(2);
return drupal_get_form('taxonomy_form_term', $vocabulary);
}
function add_vocab_one() {
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
$vocabulary = taxonomy_vocabulary_load(4);
return drupal_get_form('taxonomy_form_term', $vocabulary);
}

Related

Wordpress React JS based template website not working after upgrading to PHP 8.1

After switching from PHP 7.4.30 to PHP 8.1, my WordPress website, which was built by the original developer using React JS, started acting strangely. The menus stopped working and the home page became frozen and immovable.
Enable the debug and below are some of them.
Deprecated: Optional parameter $menu declared before required parameter $location is implicitly treated as a required parameter in \app\public\wp-content\themes**\functions\classes\Settings\Menu.php on line 22
**
<?php
namespace ThemeClasses\Settings;
class Menu
{
public function __construct()
{
add_action('after_setup_theme', [$this, 'registerNavMenus']);
add_filter('getMenuTree', [$this, 'getMenuTree'], 10, 2);
}
public function registerNavMenus()
{
register_nav_menus([
'header_menu' => __('Header Menu', 'siri'),
'footer_menu' => __('Footer Menu', 'siri'),
'footer_columns' => __('Footer Columns', 'siri'),
]);
}
public function getMenuTree($menu = [], $location)
{
$flatMenu = $menu;
$flatMenu = $this->getMenuItems($menu, $location);
$treeMenu = [];
$itemsRefs = [];
foreach ($flatMenu as $menuItemObj) {
$itemId = $menuItemObj->ID;
$parentId = $menuItemObj->menu_item_parent;
$itemsRefs[$itemId] = [
'name' => $menuItemObj->title,
'url' => $menuItemObj->url,
'target' => $menuItemObj->target,
'active' => $menuItemObj->active,
'ID' => $itemId,
'parentId' => $parentId,
'children' => [],
];;
if ($parentId == 0) {
$treeMenu[] = &$itemsRefs[$itemId];
} elseif (isset($itemsRefs[$parentId])) {
$itemsRefs[$parentId]['children'][] = &$itemsRefs[$itemId];
}
}
return $treeMenu;
}
private function getMenuItems($menu, $location)
{
// Get all locations
$locations = get_nav_menu_locations();
// Get object id by location
$menuObject = wp_get_nav_menu_object($locations[$location]);
// Check menu exists
if (!is_object($menuObject)) return [];
// Get menu items by menu slug
$menu = wp_get_nav_menu_items($menuObject->slug);
// Return menu post objects
return $menu;
}
}
**Deprecated: parse_str(): Passing null to parameter #1 ($string) of type string is deprecated in public\wp-content\themes**r\functions\classes\WordPressSecurity.php on line 364
// add the filter
add_filter('wp_admin_css', function($url, $file) {
errol_log($url);
// make filter magic happen here...
return $url;
}, 10, 2 );
}
public function removeWPVersion($src)
{
global $wp_version;
parse_str(parse_url($src, PHP_URL_QUERY), $query);
if (!empty($query['ver']) && $query['ver'] === $wp_version) {
$src = remove_query_arg('ver', $src);
}
return $src;
}

Yii2 Save multiple data in the db using foreach loop in actionCreate

In my project I want to insert multiple rows of data at a single time using the foreach loop. I have a variable which has array of elements.
For instance if my array has say 3 different elements. I want to save all these 3 elements in the 3 different db table rows. I also have other columns which are same for all the 3 array elements.
I have put them inside foreach statement but only the 1st elements gets saved. Is there any method I can achieve this?
My code
public function actionCreate($prodID)
{
$model = new ProductlinesStorage();
if ($model->load(Yii::$app->request->post())) {
$productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all();
foreach ($productlineID as $singleProductlineID) {
$model->productline_id = $singleProductlineID->productline_id;
$model->user_id = Yii::$app->user->identity->user_id;
$model->isNewRecord = true;
$model->save();
}
return $this->redirect(['/product/storage?id='.$prodID]);
} else {
return $this->renderAjax('create', [
'model' => $model,
'prodID' => $prodID,
]);
}
}
Only the productline_id is different other columns will have same data for all the prdouctline_id.
Thank You!!!
You have only one model object, and you are saving only to it.
Try this:
public function actionCreate($prodID)
{
$model = new ProductlinesStorage();
if ($model->load(Yii::$app->request->post())) {
$productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all();
foreach ($productlineID as $singleProductlineID) {
$model = new ProductlinesStorage();
$model->productline_id = $singleProductlineID->productline_id;
$model->user_id = Yii::$app->user->identity->user_id;
$model->isNewRecord = true;
$model->save();
}
return $this->redirect(['/product/storage?id='.$prodID]);
} else {
return $this->renderAjax('create', [
'model' => $model,
'prodID' => $prodID,
]);
}
}
maybe you can modify my code
public function actionCreate()
{
$model = new SemesterPendek();
$model->user_id = \Yii::$app->user->identity->id;
$model->npm = \Yii::$app->user->identity->username;
$modelsNilai = [new Nilai];
if ($model->load(Yii::$app->request->post())){
$model->waktu_daftar = date('Y-m-d h:m:s');
$model->save();
$modelsNilai = Tabular::createMultiple(Nilai::classname());
Tabular::loadMultiple($modelsNilai, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Tabular::validateMultiple($modelsNilai) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsNilai as $indexTools =>$modelNilai) {
$modelNilai->id_sp = $model->id;
// $modelNilai->user_id = \Yii::$app->user->identity->id;
if (! ($flag = $modelNilai->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack(); \Yii::$app->session->setFlash('error','gagal');
}
}
} else {
return $this->render('create', [
'model' => $model,
'modelsNilai' => (empty($modelsNilai)) ? [new Nilai] : $modelsNilai,
]);
}
}
You need to create a different object to save in different rows. For loop executes 3 times but every time same object is being updated. You can define new object and save each time. Below code will work
public function actionCreate($prodID)
{
$model = new ProductlinesStorage();
if ($model->load(Yii::$app->request->post())) {
$productlineID = Productlines::find()->where(['area_id' => $model->productline_id, 'product_id' => $prodID])->all();
foreach ($productlineID as $singleProductlineID) {
$model = new ProductlinesStorage();
$model->productline_id = $singleProductlineID->productline_id;
$model->user_id = Yii::$app->user->identity->user_id;
$model->isNewRecord = true;
$model->save();
}
return $this->redirect(['/product/storage?id='.$prodID]);
} else {
return $this->renderAjax('create', [
'model' => $model,
'prodID' => $prodID,
]);
}
}

update one field of a table from another controller while save the data

I have a manage_returns_controller and it has two action,add and index.It has one product dropdown,one stock dropdown and add button.And I also have manage_products_controller that contains a field stock.But now I want to do the following
When I click on the returns controller's add button then simultaneously save the return data as well as increase the stock in the product table.
heres the add action code
function add($id = null)
{
/*$this->pageTitle = "Edit Exchange";*/
$storeval = $this->Store->find('all',array('conditions'=>array('is_deleted'=>0,'is_blocked'=>0)));
//$storeval=$this->Store->findByName();
$storevalas = array();
foreach($storeval as $storevall)
{
$storevalas[$storevall['Store']['id']] = $storevall['Store']['name'];
}
$this->set('stock_entry_option_store', $storevalas);
//$product_name = $this->Exchange->product->find('list');
$storevaldd = $this->Product->find('all',array('conditions'=>array('is_deleted'=>0,'is_blocked'=>0)));
//$storeval=$this->Store->findByName();
$product_name = array();
foreach($storevaldd as $storevall)
{
$product_name[$storevall['Product']['id']] = $storevall['Product']['name'];
}
$this->set('stock_entry_option_product',$product_name);
$this->Return->id = $id;
if(!empty($id)){
$button_name = "Update";
$msg_act = "updated";
$this->pageTitle = "Edit Return";
} else {
$button_name = "Add";
$msg_act = "added";
$this->pageTitle = "Add Return";
}
if (empty($this->data))
{
$this->data = $this->Return->read();
}
else
{
if ($this->Return->save($this->data))
{
$this->Session->setFlash(__('Return successfully '.$msg_act.'.',true),'default',array('class' => 'success'));
$this->redirect(array('controller'=>'ManageReturns','action' => 'index'));
}
}
$this->set('button_name',$button_name);
}

How to define a checkbox variable in Cakephp

I have a form in my Plugin elements and what i would like to insert the checkbox value into a table named it_queries and field status_type and its giving me an error Undefined variable: variableValue [APP\Plugin\Feedback\View\Elements\comment_add.ctp, line 37] .I have declared the variable in my controller like this
$this->set('variableValueStatus', 'Pending');
and this is line 37 thats giving me the error
Below is the Controller code
App::uses('FeedbackAppController', 'Feedback.Controller');
class CommentsController extends FeedbackAppController
{
public $components = array('Feedback.Comments');
public function add($foreign_model = null, $foreign_id = null)
{
if (empty($foreign_model) ||
empty($foreign_id) ||
!$this->request->is('post')
)
{
foreach ($_POST['likebutton'] as $pageId => $likeFlag) {
$dbFlag = $likeFlag ? 'Yes' : 'No';
}
return $this->redirect('/');
}
App::uses($foreign_model, 'Model');
$Model = ClassRegistry::init($foreign_model);
if (!($Model instanceof Model))
{
return $this->redirect('/');
}
if ($Model->hasAny(array($Model->primaryKey => $foreign_id)) == false)
{
return $this->redirect('/');
}
if (!isset($this->request->data['Comment']['foreign_model']) ||
!isset($this->request->data['Comment']['foreign_id']) ||
$this->request->data['Comment']['foreign_model'] != $foreign_model ||
$this->request->data['Comment']['foreign_id'] != $foreign_id)
{
return $this->redirect('/');
}
$user_id = null;
if (isset($this->Auth))
{
$user_id = $this->Auth->user('id');
}
$this->request->data['Comment']['foreign_model'] = $Model->name;
$this->request->data['Comment']['foreign_id'] = $foreign_id;
$this->request->data['Comment']['user_id'] = $user_id;
$this->Comment->create();
if (!$this->Comment->save($this->request->data))
{
$this->set('validation_errors', $this->Comment->validationErrors);
return;
}
$this->redirect($this->request->referer().'#comment-'.$this->Comment->id);
}
}
and in the add view in my element here is how i am trying to accessing the variable value
echo $this->Form->checkbox('ItQuery.status_type', array('type' => 'hidden', 'value'=>$variableValueStatus));
If someone can show me how to fix this, that would be awesome
You are only passing the variable down on !post and if foreign model and foreign id are not set.
That will most likely not work in most of the cases.
You should be always passing down a variable if you do not check on it in the view prior to using it.
But it would still be wrong, anyway. You would have to use "default" instead of "value" if you dont want your form to fall back on the old value on POST (which it shouldnt).
Also, it is always better to use $this->request->data to properly fill the form with defaults:
if (!$this->request->is('post')) {
$this->request->data['ItQuery']['status_type'] = 'Pending';
}
See working-with-forms

Adding a ubercart line item and updating order total

I don't know why this is so complicated but all I want to do is to add a line item in my order when user changes payment method in Ubercart. I managed to do this by adding the following line in my payment method.
uc_order_line_item_add($order->order_id, 'Pay at door','Pay at door',5);
This seems to add the line item but the order total does not get updated. When I refresh the page, I can see the line item being added there. Obviously, I don't want to refresh the screen and I want the new order total and the line item to be displayed during callback of my payment method. I guess this is achieved by calling jquery but I could not find anything useful in Google.
Can someone help?
Hope this answer isn't too late for you, but have you tried the following:
$order->line_items[] = uc_order_line_item_add($order->order_id, 'Pay at door','Pay at door',5);
The issue is that uc_order_line_item_add simply updates the database not the current order object that is being passed around and is later used to calculate totals etc, thankfully uc_order_line_item_add returns a line item array that can simply be appended to the order object. I am not sure that it will work inside your payment method but it works for me in hook_uc_order, where I had the similar issue of the tax not being updated until a second refresh.
In case it helps my full example code is below:
<?php
function my_module_uc_order($op, &$order, $arg2) {
switch ($op) {
case 'presave':
// using presave as taxes are calculated during save
$line_item_id = FALSE;
if (!is_array($order->line_items)) {
$order->line_items = array();
}
else {
foreach ($order->line_items as $index => $line_item) {
if ($line_item['type'] == 'my_line_item') {
$line_item_id = $line_item['line_item_id'];
break;
}
}
}
if ($line_item_id === FALSE) {
// Add item.
// Dummy amount for testing.
$amount = 3;
// uc_order_line_item_add returns a line_item array so we can just add it to the order object.
$order->line_items[] = uc_order_line_item_add($order->order_id, 'my_line_item', 'My Item Title (added)', $amount);
// uc_order_line_item_add($order_id, $type, $title, $amount, $weight = NULL, $data = NULL).
}
else {
// Update item.
// Dummy amount for testing.
$amount = 4;
// uc_order_update_line_item($id, $title, $amount, $data = NULL).
uc_order_update_line_item($line_item_id, 'My Item Title (updated)', $amount);
// Manually modify amount.
$order->line_items[$index]['amount'] = $amount;
}
break;
}
}
/**
* Implements hook_uc_line_item().
*/
function my_module_uc_line_item() {
$items[] = array(
'id' => 'my_line_item',
'title' => t('Custom text'),
'weight' => 0,
'default' => FALSE,
'stored' => TRUE,
'add_list' => TRUE,
'calculated' => TRUE,
);
return $items;
}

Resources